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

Last change on this file since 1664 was 1657, checked in by tbretz, 22 years ago
*** empty log message ***
File size: 21.0 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// You can create a macro from a completely setup eventloop by: //
54// evtloop.MakeMacro("mymacro.C"); //
55// //
56// You will always need to check the macro, it will not run, but it //
57// should have al important information. //
58// //
59// //
60// You can also write all this information to a root file: //
61// TFile file("myfile.root"); //
62// evtloop.Write("MyEvtloopKey"); //
63// //
64// You can afterwards read the information from an open file by: //
65// evtloop.Read("MyEvtloopKey"); //
66// //
67// To lookup the information write it to a file using MakeMacro //
68// //
69//////////////////////////////////////////////////////////////////////////////
70#include "MEvtLoop.h"
71
72#include <time.h> // time_t
73#include <fstream.h> // ofstream, SavePrimitive
74#include <iostream.h>
75
76#include <TFile.h> // gFile
77#include <TSystem.h> // gSystem
78#include <TStopwatch.h>
79#include <TGProgressBar.h>
80
81#include "MLog.h"
82#include "MLogManip.h"
83
84#include "MParList.h"
85#include "MTaskList.h"
86#ifdef __MARS__
87#include "MRead.h" // for setting progress bar
88#include "MProgressBar.h" // MProgressBar::GetBar
89#endif
90
91ClassImp(MEvtLoop);
92
93
94//!
95//! Maybe we can add a static parameter list to MEvtLoop
96//! Also we can derive MEvtLoop from MTaskList to have a static tasklist, too
97//!
98
99TList *gListOfPrimitives; // forard declaration in MParContainer.h
100
101// --------------------------------------------------------------------------
102//
103// default constructor - emty
104//
105MEvtLoop::MEvtLoop() : fParList(NULL), fProgress(NULL)
106{
107 fName = "Evtloop";
108}
109
110// --------------------------------------------------------------------------
111//
112// default destructor - emty
113//
114MEvtLoop::~MEvtLoop()
115{
116 if (TestBit(kIsOwner) && fParList)
117 delete fParList;
118}
119
120// --------------------------------------------------------------------------
121//
122// if you set the Eventloop as owner the destructor of the given parameter
123// list is calles by the destructor of MEvtLoop, otherwise not.
124//
125void MEvtLoop::SetOwner(Bool_t enable)
126{
127 enable ? SetBit(kIsOwner) : ResetBit(kIsOwner);
128}
129
130#ifdef __MARS__
131// --------------------------------------------------------------------------
132//
133// Specify an existing MProgressBar object. It will display the progress
134// graphically. This will make thing about 1-2% slower.
135//
136void MEvtLoop::SetProgressBar(MProgressBar *bar)
137{
138 fProgress = bar->GetBar();
139}
140#endif
141
142// --------------------------------------------------------------------------
143//
144// The proprocessing part of the eventloop. Be careful, this is
145// for developers or use in special jobs only!
146//
147Bool_t MEvtLoop::PreProcess(const char *tlist)
148{
149 //
150 // check if the needed parameter list is set.
151 //
152 if (!fParList)
153 {
154 *fLog << err << dbginf << "Parlist not initialized." << endl;
155 return kFALSE;
156 }
157
158 //
159 // check for the existance of the specified task list
160 // the default name is "MTaskList"
161 //
162 fTaskList = (MTaskList*)fParList->FindObject(tlist, "MTaskList");
163 if (!fTaskList)
164 {
165 *fLog << err << dbginf << "Cannot find tasklist '" << tlist << "' in parameter list." << endl;
166 return kFALSE;
167 }
168
169 if (fLog != &gLog)
170 fParList->SetLogStream(fLog);
171
172 //
173 // execute the preprocess of all tasks
174 // connect the different tasks with the right containers in
175 // the parameter list
176 //
177 if (!fTaskList->PreProcess(fParList))
178 {
179 *fLog << err << "Error detected while PreProcessing" << endl;
180 return kFALSE;
181 }
182
183 *fLog << endl;
184
185 return kTRUE;
186}
187
188// --------------------------------------------------------------------------
189//
190// The processing part of the eventloop. Be careful, this is
191// for developers or use in special jobs only!
192//
193Bool_t MEvtLoop::Process(Int_t maxcnt) const
194{
195 //
196 // loop over all events and process all tasks for
197 // each event
198 //
199 *fLog << all <<"Eventloop running (";
200
201 if (maxcnt<0)
202 *fLog << "all";
203 else
204 *fLog << dec << maxcnt;
205
206 *fLog << " events)..." << flush;
207
208 if (fProgress)
209 {
210 if (maxcnt>0)
211 fProgress->SetRange(0, maxcnt);
212#ifdef __MARS__
213 else
214 {
215 MRead *read = (MRead*)fTaskList->FindObject("MRead");
216 if (read && read->GetEntries()>0)
217 fProgress->SetRange(0, read->GetEntries());
218 }
219#endif
220 }
221
222 Int_t dummy = maxcnt<0 ? 0 : maxcnt;
223
224 //
225 // start a stopwatch
226 //
227 TStopwatch clock;
228 clock.Start();
229
230 //
231 // This is the MAIN EVENTLOOP which processes the data
232 // if maxcnt<0 the number of processed events is counted
233 // else only maxcnt events are processed
234 //
235 Bool_t rc = kTRUE;
236 if (maxcnt<0)
237 // process first and increment if sucessfull
238 if (fProgress)
239 while ((rc=fTaskList->Process())==kTRUE)
240 {
241 fProgress->SetPosition(++dummy);
242#if ROOT_VERSION_CODE < ROOT_VERSION(3,02,06)
243 gSystem->ProcessEvents();
244#else
245 gClient->ProcessEventsFor(fProgress);
246#endif
247 }
248 else
249 while ((rc=fTaskList->Process())==kTRUE) dummy++;
250 else
251 // check for number and break if unsuccessfull
252 if (fProgress)
253 while (dummy-- && (rc=fTaskList->Process())==kTRUE)
254 {
255 fProgress->SetPosition(maxcnt - dummy);
256#if ROOT_VERSION_CODE < ROOT_VERSION(3,02,06)
257 gSystem->ProcessEvents();
258#else
259 gClient->ProcessEventsFor(fProgress);
260#endif
261 }
262 else
263 while (dummy-- && (rc=fTaskList->Process())==kTRUE);
264
265 //
266 // stop stop-watch, print results
267 //
268 clock.Stop();
269
270 *fLog << all << "Ready!" << endl << endl;
271
272 *fLog << dec << endl << "CPU - "
273 << "Time: " << clock.CpuTime() << "s"
274 << " for " << (maxcnt<0?dummy:maxcnt) << " Events"
275 << " --> " << (maxcnt<0?dummy:maxcnt)/clock.CpuTime() << " Events/s"
276 << endl;
277 *fLog << "Real - "
278 << "Time: " << clock.RealTime() << "s"
279 << " for " << (maxcnt<0?dummy:maxcnt) << " Events"
280 << " --> " << (maxcnt<0?dummy:maxcnt)/clock.RealTime() << " Events/s"
281 << endl << endl;
282
283 return rc!=kERROR;
284}
285
286// --------------------------------------------------------------------------
287//
288// The postprocessing part of the eventloop. Be careful, this is
289// for developers or use in special jobs only!
290//
291Bool_t MEvtLoop::PostProcess() const
292{
293 //
294 // execute the post process of all tasks
295 //
296 return fTaskList->PostProcess();
297}
298
299// --------------------------------------------------------------------------
300//
301// See class description above.
302//
303Bool_t MEvtLoop::Eventloop(Int_t maxcnt, const char *tlist)
304{
305 Bool_t rc = PreProcess();
306
307 //
308 // If all Tasks were PreProcesses successfully start Processing.
309 //
310 if (rc)
311 rc = Process(maxcnt);
312
313 //
314 // Now postprocess all tasks. Only successfully preprocessed tasks are
315 // postprocessed. If the Postprocessing of one task fail return an error.
316 //
317 if (!PostProcess())
318 return kFALSE;
319
320 //
321 // If postprocessing of all preprocessed tasks was sucefully return rc.
322 // This gives an error in case the preprocessing has failed already.
323 // Otherwise the eventloop is considered: successfully.
324 //
325 return rc;
326}
327
328// --------------------------------------------------------------------------
329//
330// After you setup (or read) an Evtloop you can use this to write the
331// eventloop setup as a macro. The default name is "evtloop.C". The default
332// extension is .C If the extension is not given, .C is added.
333// I the last character in the argument is a '+' the file is not closed.
334// This is usefull if you have an eventloop which runs three times and
335// you want to write one macro. If the first character is a '+' no
336// opening is written, eg:
337//
338// MEvtLoop evtloop;
339// // some setup
340// evtloop.MakeMacro("mymacro+");
341// // replace the tasklist the first time
342// evtloop.MakeMacro("+mymacro+");
343// // replace the tasklist the second time
344// evtloop.MakeMacro("+mymacro");
345//
346void MEvtLoop::MakeMacro(const char *filename)
347{
348 TString name(filename);
349
350 name = name.Strip(TString::kBoth);
351
352 Bool_t open = kTRUE;
353 Bool_t close = kTRUE;
354 if (name[0]=='+')
355 {
356 open = kFALSE;
357 name.Remove(0, 1);
358 name = name.Strip(TString::kBoth);
359 }
360
361 if (name[name.Length()-1]=='+')
362 {
363 close = kFALSE;
364 name.Remove(name.Length()-1, 1);
365 name = name.Strip(TString::kBoth);
366 }
367
368 if (!name.EndsWith(".C"))
369 name += ".C";
370
371 ofstream fout;
372
373 if (!open)
374 {
375 fout.open(name, ios::app);
376 fout << endl;
377 fout << " // ----------------------------------------------------------------------" << endl;
378 fout << endl;
379 }
380 else
381 {
382 fout.open(name);
383
384 time_t t = time(NULL);
385 fout <<
386 "/* ======================================================================== *\\" << endl <<
387 "!" << endl <<
388 "! *" << endl <<
389 "! * This file is part of MARS, the MAGIC Analysis and Reconstruction" << endl <<
390 "! * Software. It is distributed to you in the hope that it can be a useful" << endl <<
391 "! * and timesaving tool in analysing Data of imaging Cerenkov telescopes." << endl <<
392 "! * It is distributed WITHOUT ANY WARRANTY." << endl <<
393 "! *" << endl <<
394 "! * Permission to use, copy, modify and distribute this software and its" << endl <<
395 "! * documentation for any purpose is hereby granted without fee," << endl <<
396 "! * provided that the above copyright notice appear in all copies and" << endl <<
397 "! * that both that copyright notice and this permission notice appear" << endl <<
398 "! * in supporting documentation. It is provided \"as is\" without express" << endl <<
399 "! * or implied warranty." << endl <<
400 "! *" << endl <<
401 "!" << endl <<
402 "!" << endl <<
403 "! Author(s): Thomas Bretz et al. <mailto:tbretz@astro.uni-wuerzburg.de>" << endl <<
404 "!" << endl <<
405 "! Copyright: MAGIC Software Development, 2000-2002" << endl <<
406 "!" << endl <<
407 "!" << endl <<
408 "\\* ======================================================================== */" << endl << endl <<
409 "// ------------------------------------------------------------------------" << endl <<
410 "//" << endl <<
411 "// This macro was automatically created on" << endl<<
412 "// " << ctime(&t) <<
413 "// with the MEvtLoop::MakeMacro tool." << endl <<
414 "//" << endl <<
415 "// ------------------------------------------------------------------------" << endl << endl <<
416 "void " << name(0, name.Length()-2) << "()" << endl <<
417 "{" << endl;
418 }
419
420 SavePrimitive(fout, (TString)"" + (open?"open":"") + (close?"close":""));
421
422 if (!close)
423 return;
424
425 fout << "}" << endl;
426
427 *fLog << inf << "Macro '" << name << "' written." << endl;
428}
429
430// --------------------------------------------------------------------------
431//
432// Implementation of SavePrimitive. Used to write the call to a constructor
433// to a macro. In the original root implementation it is used to write
434// gui elements to a macro-file.
435//
436
437void MEvtLoop::StreamPrimitive(ofstream &out) const
438{
439 out << " MEvtLoop " << GetUniqueName() << ";" << endl;
440}
441
442void MEvtLoop::SavePrimitive(ofstream &out, Option_t *opt)
443{
444 TString options = opt;
445 options.ToLower();
446
447 if (HasDuplicateNames("MEvtLoop::SavePrimitive"))
448 {
449 out << " // !" << endl;
450 out << " // ! WARNING - Your eventloop (MParList, MTaskList, ...) contains more than" << endl;
451 out << " // ! one object (MParContainer, MTask, ...) with the same name. The created macro" << endl;
452 out << " // ! may need manual intervention before it can be used." << endl;
453 out << " // !" << endl;
454 out << endl;
455 }
456
457 if (!options.Contains("open"))
458 {
459 if (gListOfPrimitives)
460 {
461 *fLog << err << "MEvtLoop::SavePrimitive - Error: old file not closed." << endl;
462 gListOfPrimitives->ForEach(TObject, ResetBit)(BIT(15));
463 delete gListOfPrimitives;
464 }
465 gListOfPrimitives = new TList;
466 }
467
468 if (fParList)
469 fParList->SavePrimitive(out);
470
471 MParContainer::SavePrimitive(out);
472
473 if (fParList)
474 out << " " << GetUniqueName() << ".SetParList(&" << fParList->GetUniqueName() << ");" << endl;
475 else
476 out << " // fParList empty..." << endl;
477 out << " if (!" << GetUniqueName() << ".Eventloop())" << endl;
478 out << " return;" << endl;
479
480 if (!options.Contains("close"))
481 return;
482
483 gListOfPrimitives->ForEach(TObject, ResetBit)(BIT(15));
484 delete gListOfPrimitives;
485 gListOfPrimitives = 0;
486}
487
488// --------------------------------------------------------------------------
489//
490// Get a list of all conmtainer names which are somehow part of the
491// eventloop. Chack for duplicate members and print a warning if
492// duplicates are found. Return kTRUE if duplicates are found, otherwise
493// kFALSE;
494//
495Bool_t MEvtLoop::HasDuplicateNames(TObjArray &arr, const TString txt) const
496{
497 arr.Sort();
498
499 TIter Next(&arr);
500 TObject *obj;
501 TString name;
502 Bool_t found = kFALSE;
503 while ((obj=Next()))
504 {
505 if (name==obj->GetName())
506 {
507 if (!found)
508 {
509 *fLog << warn << endl;
510 *fLog << " ! WARNING (" << txt << ")" << endl;
511 *fLog << " ! Your eventloop (MParList, MTaskList, ...) contains more than" << endl;
512 *fLog << " ! one object (MParContainer, MTask, ...) with the same name." << endl;
513 *fLog << " ! Creating a macro from it using MEvtLoop::MakeMacro may create" << endl;
514 *fLog << " ! a macro which needs manual intervention before it can be used." << endl;
515 found = kTRUE;
516 }
517 *fLog << " ! Please rename: " << obj->GetName() << endl;
518 }
519 name = obj->GetName();
520 }
521
522 return found;
523}
524
525// --------------------------------------------------------------------------
526//
527// Get a list of all conmtainer names which are somehow part of the
528// eventloop. Chack for duplicate members and print a warning if
529// duplicates are found. Return kTRUE if duplicates are found, otherwise
530// kFALSE;
531//
532Bool_t MEvtLoop::HasDuplicateNames(const TString txt) const
533{
534 if (!fParList)
535 return kFALSE;
536
537 TObjArray list;
538 list.SetOwner();
539
540 fParList->GetNames(list);
541
542 return HasDuplicateNames(list, txt);
543}
544
545// --------------------------------------------------------------------------
546//
547// Reads a saved eventloop from a file. The default name is "Evtloop".
548// Therefor an open file must exist (See TFile for more information)
549//
550// eg:
551// TFile file("myfile.root", "READ");
552// MEvtLoop evtloop;
553// evtloop.Read();
554// evtloop.MakeMacro("mymacro");
555//
556Int_t MEvtLoop::Read(const char *name)
557{
558 if (!gFile)
559 {
560 *fLog << err << "MEvtloop::Read: No file found. Please create a TFile first." << endl;
561 return 0;
562 }
563
564 if (!gFile->IsOpen())
565 {
566 *fLog << err << "MEvtloop::Read: File not open. Please open the TFile first." << endl;
567 return 0;
568 }
569
570 Int_t n = 0;
571 TObjArray list;
572
573 n += TObject::Read(name);
574
575 if (n==0)
576 {
577 *fLog << err << "MEvtloop::Read: No objects read." << endl;
578 return 0;
579 }
580
581 n += list.Read((TString)name+"_names");
582
583 fParList->SetNames(list);
584
585 HasDuplicateNames(list, "MEvtLoop::Read");
586
587 *fLog << inf << "Eventloop '" << name << "' read from file." << endl;
588
589 return n;
590}
591
592// --------------------------------------------------------------------------
593//
594// If available print the contents of the parameter list.
595//
596void MEvtLoop::Print(Option_t *opt) const
597{
598 if (fParList)
599 fParList->Print();
600 else
601 *fLog << all << "MEvtloop: No Parameter List available." << endl;
602}
603
604// --------------------------------------------------------------------------
605//
606// Writes a eventloop to a file. The default name is "Evtloop".
607// Therefor an open file must exist (See TFile for more information)
608//
609// eg:
610// TFile file("myfile.root", "RECREATE");
611// MEvtLoop evtloop;
612// evtloop.Write();
613// file.Close();
614//
615Int_t MEvtLoop::Write(const char *name, Int_t option, Int_t bufsize)
616{
617 if (!gFile)
618 {
619 *fLog << err << "MEvtloop::Write: No file found. Please create a TFile first." << endl;
620 return 0;
621 }
622
623 if (!gFile->IsOpen())
624 {
625 *fLog << err << "MEvtloop::Write: File not open. Please open the TFile first." << endl;
626 return 0;
627 }
628
629 if (!gFile->IsWritable())
630 {
631 *fLog << err << "MEvtloop::Write: File not writable." << endl;
632 return 0;
633 }
634
635 Int_t n = 0;
636
637 TObjArray list;
638 list.SetOwner();
639
640 fParList->GetNames(list);
641
642 n += TObject::Write(name, option, bufsize);
643
644 if (n==0)
645 {
646 *fLog << err << "MEvtloop::Read: No objects written." << endl;
647 return 0;
648 }
649
650 n += list.Write((TString)name+"_names", kSingleKey);
651
652 HasDuplicateNames(list, "MEvtLoop::Write");
653
654 *fLog << inf << "Eventloop written to file as " << name << "." << endl;
655
656 return n;
657}
Note: See TracBrowser for help on using the repository browser.