00001 /* 00002 GNU GENERAL PUBLIC LICENSE 00003 Version 2, June 1991 00004 00005 Copyright (C) 1989, 1991 Free Software Foundation, Inc. 00006 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00007 Everyone is permitted to copy and distribute verbatim copies 00008 of this license document, but changing it is not allowed. 00009 00010 Preamble 00011 00012 The licenses for most software are designed to take away your 00013 freedom to share and change it. By contrast, the GNU General Public 00014 License is intended to guarantee your freedom to share and change free 00015 software--to make sure the software is free for all its users. This 00016 General Public License applies to most of the Free Software 00017 Foundation's software and to any other program whose authors commit to 00018 using it. (Some other Free Software Foundation software is covered by 00019 the GNU Library General Public License instead.) You can apply it to 00020 your programs, too. 00021 00022 When we speak of free software, we are referring to freedom, not 00023 price. Our General Public Licenses are designed to make sure that you 00024 have the freedom to distribute copies of free software (and charge for 00025 this service if you wish), that you receive source code or can get it 00026 if you want it, that you can change the software or use pieces of it 00027 in new free programs; and that you know you can do these things. 00028 00029 To protect your rights, we need to make restrictions that forbid 00030 anyone to deny you these rights or to ask you to surrender the rights. 00031 These restrictions translate to certain responsibilities for you if you 00032 distribute copies of the software, or if you modify it. 00033 00034 For example, if you distribute copies of such a program, whether 00035 gratis or for a fee, you must give the recipients all the rights that 00036 you have. You must make sure that they, too, receive or can get the 00037 source code. And you must show them these terms so they know their 00038 rights. 00039 00040 We protect your rights with two steps: (1) copyright the software, and 00041 (2) offer you this license which gives you legal permission to copy, 00042 distribute and/or modify the software. 00043 00044 Also, for each author's protection and ours, we want to make certain 00045 that everyone understands that there is no warranty for this free 00046 software. If the software is modified by someone else and passed on, we 00047 want its recipients to know that what they have is not the original, so 00048 that any problems introduced by others will not reflect on the original 00049 authors' reputations. 00050 00051 Finally, any free program is threatened constantly by software 00052 patents. We wish to avoid the danger that redistributors of a free 00053 program will individually obtain patent licenses, in effect making the 00054 program proprietary. To prevent this, we have made it clear that any 00055 patent must be licensed for everyone's free use or not licensed at all. 00056 00057 The precise terms and conditions for copying, distribution and 00058 modification follow. 00059 00060 GNU GENERAL PUBLIC LICENSE 00061 TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 00062 00063 0. This License applies to any program or other work which contains 00064 a notice placed by the copyright holder saying it may be distributed 00065 under the terms of this General Public License. The "Program", below, 00066 refers to any such program or work, and a "work based on the Program" 00067 means either the Program or any derivative work under copyright law: 00068 that is to say, a work containing the Program or a portion of it, 00069 either verbatim or with modifications and/or translated into another 00070 language. (Hereinafter, translation is included without limitation in 00071 the term "modification".) Each licensee is addressed as "you". 00072 00073 Activities other than copying, distribution and modification are not 00074 covered by this License; they are outside its scope. The act of 00075 running the Program is not restricted, and the output from the Program 00076 is covered only if its contents constitute a work based on the 00077 Program (independent of having been made by running the Program). 00078 Whether that is true depends on what the Program does. 00079 00080 1. You may copy and distribute verbatim copies of the Program's 00081 source code as you receive it, in any medium, provided that you 00082 conspicuously and appropriately publish on each copy an appropriate 00083 copyright notice and disclaimer of warranty; keep intact all the 00084 notices that refer to this License and to the absence of any warranty; 00085 and give any other recipients of the Program a copy of this License 00086 along with the Program. 00087 00088 You may charge a fee for the physical act of transferring a copy, and 00089 you may at your option offer warranty protection in exchange for a fee. 00090 00091 2. You may modify your copy or copies of the Program or any portion 00092 of it, thus forming a work based on the Program, and copy and 00093 distribute such modifications or work under the terms of Section 1 00094 above, provided that you also meet all of these conditions: 00095 00096 a) You must cause the modified files to carry prominent notices 00097 stating that you changed the files and the date of any change. 00098 00099 b) You must cause any work that you distribute or publish, that in 00100 whole or in part contains or is derived from the Program or any 00101 part thereof, to be licensed as a whole at no charge to all third 00102 parties under the terms of this License. 00103 00104 c) If the modified program normally reads commands interactively 00105 when run, you must cause it, when started running for such 00106 interactive use in the most ordinary way, to print or display an 00107 announcement including an appropriate copyright notice and a 00108 notice that there is no warranty (or else, saying that you provide 00109 a warranty) and that users may redistribute the program under 00110 these conditions, and telling the user how to view a copy of this 00111 License. (Exception: if the Program itself is interactive but 00112 does not normally print such an announcement, your work based on 00113 the Program is not required to print an announcement.) 00114 00115 These requirements apply to the modified work as a whole. If 00116 identifiable sections of that work are not derived from the Program, 00117 and can be reasonably considered independent and separate works in 00118 themselves, then this License, and its terms, do not apply to those 00119 sections when you distribute them as separate works. But when you 00120 distribute the same sections as part of a whole which is a work based 00121 on the Program, the distribution of the whole must be on the terms of 00122 this License, whose permissions for other licensees extend to the 00123 entire whole, and thus to each and every part regardless of who wrote it. 00124 00125 Thus, it is not the intent of this section to claim rights or contest 00126 your rights to work written entirely by you; rather, the intent is to 00127 exercise the right to control the distribution of derivative or 00128 collective works based on the Program. 00129 00130 In addition, mere aggregation of another work not based on the Program 00131 with the Program (or with a work based on the Program) on a volume of 00132 a storage or distribution medium does not bring the other work under 00133 the scope of this License. 00134 00135 3. You may copy and distribute the Program (or a work based on it, 00136 under Section 2) in object code or executable form under the terms of 00137 Sections 1 and 2 above provided that you also do one of the following: 00138 00139 a) Accompany it with the complete corresponding machine-readable 00140 source code, which must be distributed under the terms of Sections 00141 1 and 2 above on a medium customarily used for software interchange; or, 00142 00143 b) Accompany it with a written offer, valid for at least three 00144 years, to give any third party, for a charge no more than your 00145 cost of physically performing source distribution, a complete 00146 machine-readable copy of the corresponding source code, to be 00147 distributed under the terms of Sections 1 and 2 above on a medium 00148 customarily used for software interchange; or, 00149 00150 c) Accompany it with the information you received as to the offer 00151 to distribute corresponding source code. (This alternative is 00152 allowed only for noncommercial distribution and only if you 00153 received the program in object code or executable form with such 00154 an offer, in accord with Subsection b above.) 00155 00156 The source code for a work means the preferred form of the work for 00157 making modifications to it. For an executable work, complete source 00158 code means all the source code for all modules it contains, plus any 00159 associated interface definition files, plus the scripts used to 00160 control compilation and installation of the executable. However, as a 00161 special exception, the source code distributed need not include 00162 anything that is normally distributed (in either source or binary 00163 form) with the major components (compiler, kernel, and so on) of the 00164 operating system on which the executable runs, unless that component 00165 itself accompanies the executable. 00166 00167 If distribution of executable or object code is made by offering 00168 access to copy from a designated place, then offering equivalent 00169 access to copy the source code from the same place counts as 00170 distribution of the source code, even though third parties are not 00171 compelled to copy the source along with the object code. 00172 00173 4. You may not copy, modify, sublicense, or distribute the Program 00174 except as expressly provided under this License. Any attempt 00175 otherwise to copy, modify, sublicense or distribute the Program is 00176 void, and will automatically terminate your rights under this License. 00177 However, parties who have received copies, or rights, from you under 00178 this License will not have their licenses terminated so long as such 00179 parties remain in full compliance. 00180 00181 5. You are not required to accept this License, since you have not 00182 signed it. However, nothing else grants you permission to modify or 00183 distribute the Program or its derivative works. These actions are 00184 prohibited by law if you do not accept this License. Therefore, by 00185 modifying or distributing the Program (or any work based on the 00186 Program), you indicate your acceptance of this License to do so, and 00187 all its terms and conditions for copying, distributing or modifying 00188 the Program or works based on it. 00189 00190 6. Each time you redistribute the Program (or any work based on the 00191 Program), the recipient automatically receives a license from the 00192 original licensor to copy, distribute or modify the Program subject to 00193 these terms and conditions. You may not impose any further 00194 restrictions on the recipients' exercise of the rights granted herein. 00195 You are not responsible for enforcing compliance by third parties to 00196 this License. 00197 00198 7. If, as a consequence of a court judgment or allegation of patent 00199 infringement or for any other reason (not limited to patent issues), 00200 conditions are imposed on you (whether by court order, agreement or 00201 otherwise) that contradict the conditions of this License, they do not 00202 excuse you from the conditions of this License. If you cannot 00203 distribute so as to satisfy simultaneously your obligations under this 00204 License and any other pertinent obligations, then as a consequence you 00205 may not distribute the Program at all. For example, if a patent 00206 license would not permit royalty-free redistribution of the Program by 00207 all those who receive copies directly or indirectly through you, then 00208 the only way you could satisfy both it and this License would be to 00209 refrain entirely from distribution of the Program. 00210 00211 If any portion of this section is held invalid or unenforceable under 00212 any particular circumstance, the balance of the section is intended to 00213 apply and the section as a whole is intended to apply in other 00214 circumstances. 00215 00216 It is not the purpose of this section to induce you to infringe any 00217 patents or other property right claims or to contest validity of any 00218 such claims; this section has the sole purpose of protecting the 00219 integrity of the free software distribution system, which is 00220 implemented by public license practices. Many people have made 00221 generous contributions to the wide range of software distributed 00222 through that system in reliance on consistent application of that 00223 system; it is up to the author/donor to decide if he or she is willing 00224 to distribute software through any other system and a licensee cannot 00225 impose that choice. 00226 00227 This section is intended to make thoroughly clear what is believed to 00228 be a consequence of the rest of this License. 00229 00230 8. If the distribution and/or use of the Program is restricted in 00231 certain countries either by patents or by copyrighted interfaces, the 00232 original copyright holder who places the Program under this License 00233 may add an explicit geographical distribution limitation excluding 00234 those countries, so that distribution is permitted only in or among 00235 countries not thus excluded. In such case, this License incorporates 00236 the limitation as if written in the body of this License. 00237 00238 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. 00239 00240 Each version is given a distinguishing version number. If the Program 00241 specifies a version number of this License which applies to it and "any 00242 later version", you have the option of following the terms and conditions 00243 either of that version or of any later version published by the Free Software 00244 Foundation. If the Program does not specify a version number of this License, 00245 you may choose any version ever published by the Free Software Foundation. 00246 00247 10. If you wish to incorporate parts of the Program into other free 00248 programs whose distribution conditions are different, write to the author to 00249 ask for permission. For software which is copyrighted by the Free Software 00250 Foundation, write to the Free Software Foundation; we sometimes make 00251 exceptions for this. Our decision will be guided by the two goals of 00252 preserving the free status of all derivatives of our free software and of 00253 promoting the sharing and reuse of software generally. 00254 00255 NO WARRANTY 00256 00257 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR 00258 THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 00259 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE 00260 THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, 00261 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 00262 FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND 00263 PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, 00264 YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 00265 00266 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 00267 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 00268 REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 00269 INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 00270 OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO 00271 LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR 00272 THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 00273 EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 00274 DAMAGES. 00275 00276 END OF TERMS AND CONDITIONS 00277 */ 00278 static const char* Copyright = "(C) Copyright Michigan State University 2002, All rights reserved"; 00281 /* 00282 $Header: /usr/TruCluster/users/daqcvs/repository/NewClients/Readout/CExperiment.cpp,v 1.6 2002/10/22 12:38:16 fox Exp $ 00283 00284 Modification History: 00285 $Log: CExperiment.cpp,v $ 00286 // Revision 1.6 2002/10/22 12:38:16 fox 00287 // Straighten out dates in internal copyright notices. 00288 // 00289 // Revision 1.5 2002/10/16 16:16:09 fox 00290 // Add events and words status consts: Number of events acquired and number of 00291 // words acquired in current run. 00292 // 00293 // Revision 1.4 2002/10/16 14:22:21 fox 00294 // Change some runvars into consts: 00295 // - state -- the run state is now a const maintained by the program. 00296 // - starttime - the time at which the run started. 00297 // 00298 // Link in libraries from . before $(INSTDIR) 00299 // 00300 // Revision 1.3 2002/10/15 12:48:23 fox 00301 // 1. Initial testing 00302 // 2. Add const variable and make tkloaded const. 00303 // 3. Add TclServer functionality. 00304 // 00305 // Revision 1.2 2002/10/09 11:22:25 fox 00306 // Stamp with copyright/gpl license notice 00307 // 00308 */ 00309 00311 #include "CExperiment.h" 00312 #include "CStateTransitionCommand.h" 00313 #include "CBeginCommand.h" 00314 #include "CEndCommand.h" 00315 #include "CReadoutMain.h" 00316 #include "CDuplicateSingleton.h" 00317 #include "CNoSuchObjectException.h" 00318 #include "CApplicationSerializer.h" 00319 #include "CNSCLPhysicsBuffer.h" 00320 #include "CNSCLScalerBuffer.h" 00321 #include "CNSCLControlBuffer.h" 00322 #include "CStateTransitionCommand.h" 00323 #include "CRunVariableBuffer.h" 00324 #include "CStateVariableBuffer.h" 00325 #include "CNSCLDocumentationBuffer.h" 00326 #include "CDocumentedPacketManager.h" 00327 #include "CDocumentedPacket.h" 00328 #include "CInterpreterCore.h" 00329 #include "CInterpreterShell.h" 00330 00331 00332 #include "CTimer.h" 00333 #include "CScalerTrigger.h" 00334 00335 00336 #include "CRunVariableCommand.h" 00337 #include "CRunVariable.h" 00338 00339 #include "CStateVariableCommand.h" 00340 #include "CStateVariable.h" 00341 00342 #include "buftypes.h" 00343 #include "buffer.h" 00344 00345 #include <spectrodaq.h> 00346 #include <time.h> 00347 #include <sys/time.h> 00348 #include <unistd.h> 00349 #include <stdlib.h> 00350 #include <typeinfo> 00351 #include <iostream.h> 00352 #include <algorithm> 00353 #include <tcl.h> 00354 00355 extern CReadoutMain MyApp; 00356 00358 00359 static const unsigned int nTriggerDwellTime = 100; 00360 static const unsigned int nTriggersPerPoll = 10; 00361 // checks. 00362 static const unsigned int msPerClockTick = 100; 00363 // ticks. 00364 static const unsigned int SECOND = 1000000; 00365 static const unsigned int MILISECOND = 1000; 00366 00367 00374 class CTriggerThread : public DAQThread 00375 { 00376 CExperiment* m_pExperiment; 00377 CTrigger* m_pTrigger; 00378 bool m_Exiting; 00379 DAQThreadId m_Id; 00380 unsigned int m_msHoldTime; 00381 unsigned int m_nTriggerdwell; 00382 public: 00383 CTriggerThread(CExperiment* pExp, CTrigger* pTrig); 00384 00385 void Start(); // Start execution. 00386 void Stop(); // Stop execution. 00387 protected: 00388 virtual int operator()(int argc, char** argv); 00389 virtual void MainLoop(); 00390 00391 }; 00393 CTriggerThread::CTriggerThread(CExperiment* pExp, CTrigger* pTrig) : 00394 m_pExperiment(pExp), 00395 m_pTrigger(pTrig), 00396 m_Exiting(false), 00397 m_msHoldTime(nTriggerDwellTime), 00398 m_nTriggerdwell(nTriggersPerPoll) 00399 {} 00401 void 00402 CTriggerThread::Start() 00403 { 00404 m_Exiting = false; 00405 m_Id = daq_dispatcher.Dispatch(*this, 0, 0); 00406 } 00412 void 00413 CTriggerThread::Stop() 00414 { 00415 if(m_Exiting) return; // Already exiting or done. 00416 00417 DAQThreadId id( m_Id); 00418 m_Exiting = true; 00419 00420 // We have the mutex, we need to release it so the triger thread 00421 // can get it to run... we'll re-acquire after the join at the same level. 00422 // 00423 CApplicationSerializer& mutex(*(CApplicationSerializer::getInstance())); 00424 00425 unsigned nLockLevel = mutex.getLockLevel(); 00426 mutex.UnLockCompletely(); 00427 00428 Join(m_Id); // Wait for trigger to exit. 00429 00430 for(unsigned i =0; i < nLockLevel; i++) { 00431 mutex.Lock(); 00432 } 00433 } 00435 // 00436 int 00437 CTriggerThread::operator()(int argc, char** argv) 00438 { 00439 MainLoop(); 00440 return 0; 00441 } 00442 00463 void 00464 CTriggerThread::MainLoop() 00465 { 00466 while(!m_Exiting) { 00467 struct timeval mutexstart; 00468 struct timeval mutexend; 00469 struct timezone tz; // Unused but required for gettimeofday(2). 00470 int dwell; 00471 // 00472 // Lock the mutex and process triggers for the dwell time. 00473 // The trigger is checked several times to amortize gettimeofday(). 00474 // 00475 gettimeofday(&mutexstart, &tz); 00476 CApplicationSerializer::getInstance()->Lock(); 00477 do { 00478 for(int i = 0; i < 500; i++) { 00479 int triggers=0; 00480 if((*m_pTrigger)()) { // Read an event... 00481 m_pExperiment->ReadEvent(); 00482 if((triggers++) >= m_nTriggerdwell) break; // Check elapsed time. 00483 } 00484 } 00485 // If we've held the mutex for longer than m_msHoldTime, 00486 // release the mutex so that other threads get a chance to run. 00487 // 00488 gettimeofday(&mutexend, &tz); 00489 int secdif = mutexend.tv_sec - mutexstart.tv_sec; 00490 mutexend.tv_usec += SECOND*secdif; 00491 dwell = (mutexend.tv_usec - mutexstart.tv_usec)/MILISECOND; 00492 } while(dwell < m_msHoldTime); 00493 CApplicationSerializer::getInstance()->UnLock(); 00494 } 00495 } 00496 00497 00498 00504 CExperiment::CExperiment (unsigned nBufferSize = 4096) : 00505 m_EventBuffer(0), // Spectrodaq might not be up yet. 00506 m_nBufferSize(nBufferSize), 00507 m_pStatusModule(0), 00508 m_pTrigger(0), 00509 m_pTThread(0), 00510 m_LastSnapTime(0), 00511 m_LastScalerTime(0), 00512 m_nEventsAcquired(0), 00513 m_nWordsAcquired(0) 00514 { 00515 SetupTimedEvent(); 00516 } 00545 CExperiment::CExperiment(CTrigger* pTriggerModule, 00546 CEventSegment* pEventReadout, 00547 CScaler* pScalers, 00548 CStatusModule* pStatus, 00549 unsigned nBufferSize) : 00550 m_EventBuffer(0), // Spectrodaq may not be booted yet. 00551 m_nBufferSize(nBufferSize*2), 00552 m_pStatusModule(pStatus), 00553 m_pTrigger(pTriggerModule), 00554 m_pTThread(0), 00555 m_LastSnapTime(0), 00556 m_LastScalerTime(0), 00557 m_nEventsAcquired(0), 00558 m_nWordsAcquired(0) 00559 { 00560 if(pScalers) m_Scalers.AddScalerModule(pScalers); 00561 m_EventReadout.AddSegment(pEventReadout); 00562 SetupTimedEvent(); 00563 } 00564 00565 // Functions for class CExperiment 00566 00584 void 00585 CExperiment::Start(CStateTransitionCommand& rCommand) 00586 { 00587 // Execute pre-actions: 00588 00589 rCommand.ExecutePreFunction(); 00590 00591 // Figure out what kind of buffer to emit and emit it. 00592 00593 try { 00594 CBeginCommand& rBegin(dynamic_cast<CBeginCommand&>(rCommand)); // Throws if resume. 00595 MyApp.getClock().Reset(); // Reset the run elapsed time. 00596 EmitStart(); // and emit a begin buffer. 00597 m_LastScalerTime = 0; // Neither scalers have been readout yet this run. 00598 m_LastSnapTime = 0; 00599 00600 // Set the starttime variable: 00601 00602 CInterpreterShell* pShell = CReadoutMain::getInstance()->getInterpreter(); 00603 CInterpreterCore* pCore = pShell->getInterpreterCore(); 00604 time_t epochTime = time(NULL); 00605 string sTime(ctime(&epochTime)); 00606 pCore->setStartTime(sTime); 00607 00608 // Reset the statistical counters: 00609 00610 m_nEventsAcquired = 0; 00611 m_nWordsAcquired = 0; 00612 00613 pCore->setEvents(m_nEventsAcquired); 00614 pCore->setWords(m_nWordsAcquired); 00615 00616 } 00617 catch (bad_cast& rbad) { 00618 m_LastScalerTime = 0; // Snaps will not have been read out at resume. 00619 EmitResume(); // Emit a resume without zeroing the run elapsed time. 00620 } 00621 00622 // Emit documentation and runstate variable buffers. 00623 00624 TriggerDocBuffer(); 00625 TriggerRunVariableBuffer(); 00626 TriggerStateVariableBuffer(); 00627 00628 // Prepare the hardware for readout: 00629 00630 m_Scalers.Clear(); 00631 m_EventReadout.Initialize(); // Initialize the event readout... 00632 m_EventReadout.Clear(); // Clear digitizers prior to start. 00633 00634 // Start the trigger process and clock. 00635 00636 StartTrigger(); 00637 m_pScalerTrigger->SetInterval(MyApp.getScalerPeriod() * 1000); 00638 MyApp.getClock().Start(msPerClockTick, nTriggerDwellTime); 00639 00640 00641 00642 00643 //<----------- At this point we can potentially take data. 00644 // Execute post-actions: 00645 00646 rCommand.ExecutePostFunction(); 00647 } 00648 00661 void 00662 CExperiment::Stop(CStateTransitionCommand& rCommand) 00663 { 00664 // Do the pre actions. 00665 00666 rCommand.ExecutePreFunction(); 00667 00668 00669 // Emit documentation and variable list buffers. 00670 00671 TriggerDocBuffer(); 00672 TriggerRunVariableBuffer(); 00673 TriggerStateVariableBuffer(); 00674 00675 // Stop the trigger and clock processes. Note that the stop trigger function 00676 // synchronizes with the exit of the trigger thread. 00677 00678 StopTrigger(); //<--------- At this point we can't take data. 00679 TriggerScalerReadout(); // Closing scaler buffers. 00680 00681 // Figure out what kind of event this is and emit the appropriate buffer 00682 // (End or Pause). 00683 00684 try { 00685 CEndCommand& rend(dynamic_cast<CEndCommand&>( rCommand)); 00686 EmitEnd(); 00687 } 00688 catch (bad_cast& rbad) { 00689 EmitPause(); 00690 } 00691 00692 // Do the post actions. 00693 00694 MyApp.getClock().Stop(); 00695 rCommand.ExecutePostFunction(); 00696 } 00697 00705 void 00706 CExperiment::ReadEvent() 00707 { 00708 00709 // For sure spectrodaq is booted and the event buffer can be created 00710 // at this time. 00711 // 00712 if(!m_EventBuffer) { 00713 m_EventBuffer = new CNSCLPhysicsBuffer(m_nBufferSize * 2); 00714 } 00715 00716 DAQWordBufferPtr ptr(m_EventBuffer->StartEvent()); 00717 DAQWordBufferPtr hdr = ptr; 00718 00719 ptr = m_EventReadout.Read(ptr); 00720 m_EventReadout.Clear(); 00721 PostEvent(); 00722 00723 m_nEventsAcquired++; 00724 m_nWordsAcquired += ptr.GetIndex() - hdr.GetIndex(); 00725 if(ptr.GetIndex() > m_nBufferSize) { 00726 00727 Overflow(hdr, ptr); 00728 00729 // Update the Tcl statistic vars: 00730 00731 CInterpreterShell* pShell = 00732 CReadoutMain::getInstance()->getInterpreter(); 00733 CInterpreterCore* pCore = pShell->getInterpreterCore(); 00734 pCore->setEvents(m_nEventsAcquired); 00735 pCore->setWords(m_nWordsAcquired); 00736 00737 00738 } else { 00739 m_EventBuffer->EndEvent(ptr); 00740 } 00741 00742 if(!m_EventBuffer->EntityFits(m_EventReadout.size())) { 00743 m_EventBuffer->SetRun(GetRunNumber()); 00744 m_EventBuffer->Route(); 00745 } 00746 } 00747 00754 void 00755 CExperiment::PostEvent() 00756 { 00757 00758 // 00759 // Clear the busy 00760 00761 ClearBusy(); 00762 00763 } 00764 00779 void 00780 CExperiment::AddEventSegment(CEventSegment* rSegment) 00781 { 00782 m_EventReadout.AddSegment(rSegment); 00783 } 00784 00794 void 00795 CExperiment::RemoveEventSegment(CEventSegment* pSegment) 00796 { 00797 m_EventReadout.DeleteSegment(pSegment); // Throws the exception for us. 00798 } 00799 00806 void 00807 CExperiment::SetBusy() 00808 { 00809 m_pStatusModule->GoBusy(); 00810 } 00811 00819 void 00820 CExperiment::ClearBusy() 00821 { 00822 m_pStatusModule->ModuleClear(); 00823 m_pStatusModule->GoClear(); 00824 } 00825 00834 void 00835 CExperiment::EstablishTrigger(CTrigger* pTrigger) 00836 { 00837 m_pTrigger = pTrigger; 00838 } 00839 00840 00841 00853 void 00854 CExperiment::EstablishBusy(CStatusModule* pStatus) 00855 { 00856 m_pStatusModule = pStatus; 00857 } 00858 00868 void 00869 CExperiment::TriggerScalerReadout() 00870 { 00871 00872 // If there is an event data buffer with events in it 00873 // commit it: 00874 00875 if(m_EventBuffer) { 00876 00877 if(m_EventBuffer->getEntityCount()) { 00878 m_EventBuffer->SetRun(GetRunNumber()); 00879 m_EventBuffer->Route(); 00880 } 00881 } 00882 00883 // 00884 vector<unsigned long> scalers = m_Scalers.Read(); 00885 m_Scalers.Clear(); 00886 CNSCLScalerBuffer buffer(m_nBufferSize); 00887 00888 // If a snapshot scaler has been readout, the values 00889 // just read must be added to their sums. 00890 // 00891 if((!m_IntervalSums.empty()) && (m_LastSnapTime != 0)) { 00892 int nelements = (scalers.size() <= m_IntervalSums.size()) ? scalers.size() : 00893 m_IntervalSums.size(); 00894 for(int i =0; i < nelements; i++) { 00895 scalers[i] += m_IntervalSums[i]; 00896 m_IntervalSums[i] = 0; 00897 } 00898 } 00899 // Format the buffer and adjust the times: 00900 // 00901 00902 buffer.PutScalerVector(scalers); 00903 buffer.SetStartTime(m_LastScalerTime); 00904 buffer.SetEndTime(m_LastScalerTime = GetElapsedTime()); 00905 buffer.SetRun(GetRunNumber()); 00906 buffer.Route(false); // No sequence increment for scalers. 00907 00908 m_LastSnapTime = 0; 00909 m_LastScalerTime = GetElapsedTime(); 00910 00911 00912 } 00913 00922 void 00923 CExperiment::TriggerRunVariableBuffer() 00924 { 00925 CRunVariableCommand* 00926 Vars(MyApp.getInterpreter()->getInterpreterCore()->getRunVariables()); 00927 00928 RunVariableIterator i = Vars->begin(); 00929 00930 // 00931 // Multiple buffers may be required: 00932 00933 while(i != Vars->end()) { 00934 CRunVariableBuffer buf; 00935 i = EmitRunVariableBuffer(buf, i, Vars->end()); 00936 buf.SetRun(GetRunNumber()); 00937 buf.Route(false); // No sequenc increment for run variable buffers. 00938 } 00939 00940 } 00941 void 00942 CExperiment::TriggerStateVariableBuffer() 00943 { 00944 CStateVariableCommand* 00945 Vars(MyApp.getInterpreter()->getInterpreterCore()->getStateVariables()); 00946 StateVariableIterator i = Vars->begin(); 00947 StateVariableIterator e = Vars->end(); 00948 // Multiple buffers may be required: 00949 00950 while(i != Vars->end()) { 00951 CStateVariableBuffer buf(m_nBufferSize); 00952 i = EmitStateVariableBuffer(buf, i, e); 00953 buf.SetRun(GetRunNumber()); 00954 buf.Route(false); // No sequenc increment for run variable buffers. 00955 } 00956 00957 } 00965 void 00966 CExperiment::TriggerSnapshotScaler() 00967 { 00968 00969 CNSCLScalerBuffer buffer(m_nBufferSize); 00970 vector<unsigned long> scalers = m_Scalers.Read(); 00971 m_Scalers.Clear(); 00972 00973 // Sum the scalers into the snapshot scaler totals vector. 00974 // If necessary, the scaler vector is extended. 00975 00976 for(int i = 0; i < scalers.size(); i++) { 00977 if(i < m_IntervalSums.size()) { 00978 m_IntervalSums[i] += scalers[i]; 00979 } 00980 else { 00981 m_IntervalSums.push_back(scalers[i]); 00982 } 00983 } 00984 // Now emit the scaler buffer, and adjust the times: 00985 00986 buffer.PutScalerVector(scalers); 00987 buffer.SetType(SNAPSCBF); 00988 buffer.SetStartTime(m_LastSnapTime); 00989 buffer.SetEndTime(m_LastSnapTime = GetElapsedTime()); 00990 00991 buffer.SetRun(GetRunNumber()); 00992 buffer.Route(false); 00993 00994 } 00995 01004 void 01005 CExperiment::TriggerDocBuffer() 01006 { 01007 CDocumentedPacketManager* pManager = CDocumentedPacketManager::getInstance(); 01008 DocumentationPacketIterator i = pManager->begin(); 01009 while(i != pManager->end()) { 01010 CNSCLDocumentationBuffer buf(m_nBufferSize); 01011 01012 i = EmitDocBuffer(i, pManager->end(), buf); 01013 buf.SetRun(GetRunNumber()); 01014 buf.Route(false); 01015 } 01016 01017 } 01018 01025 void 01026 CExperiment::AddScalerModule(CScaler* pScaler) 01027 { 01028 m_Scalers.AddScalerModule(pScaler); 01029 } 01030 01038 void 01039 CExperiment::RemoveScalerModule(CScaler* pScaler) 01040 { 01041 m_Scalers.DeleteScalerModule(pScaler); 01042 } 01043 01061 void 01062 CExperiment::EmitStart() 01063 { 01064 CNSCLOutputBuffer::ClearSequence(); // Begin run always has zero for sequence number. 01065 01066 // Need a bunch of stuff: 01067 // Title run number, time offset and time of day. 01068 01069 CNSCLControlBuffer buffer(m_nBufferSize); 01070 buffer.PutTitle(MyApp.getTitle()); 01071 buffer.PutTimeOffset(GetElapsedTime()); 01072 buffer.SetRun(GetRunNumber()); 01073 buffer.SetType(BEGRUNBF); 01074 buffer.Route(false); 01075 01076 } 01081 void 01082 CExperiment::EmitEnd() 01083 { 01084 CNSCLControlBuffer buffer(m_nBufferSize); 01085 buffer.PutTitle(MyApp.getTitle()); 01086 buffer.PutTimeOffset(GetElapsedTime()); 01087 buffer.SetRun(GetRunNumber()); 01088 buffer.SetType(ENDRUNBF); 01089 buffer.Route(false); 01090 } 01095 void 01096 CExperiment::EmitPause() 01097 { 01098 CNSCLControlBuffer buffer(m_nBufferSize); 01099 buffer.PutTitle(MyApp.getTitle()); 01100 buffer.PutTimeOffset(GetElapsedTime()); 01101 buffer.SetRun(GetRunNumber()); 01102 buffer.SetType(PAUSEBF); 01103 buffer.Route(false); 01104 01105 } 01110 void 01111 CExperiment::EmitResume() 01112 { 01113 CNSCLControlBuffer buffer(m_nBufferSize); 01114 buffer.PutTitle(MyApp.getTitle()); 01115 buffer.PutTimeOffset(GetElapsedTime()); 01116 buffer.SetRun(GetRunNumber()); 01117 buffer.SetType(RESUMEBF); 01118 buffer.Route(false); 01119 01120 } 01132 void 01133 CExperiment::StartTrigger() 01134 { 01135 if(m_pTThread) { // Should be null...else running. 01136 throw CDuplicateSingleton("Creating trigger thread", 01137 "TriggerThreadObject"); 01138 } 01139 m_pTThread = new CTriggerThread(this, m_pTrigger); 01140 m_pTThread->Start(); 01141 01142 } 01152 void 01153 CExperiment::StopTrigger() 01154 { 01155 if(m_pTThread) { 01156 m_pTThread->Stop(); 01157 delete m_pTThread; 01158 m_pTThread = (CTriggerThread*)NULL; 01159 } 01160 } 01178 RunVariableIterator 01179 CExperiment::EmitRunVariableBuffer(CRunVariableBuffer& rBuffer, 01180 RunVariableIterator start, 01181 RunVariableIterator end) 01182 { 01183 while(start != end) { 01184 string item = (start->second)->FormatForBuffer(); 01185 if(!rBuffer.PutEntityString(item)) break; // Won't fit if break. 01186 start++; 01187 } 01188 return start; // Return the next entity. 01189 } 01204 StateVariableIterator 01205 CExperiment::EmitStateVariableBuffer(CStateVariableBuffer& rBuffer, 01206 StateVariableIterator& start, 01207 StateVariableIterator& end) 01208 { 01209 while(start != end) { 01210 CStateVariable* pv = start->second; 01211 01212 string item = pv->FormatForBuffer(); 01213 if(!rBuffer.PutEntityString(item)) break; 01214 start++; 01215 } 01216 return start; 01217 } 01235 DocumentationPacketIterator 01236 CExperiment::EmitDocBuffer(DocumentationPacketIterator s, 01237 DocumentationPacketIterator e, 01238 CNSCLDocumentationBuffer& b) 01239 { 01240 while(s != e) { 01241 string item = (*s)->Format(); 01242 if(!b.PutEntityString(item)) break; 01243 s++; 01244 } 01245 return s; 01246 } 01260 void 01261 CExperiment::Overflow(DAQWordBufferPtr& header, 01262 DAQWordBufferPtr& end) 01263 { 01264 01265 // Copy the overflowing event to a new buffer: 01266 01267 CNSCLPhysicsBuffer* pNewBuffer = new CNSCLPhysicsBuffer(m_nBufferSize*2); 01268 DAQWordBufferPtr pDest = pNewBuffer->StartEvent(); 01269 DAQWordBufferPtr pSrc = header; 01270 DAQWordBufferPtr pEnd = end; 01271 while(pSrc != pEnd) { 01272 *pDest = *pSrc; 01273 ++pDest; ++pSrc; // Preinccrement is fastest. 01274 } 01275 pNewBuffer->EndEvent(pDest); 01276 01277 // Retract the event from the old buffer and route it: 01278 01279 m_EventBuffer->RetractEvent(header); 01280 m_EventBuffer->SetRun(GetRunNumber()); 01281 m_EventBuffer->Route(); 01282 delete m_EventBuffer; 01283 01284 // And put the new event buffer in place as the class member.: 01285 01286 m_EventBuffer = pNewBuffer; 01287 01288 } 01292 unsigned short 01293 CExperiment::GetRunNumber() const 01294 { 01295 01296 // Locate the state variable command object as it containst the 01297 // database of state variables. 01298 01299 01300 CReadoutMain* pReadout = CReadoutMain::getInstance(); 01301 CInterpreterShell* pShell = pReadout->getInterpreter(); 01302 CInterpreterCore* pCore = pShell->getInterpreterCore(); 01303 CStateVariableCommand& rState(*(pCore->getStateVariables())); 01304 01305 // Now look for the state variable named "run" as that has the run number. 01306 01307 StateVariableIterator i = rState.find(string("run")); 01308 if(i == rState.end()) { 01309 throw CNoSuchObjectException("Getting the run number variable object", 01310 "run"); 01311 } 01312 01313 return atoi((i->second)->Get(TCL_GLOBAL_ONLY)); 01314 01315 } 01321 unsigned long 01322 CExperiment::GetElapsedTime() const 01323 { 01324 // Note that CTimer::GetElapsedTime's units are ms. 01325 01326 return (MyApp.getClock().GetElapsedTime()) / 100; 01327 } 01333 void 01334 CExperiment::SetupTimedEvent() 01335 { 01336 CTimer& rTimer(MyApp.getClock()); 01337 m_pScalerTrigger = new CScalerTrigger(*this); 01338 rTimer.EstablishEvent(*m_pScalerTrigger); 01339 01340 }