source: tags/Mars-V0.8/mbase/MEvtLoop.cc

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