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

Last change on this file since 1540 was 1540, checked in by tbretz, 22 years ago
*** empty log message ***
File size: 20.9 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 "MReadTree.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//
193void 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 MReadTree *read = (MReadTree*)fTaskList->FindObject("MReadTree");
216 if (!read)
217 read = (MReadTree*)fTaskList->FindObject("MReadMarsFile");
218 if (read)
219 fProgress->SetRange(0, read->GetEntries());
220 }
221#endif
222 }
223
224 Int_t dummy = maxcnt<0 ? 0 : maxcnt;
225
226 //
227 // start a stopwatch
228 //
229 TStopwatch clock;
230 clock.Start();
231
232 //
233 // This is the MAIN EVENTLOOP which processes the data
234 // if maxcnt<0 the number of processed events is counted
235 // else only maxcnt events are processed
236 //
237 if (maxcnt<0)
238 // process first and increment if sucessfull
239 if (fProgress)
240 while (fTaskList->Process())
241 {
242 fProgress->SetPosition(++dummy);
243#if ROOT_VERSION_CODE < ROOT_VERSION(3,02,06)
244 gSystem->ProcessEvents();
245#else
246 gClient->ProcessEventsFor(fProgress);
247#endif
248 }
249 else
250 while (fTaskList->Process()) dummy++;
251 else
252 // check for number and break if unsuccessfull
253 if (fProgress)
254 while (dummy-- && fTaskList->Process())
255 {
256 fProgress->SetPosition(maxcnt - dummy);
257#if ROOT_VERSION_CODE < ROOT_VERSION(3,02,06)
258 gSystem->ProcessEvents();
259#else
260 gClient->ProcessEventsFor(fProgress);
261#endif
262 }
263 else
264 while (dummy-- && fTaskList->Process());
265
266 //
267 // stop stop-watch, print results
268 //
269 clock.Stop();
270
271 *fLog << all << "Ready!" << endl << endl;
272
273 *fLog << dec << endl << "CPU - "
274 << "Time: " << clock.CpuTime() << "s"
275 << " for " << (maxcnt<0?dummy:maxcnt) << " Events"
276 << " --> " << (maxcnt<0?dummy:maxcnt)/clock.CpuTime() << " Events/s"
277 << endl;
278 *fLog << "Real - "
279 << "Time: " << clock.RealTime() << "s"
280 << " for " << (maxcnt<0?dummy:maxcnt) << " Events"
281 << " --> " << (maxcnt<0?dummy:maxcnt)/clock.RealTime() << " Events/s"
282 << endl << endl;
283}
284
285// --------------------------------------------------------------------------
286//
287// The postprocessing part of the eventloop. Be careful, this is
288// for developers or use in special jobs only!
289//
290Bool_t MEvtLoop::PostProcess() const
291{
292 //
293 // execute the post process of all tasks
294 //
295 return fTaskList->PostProcess();
296}
297
298// --------------------------------------------------------------------------
299//
300// See class description above.
301//
302Bool_t MEvtLoop::Eventloop(Int_t maxcnt, const char *tlist)
303{
304 Bool_t rc = PreProcess();
305
306 //
307 // If all Tasks were PreProcesses successfully start Processing.
308 //
309 if (rc)
310 Process(maxcnt);
311
312 //
313 // Now postprocess all tasks. Only successfully preprocessed tasks are
314 // postprocessed. If the Postprocessing of one task fail return an error.
315 //
316 if (!PostProcess())
317 return kFALSE;
318
319 //
320 // If postprocessing of all preprocessed tasks was sucefully return rc.
321 // This gives an error in case the preprocessing has failed already.
322 // Otherwise the eventloop is considered: successfully.
323 //
324 return rc;
325}
326
327// --------------------------------------------------------------------------
328//
329// After you setup (or read) an Evtloop you can use this to write the
330// eventloop setup as a macro. The default name is "evtloop.C". The default
331// extension is .C If the extension is not given, .C is added.
332// I the last character in the argument is a '+' the file is not closed.
333// This is usefull if you have an eventloop which runs three times and
334// you want to write one macro. If the first character is a '+' no
335// opening is written, eg:
336//
337// MEvtLoop evtloop;
338// // some setup
339// evtloop.MakeMacro("mymacro+");
340// // replace the tasklist the first time
341// evtloop.MakeMacro("+mymacro+");
342// // replace the tasklist the second time
343// evtloop.MakeMacro("+mymacro");
344//
345void MEvtLoop::MakeMacro(const char *filename)
346{
347 TString name(filename);
348
349 name = name.Strip(TString::kBoth);
350
351 Bool_t open = kTRUE;
352 Bool_t close = kTRUE;
353 if (name[0]=='+')
354 {
355 open = kFALSE;
356 name.Remove(0, 1);
357 name = name.Strip(TString::kBoth);
358 }
359
360 if (name[name.Length()-1]=='+')
361 {
362 close = kFALSE;
363 name.Remove(name.Length()-1, 1);
364 name = name.Strip(TString::kBoth);
365 }
366
367 if (!name.EndsWith(".C"))
368 name += ".C";
369
370 ofstream fout;
371
372 if (!open)
373 {
374 fout.open(name, ios::app);
375 fout << endl;
376 fout << " // ----------------------------------------------------------------------" << endl;
377 fout << endl;
378 }
379 else
380 {
381 fout.open(name);
382
383 time_t t = time(NULL);
384 fout <<
385 "/* ======================================================================== *\\" << endl <<
386 "!" << endl <<
387 "! *" << endl <<
388 "! * This file is part of MARS, the MAGIC Analysis and Reconstruction" << endl <<
389 "! * Software. It is distributed to you in the hope that it can be a useful" << endl <<
390 "! * and timesaving tool in analysing Data of imaging Cerenkov telescopes." << endl <<
391 "! * It is distributed WITHOUT ANY WARRANTY." << endl <<
392 "! *" << endl <<
393 "! * Permission to use, copy, modify and distribute this software and its" << endl <<
394 "! * documentation for any purpose is hereby granted without fee," << endl <<
395 "! * provided that the above copyright notice appear in all copies and" << endl <<
396 "! * that both that copyright notice and this permission notice appear" << endl <<
397 "! * in supporting documentation. It is provided \"as is\" without express" << endl <<
398 "! * or implied warranty." << endl <<
399 "! *" << endl <<
400 "!" << endl <<
401 "!" << endl <<
402 "! Author(s): Thomas Bretz et al. <mailto:tbretz@astro.uni-wuerzburg.de>" << endl <<
403 "!" << endl <<
404 "! Copyright: MAGIC Software Development, 2000-2002" << endl <<
405 "!" << endl <<
406 "!" << endl <<
407 "\\* ======================================================================== */" << endl << endl <<
408 "// ------------------------------------------------------------------------" << endl <<
409 "//" << endl <<
410 "// This macro was automatically created on" << endl<<
411 "// " << ctime(&t) <<
412 "// with the MEvtLoop::MakeMacro tool." << endl <<
413 "//" << endl <<
414 "// ------------------------------------------------------------------------" << endl << endl <<
415 "void " << name(0, name.Length()-2) << "()" << endl <<
416 "{" << endl;
417 }
418
419 SavePrimitive(fout, (TString)"" + (open?"open":"") + (close?"close":""));
420
421 if (!close)
422 return;
423
424 fout << "}" << endl;
425
426 *fLog << inf << "Macro '" << name << "' written." << endl;
427}
428
429// --------------------------------------------------------------------------
430//
431// Implementation of SavePrimitive. Used to write the call to a constructor
432// to a macro. In the original root implementation it is used to write
433// gui elements to a macro-file.
434//
435
436void MEvtLoop::StreamPrimitive(ofstream &out) const
437{
438 out << " MEvtLoop " << GetUniqueName() << ";" << endl;
439}
440
441void MEvtLoop::SavePrimitive(ofstream &out, Option_t *opt)
442{
443 TString options = opt;
444 options.ToLower();
445
446 if (HasDuplicateNames("MEvtLoop::SavePrimitive"))
447 {
448 out << " // !" << endl;
449 out << " // ! WARNING - Your eventloop (MParList, MTaskList, ...) contains more than" << endl;
450 out << " // ! one object (MParContainer, MTask, ...) with the same name. The created macro" << endl;
451 out << " // ! may need manual intervention before it can be used." << endl;
452 out << " // !" << endl;
453 out << endl;
454 }
455
456 if (!options.Contains("open"))
457 {
458 if (gListOfPrimitives)
459 {
460 *fLog << err << "MEvtLoop::SavePrimitive - Error: old file not closed." << endl;
461 gListOfPrimitives->ForEach(TObject, ResetBit)(BIT(15));
462 delete gListOfPrimitives;
463 }
464 gListOfPrimitives = new TList;
465 }
466
467 if (fParList)
468 fParList->SavePrimitive(out);
469
470 MParContainer::SavePrimitive(out);
471
472 if (fParList)
473 out << " " << GetUniqueName() << ".SetParList(&" << fParList->GetUniqueName() << ");" << endl;
474 else
475 out << " // fParList empty..." << endl;
476 out << " if (!" << GetUniqueName() << ".Eventloop())" << endl;
477 out << " return;" << endl;
478
479 if (!options.Contains("close"))
480 return;
481
482 gListOfPrimitives->ForEach(TObject, ResetBit)(BIT(15));
483 delete gListOfPrimitives;
484 gListOfPrimitives = 0;
485}
486
487// --------------------------------------------------------------------------
488//
489// Get a list of all conmtainer names which are somehow part of the
490// eventloop. Chack for duplicate members and print a warning if
491// duplicates are found. Return kTRUE if duplicates are found, otherwise
492// kFALSE;
493//
494Bool_t MEvtLoop::HasDuplicateNames(TObjArray &arr, const TString txt) const
495{
496 arr.Sort();
497
498 TIter Next(&arr);
499 TObject *obj;
500 TString name;
501 Bool_t found = kFALSE;
502 while ((obj=Next()))
503 {
504 if (name==obj->GetName())
505 {
506 if (!found)
507 {
508 *fLog << warn << endl;
509 *fLog << " ! WARNING (" << txt << ")" << endl;
510 *fLog << " ! Your eventloop (MParList, MTaskList, ...) contains more than" << endl;
511 *fLog << " ! one object (MParContainer, MTask, ...) with the same name." << endl;
512 *fLog << " ! Creating a macro from it using MEvtLoop::MakeMacro may create" << endl;
513 *fLog << " ! a macro which needs manual intervention before it can be used." << endl;
514 found = kTRUE;
515 }
516 *fLog << " ! Please rename: " << obj->GetName() << endl;
517 }
518 name = obj->GetName();
519 }
520
521 return found;
522}
523
524// --------------------------------------------------------------------------
525//
526// Get a list of all conmtainer names which are somehow part of the
527// eventloop. Chack for duplicate members and print a warning if
528// duplicates are found. Return kTRUE if duplicates are found, otherwise
529// kFALSE;
530//
531Bool_t MEvtLoop::HasDuplicateNames(const TString txt) const
532{
533 if (!fParList)
534 return kFALSE;
535
536 TObjArray list;
537 list.SetOwner();
538
539 fParList->GetNames(list);
540
541 return HasDuplicateNames(list, txt);
542}
543
544// --------------------------------------------------------------------------
545//
546// Reads a saved eventloop from a file. The default name is "Evtloop".
547// Therefor an open file must exist (See TFile for more information)
548//
549// eg:
550// TFile file("myfile.root", "READ");
551// MEvtLoop evtloop;
552// evtloop.Read();
553// evtloop.MakeMacro("mymacro");
554//
555Int_t MEvtLoop::Read(const char *name)
556{
557 if (!gFile)
558 {
559 *fLog << err << "MEvtloop::Read: No file found. Please create a TFile first." << endl;
560 return 0;
561 }
562
563 if (!gFile->IsOpen())
564 {
565 *fLog << err << "MEvtloop::Read: File not open. Please open the TFile first." << endl;
566 return 0;
567 }
568
569 Int_t n = 0;
570 TObjArray list;
571
572 n += TObject::Read(name);
573
574 if (n==0)
575 {
576 *fLog << err << "MEvtloop::Read: No objects read." << endl;
577 return 0;
578 }
579
580 n += list.Read((TString)name+"_names");
581
582 fParList->SetNames(list);
583
584 HasDuplicateNames(list, "MEvtLoop::Read");
585
586 *fLog << inf << "Eventloop '" << name << "' read from file." << endl;
587
588 return n;
589}
590
591// --------------------------------------------------------------------------
592//
593// If available print the contents of the parameter list.
594//
595void MEvtLoop::Print(Option_t *opt) const
596{
597 if (fParList)
598 fParList->Print();
599 else
600 *fLog << all << "MEvtloop: No Parameter List available." << endl;
601}
602
603// --------------------------------------------------------------------------
604//
605// Writes a eventloop to a file. The default name is "Evtloop".
606// Therefor an open file must exist (See TFile for more information)
607//
608// eg:
609// TFile file("myfile.root", "RECREATE");
610// MEvtLoop evtloop;
611// evtloop.Write();
612// file.Close();
613//
614Int_t MEvtLoop::Write(const char *name, Int_t option, Int_t bufsize)
615{
616 if (!gFile)
617 {
618 *fLog << err << "MEvtloop::Write: No file found. Please create a TFile first." << endl;
619 return 0;
620 }
621
622 if (!gFile->IsOpen())
623 {
624 *fLog << err << "MEvtloop::Write: File not open. Please open the TFile first." << endl;
625 return 0;
626 }
627
628 if (!gFile->IsWritable())
629 {
630 *fLog << err << "MEvtloop::Write: File not writable." << endl;
631 return 0;
632 }
633
634 Int_t n = 0;
635
636 TObjArray list;
637 list.SetOwner();
638
639 fParList->GetNames(list);
640
641 n += TObject::Write(name, option, bufsize);
642
643 if (n==0)
644 {
645 *fLog << err << "MEvtloop::Read: No objects written." << endl;
646 return 0;
647 }
648
649 n += list.Write((TString)name+"_names", kSingleKey);
650
651 HasDuplicateNames(list, "MEvtLoop::Write");
652
653 *fLog << inf << "Eventloop written to file as " << name << "." << endl;
654
655 return n;
656}
Note: See TracBrowser for help on using the repository browser.