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

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