| 1 | /* ======================================================================== *\ | 
|---|
| 2 | ! | 
|---|
| 3 | ! * | 
|---|
| 4 | ! * This file is part of MARS, the MAGIC Analysis and Reconstruction | 
|---|
| 5 | ! * Software. It is distributed to you in the hope that it can be a useful | 
|---|
| 6 | ! * and timesaving tool in analysing Data of imaging Cerenkov telescopes. | 
|---|
| 7 | ! * It is distributed WITHOUT ANY WARRANTY. | 
|---|
| 8 | ! * | 
|---|
| 9 | ! * Permission to use, copy, modify and distribute this software and its | 
|---|
| 10 | ! * documentation for any purpose is hereby granted without fee, | 
|---|
| 11 | ! * provided that the above copyright notice appear in all copies and | 
|---|
| 12 | ! * that both that copyright notice and this permission notice appear | 
|---|
| 13 | ! * in supporting documentation. It is provided "as is" without express | 
|---|
| 14 | ! * or implied warranty. | 
|---|
| 15 | ! * | 
|---|
| 16 | ! | 
|---|
| 17 | ! | 
|---|
| 18 | !   Author(s): Thomas Bretz  12/2000 <mailto:tbretz@uni-sw.gwdg.de> | 
|---|
| 19 | ! | 
|---|
| 20 | !   Copyright: MAGIC Software Development, 2000-2001 | 
|---|
| 21 | ! | 
|---|
| 22 | ! | 
|---|
| 23 | \* ======================================================================== */ | 
|---|
| 24 |  | 
|---|
| 25 |  | 
|---|
| 26 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 27 | //                                                                          // | 
|---|
| 28 | // MEvtLoop                                                                 // | 
|---|
| 29 | //                                                                          // | 
|---|
| 30 | // This class is the core of each event processing.                         // | 
|---|
| 31 | // First you must set the parameter list to use. The parameter list         // | 
|---|
| 32 | // must contain the task list (MTaskList) to use. The name of the task      // | 
|---|
| 33 | // list can be specified if you call Eventloop. The standard name is        // | 
|---|
| 34 | // "MTaskList". The name you specify must match the name of the MTaskList   // | 
|---|
| 35 | // object.                                                                  // | 
|---|
| 36 | //                                                                          // | 
|---|
| 37 | // If you call Eventloop first all PreProcess functions - with the          // | 
|---|
| 38 | // parameter list as an argument - of the tasks in the task list are        // | 
|---|
| 39 | // executed. If one of them returns kFALSE then the execution is stopped.   // | 
|---|
| 40 | // If the preprocessing was ok. The Process funtion of the tasks are        // | 
|---|
| 41 | // as long as one function returns kSTOP. Only the tasks which are marked   // | 
|---|
| 42 | // marked as "All" or with a string which matches the MInputStreamID of     // | 
|---|
| 43 | // MTaskList are executed. If one tasks returns kCONTINUE the pending       // | 
|---|
| 44 | // tasks in the list are skipped and the execution in continued with        // | 
|---|
| 45 | // the first one in the list.                                               // | 
|---|
| 46 | // Afterwards the PostProcess functions are executed.                       // | 
|---|
| 47 | //                                                                          // | 
|---|
| 48 | //                                                                          // | 
|---|
| 49 | //  Maybe we can add a TProgressMeter sometimes later to be able to show    // | 
|---|
| 50 | //  the progress graphically...                                             // | 
|---|
| 51 | //                                                                          // | 
|---|
| 52 | //                                                                          // | 
|---|
| 53 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 54 | #include "MEvtLoop.h" | 
|---|
| 55 |  | 
|---|
| 56 | #include <iostream.h> | 
|---|
| 57 |  | 
|---|
| 58 | #include <TSystem.h> | 
|---|
| 59 | #include <TStopwatch.h> | 
|---|
| 60 | #include <TGProgressBar.h> | 
|---|
| 61 |  | 
|---|
| 62 | #include "MLog.h" | 
|---|
| 63 | #include "MLogManip.h" | 
|---|
| 64 |  | 
|---|
| 65 | #include "MParList.h" | 
|---|
| 66 | #include "MTaskList.h" | 
|---|
| 67 |  | 
|---|
| 68 | ClassImp(MEvtLoop); | 
|---|
| 69 |  | 
|---|
| 70 |  | 
|---|
| 71 | //! | 
|---|
| 72 | //! Maybe we can add a static parameter list to MEvtLoop | 
|---|
| 73 | //! Also we can derive MEvtLoop from MTaskList to have a static tasklist, too | 
|---|
| 74 | //! | 
|---|
| 75 |  | 
|---|
| 76 | // -------------------------------------------------------------------------- | 
|---|
| 77 | // | 
|---|
| 78 | // default constructor - emty | 
|---|
| 79 | // | 
|---|
| 80 | MEvtLoop::MEvtLoop() : fParList(NULL), fProgress(NULL) | 
|---|
| 81 | { | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | // -------------------------------------------------------------------------- | 
|---|
| 85 | // | 
|---|
| 86 | // default destructor - emty | 
|---|
| 87 | // | 
|---|
| 88 | MEvtLoop::~MEvtLoop() | 
|---|
| 89 | { | 
|---|
| 90 | if (TestBit(kIsOwner) && fParList) | 
|---|
| 91 | delete fParList; | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | // -------------------------------------------------------------------------- | 
|---|
| 95 | // | 
|---|
| 96 | //  if you set the Eventloop as owner the destructor of the given parameter | 
|---|
| 97 | //  list is calles by the destructor of MEvtLoop, otherwise not. | 
|---|
| 98 | // | 
|---|
| 99 | void MEvtLoop::SetOwner(Bool_t enable) | 
|---|
| 100 | { | 
|---|
| 101 | enable ? SetBit(kIsOwner) : ResetBit(kIsOwner); | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | // -------------------------------------------------------------------------- | 
|---|
| 105 | // | 
|---|
| 106 | // The proprocessing part of the eventloop. Be careful, this is | 
|---|
| 107 | // for developers or use in special jobs only! | 
|---|
| 108 | // | 
|---|
| 109 | Bool_t MEvtLoop::PreProcess(const char *tlist) | 
|---|
| 110 | { | 
|---|
| 111 | // | 
|---|
| 112 | // check if the needed parameter list is set. | 
|---|
| 113 | // | 
|---|
| 114 | if (!fParList) | 
|---|
| 115 | { | 
|---|
| 116 | *fLog << err << dbginf << "Parlist not initialized." << endl; | 
|---|
| 117 | return kFALSE; | 
|---|
| 118 | } | 
|---|
| 119 |  | 
|---|
| 120 | // | 
|---|
| 121 | //  check for the existance of the specified task list | 
|---|
| 122 | //  the default name is "MTaskList" | 
|---|
| 123 | // | 
|---|
| 124 | fTaskList = (MTaskList*)fParList->FindObject(tlist); | 
|---|
| 125 | if (!fTaskList) | 
|---|
| 126 | { | 
|---|
| 127 | *fLog << err << dbginf << "Cannot find tasklist '" << tlist << "' in parameter list." << endl; | 
|---|
| 128 | return kFALSE; | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | if (fLog != &gLog) | 
|---|
| 132 | fParList ->SetLogStream(fLog); | 
|---|
| 133 |  | 
|---|
| 134 | // | 
|---|
| 135 | //  execute the preprocess of all tasks | 
|---|
| 136 | //  connect the different tasks with the right containers in | 
|---|
| 137 | //  the parameter list | 
|---|
| 138 | // | 
|---|
| 139 | if (!fTaskList->PreProcess(fParList)) | 
|---|
| 140 | { | 
|---|
| 141 | *fLog << err << "Error detected while PreProcessing" << endl; | 
|---|
| 142 | return kFALSE; | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 | *fLog << endl; | 
|---|
| 146 |  | 
|---|
| 147 | return kTRUE; | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | // -------------------------------------------------------------------------- | 
|---|
| 151 | // | 
|---|
| 152 | // The processing part of the eventloop. Be careful, this is | 
|---|
| 153 | // for developers or use in special jobs only! | 
|---|
| 154 | // | 
|---|
| 155 | void MEvtLoop::Process(Int_t maxcnt) const | 
|---|
| 156 | { | 
|---|
| 157 | // | 
|---|
| 158 | //   loop over all events and process all tasks for | 
|---|
| 159 | //   each event | 
|---|
| 160 | // | 
|---|
| 161 | *fLog << all <<"Eventloop running ("; | 
|---|
| 162 |  | 
|---|
| 163 | if (maxcnt<0) | 
|---|
| 164 | *fLog << "all"; | 
|---|
| 165 | else | 
|---|
| 166 | *fLog << dec << maxcnt; | 
|---|
| 167 |  | 
|---|
| 168 | *fLog << " events)..." << flush; | 
|---|
| 169 |  | 
|---|
| 170 | if (fProgress && maxcnt>0) | 
|---|
| 171 | fProgress->SetRange(0, maxcnt); | 
|---|
| 172 |  | 
|---|
| 173 | Int_t dummy = maxcnt<0 ? 0 : maxcnt; | 
|---|
| 174 |  | 
|---|
| 175 | // | 
|---|
| 176 | // start a stopwatch | 
|---|
| 177 | // | 
|---|
| 178 | TStopwatch clock; | 
|---|
| 179 | clock.Start(); | 
|---|
| 180 |  | 
|---|
| 181 | // | 
|---|
| 182 | // This is the MAIN EVENTLOOP which processes the data | 
|---|
| 183 | // if maxcnt<0 the number of processed events is counted | 
|---|
| 184 | // else only maxcnt events are processed | 
|---|
| 185 | // | 
|---|
| 186 | if (maxcnt<0) | 
|---|
| 187 | // process first and increment if sucessfull | 
|---|
| 188 | if (fProgress) | 
|---|
| 189 | while (fTaskList->Process()) | 
|---|
| 190 | { | 
|---|
| 191 | fProgress->SetPosition(++dummy); | 
|---|
| 192 | gClient->ProcessEventsFor(fProgress); | 
|---|
| 193 | } | 
|---|
| 194 | else | 
|---|
| 195 | while (fTaskList->Process()) dummy++; | 
|---|
| 196 | else | 
|---|
| 197 | // check for number and break if unsuccessfull | 
|---|
| 198 | if (fProgress) | 
|---|
| 199 | while (dummy-- && fTaskList->Process()) | 
|---|
| 200 | { | 
|---|
| 201 | fProgress->SetPosition(maxcnt - dummy); | 
|---|
| 202 | gClient->ProcessEventsFor(fProgress); | 
|---|
| 203 | } | 
|---|
| 204 | else | 
|---|
| 205 | while (dummy-- && fTaskList->Process()); | 
|---|
| 206 |  | 
|---|
| 207 | // | 
|---|
| 208 | // stop stop-watch, print results | 
|---|
| 209 | // | 
|---|
| 210 | clock.Stop(); | 
|---|
| 211 |  | 
|---|
| 212 | *fLog << all << "Ready!" << endl << endl; | 
|---|
| 213 |  | 
|---|
| 214 | *fLog << dec << endl << "CPU  - " | 
|---|
| 215 | << "Time: " << clock.CpuTime() << "s" | 
|---|
| 216 | << " for " << (maxcnt<0?dummy:maxcnt) << " Events" | 
|---|
| 217 | << " --> " << (maxcnt<0?dummy:maxcnt)/clock.CpuTime() << " Events/s" | 
|---|
| 218 | << endl; | 
|---|
| 219 | *fLog << "Real - " | 
|---|
| 220 | << "Time: " << clock.RealTime() << "s" | 
|---|
| 221 | << " for " << (maxcnt<0?dummy:maxcnt) << " Events" | 
|---|
| 222 | << " --> " << (maxcnt<0?dummy:maxcnt)/clock.RealTime() << " Events/s" | 
|---|
| 223 | << endl << endl; | 
|---|
| 224 | } | 
|---|
| 225 |  | 
|---|
| 226 | // -------------------------------------------------------------------------- | 
|---|
| 227 | // | 
|---|
| 228 | //  The postprocessing part of the eventloop. Be careful, this is | 
|---|
| 229 | // for developers or use in special jobs only! | 
|---|
| 230 | // | 
|---|
| 231 | Bool_t MEvtLoop::PostProcess() const | 
|---|
| 232 | { | 
|---|
| 233 | // | 
|---|
| 234 | //  execute the post process of all tasks | 
|---|
| 235 | // | 
|---|
| 236 | return fTaskList->PostProcess(); | 
|---|
| 237 | } | 
|---|
| 238 |  | 
|---|
| 239 | // -------------------------------------------------------------------------- | 
|---|
| 240 | // | 
|---|
| 241 | //  See class description above. | 
|---|
| 242 | // | 
|---|
| 243 | Bool_t MEvtLoop::Eventloop(Int_t maxcnt, const char *tlist) | 
|---|
| 244 | { | 
|---|
| 245 | Bool_t rc = PreProcess(); | 
|---|
| 246 |  | 
|---|
| 247 | // | 
|---|
| 248 | // If all Tasks were PreProcesses successfully start Processing. | 
|---|
| 249 | // | 
|---|
| 250 | if (rc) | 
|---|
| 251 | Process(maxcnt); | 
|---|
| 252 |  | 
|---|
| 253 | // | 
|---|
| 254 | // Now postprocess all tasks. Only successfully preprocessed tasks are | 
|---|
| 255 | // postprocessed. If the Postprocessing of one task fail return an error. | 
|---|
| 256 | // | 
|---|
| 257 | if (!PostProcess()) | 
|---|
| 258 | return kFALSE; | 
|---|
| 259 |  | 
|---|
| 260 | // | 
|---|
| 261 | // If postprocessing of all preprocessed tasks was sucefully return rc. | 
|---|
| 262 | // This gives an error in case the preprocessing has failed already. | 
|---|
| 263 | // Otherwise the eventloop is considered: successfully. | 
|---|
| 264 | // | 
|---|
| 265 | return rc; | 
|---|
| 266 | } | 
|---|
| 267 |  | 
|---|