source: trunk/MagicSoft/Mars/mbase/MEvtLoop.cc@ 1181

Last change on this file since 1181 was 1172, checked in by tbretz, 23 years ago
*** empty log message ***
File size: 8.8 KB
Line 
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
68ClassImp(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//
80MEvtLoop::MEvtLoop() : fParList(NULL), fProgress(NULL)
81{
82}
83
84// --------------------------------------------------------------------------
85//
86// default destructor - emty
87//
88MEvtLoop::~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//
99void 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//
109Bool_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 (!fTaskList->InheritsFrom("MTasklist"))
132 {
133 *fLog << err << dbginf << "'" << tlist << "' doesn't inherit from MTaskList!" << endl;
134 return kFALSE;
135 }
136
137 if (fLog != &gLog)
138 fParList->SetLogStream(fLog);
139
140 //
141 // execute the preprocess of all tasks
142 // connect the different tasks with the right containers in
143 // the parameter list
144 //
145 if (!fTaskList->PreProcess(fParList))
146 {
147 *fLog << err << "Error detected while PreProcessing" << endl;
148 return kFALSE;
149 }
150
151 *fLog << endl;
152
153 return kTRUE;
154}
155
156// --------------------------------------------------------------------------
157//
158// The processing part of the eventloop. Be careful, this is
159// for developers or use in special jobs only!
160//
161void MEvtLoop::Process(Int_t maxcnt) const
162{
163 //
164 // loop over all events and process all tasks for
165 // each event
166 //
167 *fLog << all <<"Eventloop running (";
168
169 if (maxcnt<0)
170 *fLog << "all";
171 else
172 *fLog << dec << maxcnt;
173
174 *fLog << " events)..." << flush;
175
176 if (fProgress && maxcnt>0)
177 fProgress->SetRange(0, maxcnt);
178
179 Int_t dummy = maxcnt<0 ? 0 : maxcnt;
180
181 //
182 // start a stopwatch
183 //
184 TStopwatch clock;
185 clock.Start();
186
187 //
188 // This is the MAIN EVENTLOOP which processes the data
189 // if maxcnt<0 the number of processed events is counted
190 // else only maxcnt events are processed
191 //
192 if (maxcnt<0)
193 // process first and increment if sucessfull
194 if (fProgress)
195 while (fTaskList->Process())
196 {
197 fProgress->SetPosition(++dummy);
198#if ROOT_VERSION_CODE < ROOT_VERSION(3,02,06)
199 gSystem->ProcessEvents();
200#else
201 gClient->ProcessEventsFor(fProgress);
202#endif
203 }
204 else
205 while (fTaskList->Process()) dummy++;
206 else
207 // check for number and break if unsuccessfull
208 if (fProgress)
209 while (dummy-- && fTaskList->Process())
210 {
211 fProgress->SetPosition(maxcnt - dummy);
212#if ROOT_VERSION_CODE < ROOT_VERSION(3,02,06)
213 gSystem->ProcessEvents();
214#else
215 gClient->ProcessEventsFor(fProgress);
216#endif
217 }
218 else
219 while (dummy-- && fTaskList->Process());
220
221 //
222 // stop stop-watch, print results
223 //
224 clock.Stop();
225
226 *fLog << all << "Ready!" << endl << endl;
227
228 *fLog << dec << endl << "CPU - "
229 << "Time: " << clock.CpuTime() << "s"
230 << " for " << (maxcnt<0?dummy:maxcnt) << " Events"
231 << " --> " << (maxcnt<0?dummy:maxcnt)/clock.CpuTime() << " Events/s"
232 << endl;
233 *fLog << "Real - "
234 << "Time: " << clock.RealTime() << "s"
235 << " for " << (maxcnt<0?dummy:maxcnt) << " Events"
236 << " --> " << (maxcnt<0?dummy:maxcnt)/clock.RealTime() << " Events/s"
237 << endl << endl;
238}
239
240// --------------------------------------------------------------------------
241//
242// The postprocessing part of the eventloop. Be careful, this is
243// for developers or use in special jobs only!
244//
245Bool_t MEvtLoop::PostProcess() const
246{
247 //
248 // execute the post process of all tasks
249 //
250 return fTaskList->PostProcess();
251}
252
253// --------------------------------------------------------------------------
254//
255// See class description above.
256//
257Bool_t MEvtLoop::Eventloop(Int_t maxcnt, const char *tlist)
258{
259 Bool_t rc = PreProcess();
260
261 //
262 // If all Tasks were PreProcesses successfully start Processing.
263 //
264 if (rc)
265 Process(maxcnt);
266
267 //
268 // Now postprocess all tasks. Only successfully preprocessed tasks are
269 // postprocessed. If the Postprocessing of one task fail return an error.
270 //
271 if (!PostProcess())
272 return kFALSE;
273
274 //
275 // If postprocessing of all preprocessed tasks was sucefully return rc.
276 // This gives an error in case the preprocessing has failed already.
277 // Otherwise the eventloop is considered: successfully.
278 //
279 return rc;
280}
281
Note: See TracBrowser for help on using the repository browser.