00001 // ===================================================================== 00002 // $Id: TEventManager.cc,v 1.7 2004/03/07 10:30:30 goiwai Exp $ 00003 // $Name: CLDAQ-1-14-03 $ 00004 // $Log: TEventManager.cc,v $ 00005 // Revision 1.7 2004/03/07 10:30:30 goiwai 00006 // ROOTに組みこむためのおまじないマクロを埋めこみました。 00007 // 全てにおいて完全に動作するわけではありません。 00008 // 00009 // Revision 1.6 2004/03/04 14:53:43 goiwai 00010 // BeginOfEventAction -> AtFirst 00011 // EndOfEventAction -> AtLast 00012 // WaitEvent -> WaitTrigger 00013 // 00014 // Revision 1.5 2003/10/06 17:01:32 goiwai 00015 // Clear()はRunManagerのStopRun()で呼ばれるんですが,そこで 00016 // theLastAccessID=-1としました. 00017 // 00018 // Revision 1.4 2003/10/06 16:50:25 goiwai 00019 // ちょっとわかりづらい仕様なんですけど,CLDAQではイベントスタックはRunオ 00020 // ブジェクトが抱えます.このイベントスタックへのアクセスメソッドを 00021 // EventManagerに追加しました. 00022 // 動作としてはクリア,検索,最新のイベント,前回のイベント,次のイベントの取 00023 // 得です. 00024 // イベントマネージャーを通じてスタックのイベントを取得するたびに,スタッ 00025 // クのヒストリは更新されます.LastEventとかNextEventというのはこのヒスト 00026 // リを見てEventを返してきます.スタックサイズが足りなくてすでに目的のイベ 00027 // ントが存在しないときはEventID=-1のEventが返ってきます. 00028 // 00029 // Revision 1.3 2003/07/30 16:18:09 goiwai 00030 // ファイルにコミットログをつけることにしました. 00031 // 00032 // ===================================================================== 00033 #include "TEventManager.hh" 00034 #include "TRunManager.hh" 00035 #include "TEventAction.hh" 00036 #include "TDataRecord.hh" 00037 #include "TEventAction.hh" 00038 #include "TEventStack.hh" 00039 #include "TReadoutList.hh" 00040 #include "TRun.hh" 00041 00042 TEventManager* TEventManager::theEventManager = 0; 00043 00044 TEventManager::TEventManager( Tint stacksize ) 00045 : theStatus( tStatusDead ), 00046 theStackSize( stacksize ), 00047 theNumberOfEvents( 0 ), 00048 theEventAction( 0 ), 00049 theEvent(), 00050 //theEventTimer( Tmsec ) 00051 theLastAccessID( -1 ) 00052 { 00053 if ( theEventManager ) { 00054 Tcerr << "TEventManager::TEventManager: EventManager constructed twice." << Tendl; 00055 exit( -EFAULT ); 00056 } 00057 theEventManager = this; 00058 theStatus = tStatusReady; 00059 } 00060 00061 TEventManager::~TEventManager() 00062 { 00063 Tstring head = "TEventManager::~TEventManager: "; 00064 if ( theEventAction ) { 00065 delete theEventAction; 00066 theEventAction = 0; 00067 Tcout << head << "EventAction was deleted." << Tendl; 00068 } 00069 theStatus = tStatusDead; 00070 Tcout << head << "EventManager was deleted." << Tendl; 00071 theEventManager = 0; 00072 } 00073 00074 const TEvent& TEventManager::TakeEvent() 00075 { 00076 //theEventTimer.Start(); 00077 00078 theStatus = tStatusJustTakingEvent; 00079 00080 theEvent.Clear(); 00081 theEvent.SetEventID( theNumberOfEvents ); 00082 if ( theEventAction ) { 00083 theEventAction -> AtFirst( theEvent ); 00084 // タイムアウト処理をするべきかも? 00085 theEvent.SetDataRecord( theEventAction -> WaitTrigger() -> Read() ); 00086 theEventAction -> AtLast( theEvent ); 00087 } 00088 00089 theNumberOfEvents ++; 00090 00091 theStatus = tStatusReady; 00092 return theEvent; 00093 } 00094 00095 const TRun& TEventManager::RecordEvent() 00096 { 00097 theStatus = tStatusJustRecordingEvent; 00098 TRun& run = TRunManager::GetRunManager() -> GetRun(); 00099 if ( theNumberOfEvents == 0 || theStackSize <= 0 ) { 00100 theStatus = tStatusReady; 00101 //theEventTimer.Stop(); 00102 return run; 00103 } 00104 00105 00106 TEventStack& eventstack = run.GetEventStack(); 00107 if ( !eventstack.empty() && eventstack[ eventstack.size() - 1 ].GetEventID() == theEvent.GetEventID() ) { 00108 theStatus = tStatusReady; 00109 //theEventTimer.Stop(); 00110 return run; 00111 } 00112 00113 00114 if ( theNumberOfEvents > theStackSize ) { 00115 eventstack.erase( eventstack.begin() ); 00116 } 00117 00118 eventstack.push_back( theEvent ); 00119 00120 theStatus = tStatusReady; 00121 //theEventTimer.Stop(); 00122 00123 return run; 00124 } 00125 00126 Tvoid TEventManager::ShowStatus() const 00127 { 00128 switch ( theStatus ) { 00129 case tStatusReady: 00130 Tcout << "STATUS" << Tendl; 00131 Tcout << Ttab << "Ready(" << theStatus << ")" << Tendl; 00132 Tcout << "DESCRIPTION" << Tendl; 00133 Tcout << Ttab << "Just be preparing for next event or suspended the run." << Tendl; 00134 break; 00135 case tStatusJustTakingEvent: 00136 Tcout << "STATUS" << Tendl; 00137 Tcout << Ttab << "JustTakingEvent(" << theStatus << ")" << Tendl; 00138 Tcout << "DESCRIPTION" << Tendl; 00139 Tcout << Ttab << "This status is kept on during following procedure:" << Tendl; 00140 Tcout << Ttab << " 1. Clear the last event." << Tendl; 00141 Tcout << Ttab << " 2. Set the event ID." << Tendl; 00142 Tcout << Ttab << " 3. Execute AtFirst() method." << Tendl; 00143 Tcout << Ttab << " 4. Wait for trigger." << Tendl; 00144 Tcout << Ttab << " 5. Readout from modules according to the readout list." << Tendl; 00145 Tcout << Ttab << " 6. Execute AtLast() method." << Tendl; 00146 break; 00147 case tStatusJustRecordingEvent: 00148 Tcout << "STATUS" << Tendl; 00149 Tcout << Ttab << "JustRecordingEvent(" << theStatus << ")" << Tendl; 00150 Tcout << "DESCRIPTION" << Tendl; 00151 Tcout << Ttab << "Stacking the taken event record." << Tendl; 00152 break; 00153 case tStatusDead: 00154 Tcout << "STATUS" << Tendl; 00155 Tcout << Ttab << "Dead(" << theStatus << ")" << Tendl; 00156 Tcout << "DESCRIPTION" << Tendl; 00157 Tcout << Ttab << "Serious trouble." << Tendl; 00158 break; 00159 case tStatusUnknown: 00160 default: 00161 Tcout << "STATUS" << Tendl; 00162 Tcout << Ttab << "Unknown(" << theStatus << ")" << Tendl; 00163 Tcout << "DESCRIPTION" << Tendl; 00164 Tcout << Ttab << "Can not resolve status paramter." << Tendl; 00165 break; 00166 } 00167 00168 Tcout << "STACK" << Tendl; 00169 Tcout << Ttab << TRunManager::GetRunManager()->GetRun().GetEventStack().size() << "/" << theStackSize << Tendl; 00170 00171 Tcout << "NUMBER OF EVENTS" << Tendl; 00172 Tcout << Ttab << theNumberOfEvents << Tendl; 00173 00174 Tcout << "ID" << Tendl; 00175 Tcout << Ttab << theEvent.GetEventID() << Tendl; 00176 00177 //Tcout << "TIMER" << Tendl; 00178 //Tcout << Ttab << theEventTimer << Tendl; 00179 00180 if ( theEventAction != 0 ) { 00181 Tcout << "ACTION" << Tendl; 00182 Tcout << Ttab << typeid( *theEventAction ).name() << Tendl; 00183 } 00184 00185 return; 00186 } 00187 00188 Tvoid TEventManager::SetStackSize( Tint stacksize ) 00189 { 00190 TRunManager* manager = TRunManager::GetRunManager(); 00191 if ( manager -> GetStatus() == tStatusStandby ) { 00192 theStackSize = stacksize; 00193 } else { 00194 manager -> ShowStatus(); 00195 Tstring head = "TEventManager::SetStackSize: "; 00196 Tcerr << head << "if you (re)set stacking size, you must stop a this run." << Tendl; 00197 Tcerr << head << "nothing to do, return." << Tendl; 00198 } 00199 return; 00200 } 00201 00202 Tvoid TEventManager::SetEventAction( TEventAction* action ) 00203 { 00204 if ( theEventAction ) { 00205 Tstring head = "TEventManager::SetEventAction: "; 00206 delete theEventAction; 00207 Tcout << head << "EventAction was deleted." << Tendl; 00208 theEventAction = 0; 00209 } 00210 theEventAction = action; 00211 return; 00212 } 00213 00214 Tvoid TEventManager::Clear() 00215 { 00216 theNumberOfEvents = 0; 00217 theEvent.Clear(); 00218 theLastAccessID = -1; 00219 return; 00220 } 00221 00222 Tvoid TEventManager::ClearEventStack() 00223 { 00224 TouchNewestEvent(); 00225 TRunManager::GetRunManager()->GetRun().GetEventStack().clear(); 00226 return; 00227 } 00228 00229 Tint TEventManager::FindEvent( Tint id ) const 00230 { 00231 const TEventStack& stack = TRunManager::GetRunManager()->GetRun().GetEventStack(); 00232 Tint stacklen = (Tint)stack.size(); 00233 for ( Tint i = 0; i < stacklen; i ++ ) { 00234 if ( stack[ i ].GetEventID() == id ) { 00235 return i; 00236 } 00237 } 00238 return -1; 00239 } 00240 00241 Tbool TEventManager::HasEvent( Tint id ) const 00242 { 00243 if ( FindEvent( id ) >= 0 ) { 00244 return Ttrue; 00245 } else { 00246 return Tfalse; 00247 } 00248 } 00249 00250 Tvoid TEventManager::TouchNewestEvent() 00251 { 00252 GetNewestEvent(); 00253 return; 00254 } 00255 00256 TEvent TEventManager::GetNewestEvent() 00257 { 00258 const TEventStack& stack = TRunManager::GetRunManager()->GetRun().GetEventStack(); 00259 Tint stacklen = (Tint)stack.size(); 00260 Tint maxid = -1; 00261 for ( Tint i = 0; i < stacklen; i ++ ) { 00262 if ( stack[ i ].GetEventID() > maxid ) { 00263 maxid = i; 00264 } 00265 } 00266 theLastAccessID = maxid; 00267 return stack[ maxid ]; 00268 } 00269 00270 TEvent TEventManager::GetNextEvent() 00271 { 00272 Tint id = theLastAccessID + 1; 00273 return GetEvent( id ); 00274 } 00275 00276 TEvent TEventManager::GetLastEvent() 00277 { 00278 Tint id = theLastAccessID; 00279 return GetEvent( id ); 00280 } 00281 00282 TEvent TEventManager::GetEvent( Tint id ) 00283 { 00284 TEvent evt( -1 ); 00285 Tint pos = FindEvent( id ); 00286 if ( pos >= 0 ) { 00287 const TEventStack& stack = TRunManager::GetRunManager()->GetRun().GetEventStack(); 00288 evt = stack[ pos ]; 00289 theLastAccessID = id; 00290 } 00291 return evt; 00292 } 00293 00294 #ifdef __CLDAQ_ROOT_DLL 00295 ClassImp(TEventManager) 00296 #endif