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

Last change on this file since 5723 was 5382, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 30.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@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2003
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 function of the tasks are
41// executed as long as one function returns kSTOP. Only the tasks which
42// are marked as "All" or with a string which matches the MInputStreamID
43// of 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// If you want to display the progress in a gui you can use SetProgressBar
49// and a TGProgressBar or a MProgressBar. If you set a MStatusDisplay
50// using SetDisplay, the Progress bar from this display is used.
51//
52// You can create a macro from a completely setup eventloop by:
53// evtloop.MakeMacro("mymacro.C");
54//
55// You will always need to check the macro, it will not run, but it
56// should have al important information.
57//
58//
59// You can also write all this information to a root file:
60// TFile file("myfile.root");
61// evtloop.Write("MyEvtloopKey");
62//
63// You can afterwards read the information from an open file by:
64// evtloop.Read("MyEvtloopKey");
65//
66// To lookup the information write it to a file using MakeMacro
67//
68//////////////////////////////////////////////////////////////////////////////
69#include "MEvtLoop.h"
70
71#include <time.h> // time_t
72#include <fstream> // ofstream, SavePrimitive
73
74#include <TEnv.h> // TEnv
75#include <TRint.h> // gApplication, TRint::Class()
76#include <TTime.h> // TTime
77#include <TFile.h> // gFile
78#include <TThread.h> // TThread::Self()
79#include <TDatime.h> // TDatime
80#include <TSystem.h> // gSystem
81#include <TStopwatch.h>
82#include <TGProgressBar.h>
83
84#include "MLog.h"
85#include "MLogManip.h"
86
87#include "MParList.h"
88#include "MTaskList.h"
89#ifdef __MARS__
90#include "MRead.h" // for setting progress bar
91#include "MProgressBar.h" // MProgressBar::GetBar
92#include "MStatusDisplay.h" // MStatusDisplay::GetBar
93#endif
94
95ClassImp(MEvtLoop);
96
97using namespace std;
98
99// --------------------------------------------------------------------------
100//
101// default constructor
102//
103MEvtLoop::MEvtLoop(const char *name) : fParList(NULL), fProgress(NULL)
104{
105 fName = name;
106
107 gROOT->GetListOfCleanups()->Add(this); // To remove fDisplay
108 SetBit(kMustCleanup);
109
110 *fLog << inf << underline << "Instantiated MEvtLoop (" << name << "), using ROOT v" << ROOTVER << endl;
111}
112
113// --------------------------------------------------------------------------
114//
115// destructor
116//
117MEvtLoop::~MEvtLoop()
118{
119 if (TestBit(kIsOwner) && fParList)
120 delete fParList;
121}
122
123void MEvtLoop::SetParList(MParList *p)
124{
125 if (!p)
126 return;
127
128 p->SetBit(kMustCleanup);
129 fParList = p;
130}
131
132// --------------------------------------------------------------------------
133//
134// If the evntloop knows its tasklist search for the task there,
135// otherwise return NULL.
136//
137MTask *MEvtLoop::FindTask(const char *name) const
138{
139 return fTaskList ? fTaskList->FindTask(name) : NULL;
140}
141
142// --------------------------------------------------------------------------
143//
144// If the evntloop knows its tasklist search for the task there,
145// otherwise return NULL.
146//
147MTask *MEvtLoop::FindTask(const MTask *obj) const
148{
149 return fTaskList ? fTaskList->FindTask(obj) : NULL;
150}
151
152// --------------------------------------------------------------------------
153//
154// if you set the Eventloop as owner the destructor of the given parameter
155// list is calles by the destructor of MEvtLoop, otherwise not.
156//
157void MEvtLoop::SetOwner(Bool_t enable)
158{
159 enable ? SetBit(kIsOwner) : ResetBit(kIsOwner);
160}
161
162void MEvtLoop::SetProgressBar(TGProgressBar *bar)
163{
164 fProgress = bar;
165 if (fProgress)
166 fProgress->SetBit(kMustCleanup);
167}
168
169#ifdef __MARS__
170// --------------------------------------------------------------------------
171//
172// Specify an existing MProgressBar object. It will display the progress
173// graphically. This will make thing about 1-2% slower.
174//
175void MEvtLoop::SetProgressBar(MProgressBar *bar)
176{
177 SetProgressBar(bar->GetBar());
178}
179#endif
180
181void MEvtLoop::SetDisplay(MStatusDisplay *d)
182{
183 MParContainer::SetDisplay(d);
184 if (!d)
185 fProgress=NULL;
186 else
187 {
188 d->SetBit(kMustCleanup);
189
190 // Get pointer to update Progress bar
191 fProgress = fDisplay->GetBar();
192 }
193
194 if (fParList)
195 fParList->SetDisplay(d);
196}
197
198// --------------------------------------------------------------------------
199//
200// The proprocessing part of the eventloop. Be careful, this is
201// for developers or use in special jobs only!
202//
203Bool_t MEvtLoop::PreProcess(const char *tlist)
204{
205 fTaskList = NULL;
206
207 //
208 // check if the needed parameter list is set.
209 //
210 if (!fParList)
211 {
212 *fLog << err << dbginf << "Parlist not initialized." << endl;
213 return kFALSE;
214 }
215
216 //
217 // check for the existance of the specified task list
218 // the default name is "MTaskList"
219 //
220 fTaskList = (MTaskList*)fParList->FindObject(tlist, "MTaskList");
221 if (!fTaskList)
222 {
223 *fLog << err << dbginf << "Cannot find tasklist '" << tlist << "' in parameter list." << endl;
224 return kFALSE;
225 }
226
227 if (fLog != &gLog)
228 fParList->SetLogStream(fLog);
229
230#ifdef __MARS__
231 //
232 // Check whether display is still existing
233 //
234 if (fDisplay)
235 {
236 // Lock display to prevent user from deleting it
237 fDisplay->Lock();
238 // Don't display context menus
239 fDisplay->SetNoContextMenu();
240 // Set window and icon name
241 fDisplay->SetWindowName(TString("Status Display: ")+fName);
242 fDisplay->SetIconName(fName);
243 // Start automatic update
244 fDisplay->StartUpdate();
245 // Cascade display through childs
246 fParList->SetDisplay(fDisplay);
247 }
248#endif
249
250 //
251 // execute the preprocess of all tasks
252 // connect the different tasks with the right containers in
253 // the parameter list
254 //
255 if (!fTaskList->PreProcess(fParList))
256 {
257 *fLog << err << "Error detected while PreProcessing." << endl;
258 return kFALSE;
259 }
260
261 *fLog << endl;
262
263 return kTRUE;
264}
265
266Bool_t MEvtLoop::ProcessGuiEvents(Int_t num)
267{
268 if (gROOT->IsBatch())
269 return kTRUE;
270
271 //
272 // Check status of display
273 //
274 Bool_t rc = kTRUE;
275
276 if (fDisplay)
277 switch (fDisplay->CheckStatus())
278 {
279 case MStatusDisplay::kLoopNone:
280 break;
281 case MStatusDisplay::kLoopStop:
282 rc = kFALSE;
283 fDisplay->ClearStatus();
284 break;
285 //
286 // If the display is not on the heap (means: not created
287 // with the new operator) the object is deleted somewhere
288 // else in the code. It is the responsibility of the
289 // application which instantiated the object to make
290 // sure that the correct action is taken. This can be
291 // done by calling MStatusDisplay::CheckStatus()
292 //
293 // Because we are synchronous we can safely delete it here!
294 //
295 // Close means: Close the display but leave analysis running
296 // Exit means: Close the display and stop analysis
297 //
298 case MStatusDisplay::kFileClose:
299 case MStatusDisplay::kFileExit:
300 rc = fDisplay->CheckStatus() == MStatusDisplay::kFileClose;
301
302 if (fDisplay->IsOnHeap())
303 delete fDisplay;
304
305 //
306 // This makes the display really disappear physically on
307 // the screen in case of MStatusDisplay::kFileClose
308 //
309 gSystem->ProcessEvents();
310
311 return rc;
312 default:
313 *fLog << warn << "MEvtloop: fDisplay->CheckStatus() has returned unknown status #" << fDisplay->CheckStatus() << "... cleared." << endl;
314 fDisplay->ClearStatus();
315 break;
316 }
317
318 //
319 // Check System time (don't loose too much time by updating the GUI)
320 //
321
322 // FIXME: Not thread safe (if you have more than one eventloop running)
323 static Int_t start = num;
324 static TTime t1 = gSystem->Now();
325 static TTime t2 = t1;
326
327 //
328 // No update < 20ms
329 //
330 const TTime t0 = gSystem->Now();
331 if (t0-t1 < (TTime)20)
332 return rc;
333 t1 = t0;
334
335 //
336 // Update current speed each 1.5 second
337 //
338 if (fDisplay && t0-t2>(TTime)1500)
339 {
340 const Float_t speed = 1000.*(num-start)/(long int)(t0-t2);
341 TString txt = "Processing...";
342 if (speed>0)
343 {
344 txt += " (";
345 txt += (Int_t)speed;
346 txt += "Evts/s";
347 if (fNumEvents>0)
348 {
349 txt += ", est: ";
350 txt += (int)((fNumEvents-num)/speed/60)+1;
351 txt += "min";
352 }
353 //txt += (int)fmod(entries/(1000.*(num-start)/(long int)(t0-t2)), 60);
354 //txt += "s";
355 txt += ")";
356 }
357 fDisplay->SetStatusLine1(txt);
358 start = num;
359 t2 = t0;
360 }
361
362 //
363 // Set new progress bar position
364 //
365 if (fProgress && fNumEvents>0)
366 fProgress->SetPosition((Double_t)num/fNumEvents);
367
368 // FIXME: This is a workaround, because TApplication::Run is not
369 // thread safe against ProcessEvents. We assume, that if
370 // we are not in the Main-Thread ProcessEvents() is
371 // called by the TApplication Event Loop...
372 if (!TThread::Self()/*gApplication->InheritsFrom(TRint::Class())*/)
373 {
374 //
375 // Handle GUI events (display changes)
376 //
377#if ROOT_VERSION_CODE < ROOT_VERSION(3,02,06)
378 gSystem->ProcessEvents();
379#else
380 if (fDisplay)
381 gSystem->ProcessEvents();
382 else
383 if (fProgress)
384 gClient->ProcessEventsFor(fProgress);
385#endif
386 }
387
388 return rc;
389}
390
391// --------------------------------------------------------------------------
392//
393// The processing part of the eventloop. Be careful, this is
394// for developers or use in special jobs only!
395//
396Int_t MEvtLoop::Process(UInt_t maxcnt)
397{
398 if (!fTaskList)
399 return kFALSE;
400
401 //
402 // loop over all events and process all tasks for
403 // each event
404 //
405 *fLog << all <<"Eventloop running (";
406
407 if (maxcnt==0)
408 *fLog << "all";
409 else
410 *fLog << dec << maxcnt;
411
412 *fLog << " events)..." << flush;
413
414 UInt_t entries = kMaxUInt;
415 fNumEvents = 0;
416
417 if (fProgress && !gROOT->IsBatch())
418 {
419 fProgress->Reset();
420 fProgress->SetRange(0, 1);
421
422#ifdef __MARS__
423 MRead *read = (MRead*)fTaskList->FindObject("MRead");
424 if (read && read->GetEntries()>0)
425 entries = read->GetEntries();
426#endif
427
428 if (maxcnt>0)
429 fNumEvents = TMath::Min(maxcnt, entries);
430 else
431 if (entries!=kMaxUInt)
432 fNumEvents = entries;
433 }
434
435 if (fDisplay)
436 {
437 fDisplay->SetStatusLine1("Processing...");
438 fDisplay->SetStatusLine2("");
439 }
440
441 //
442 // start a stopwatch
443 //
444 TStopwatch clock;
445 clock.Start();
446
447 //
448 // This is the MAIN EVENTLOOP which processes the data
449 // if maxcnt==0 the number of processed events is counted
450 // else only maxcnt events are processed
451 //
452 UInt_t numcnts = 0;
453 UInt_t dummy = maxcnt;
454
455 Int_t rc=kTRUE;
456 if (maxcnt==0)
457 // process first and increment if sucessfull
458 while (1)
459 {
460 rc=fTaskList->Process();
461 if (rc!=kTRUE && rc!=kCONTINUE)
462 break;
463
464 numcnts++;
465 if (!ProcessGuiEvents(++dummy))
466 break;
467 }
468 else
469 // check for number and break if unsuccessfull
470 while (dummy--)
471 {
472 rc=fTaskList->Process();
473 if (rc!=kTRUE && rc!=kCONTINUE)
474 break;
475
476 numcnts++;
477 if (!ProcessGuiEvents(maxcnt - dummy))
478 break;
479 }
480
481 //
482 // stop stop-watch, print results
483 //
484 clock.Stop();
485
486 if (fProgress && !gROOT->IsBatch())
487 {
488 //fProgress->SetPosition(maxcnt>0 ? TMath::Min(maxcnt, entries) : entries);
489 fProgress->SetPosition(1);
490
491 // FIXME: This is a workaround, because TApplication::Run is not
492 // thread safe against ProcessEvents. We assume, that if
493 // we are not in the Main-Thread ProcessEvents() is
494 // called by the TApplication Event Loop...
495 if (!TThread::Self()/*gApplication->InheritsFrom(TRint::Class())*/)
496 {
497#if ROOT_VERSION_CODE < ROOT_VERSION(3,02,06)
498 gSystem->ProcessEvents();
499#else
500 gClient->ProcessEventsFor(fDisplay ? fDisplay->GetBar() : fProgress);
501#endif
502 }
503 }
504
505 *fLog << all << "Ready!" << endl << endl;
506
507 *fLog << dec << endl << "CPU - Time: ";
508 *fLog << clock.CpuTime() << "s" << " for " << numcnts << " Events";
509 if (numcnts>0)
510 *fLog << " --> " << numcnts/clock.CpuTime() << " Events/s";
511 *fLog << endl << "Real - Time: ";
512 *fLog << clock.RealTime() << "s" << " for " << numcnts << " Events";
513 if (numcnts>0)
514 *fLog << " --> " << numcnts/clock.RealTime() << " Events/s";
515
516 *fLog << endl << endl;
517
518 return rc!=kERROR;
519}
520
521// --------------------------------------------------------------------------
522//
523// The postprocessing part of the eventloop. Be careful, this is
524// for developers or use in special jobs only!
525//
526Bool_t MEvtLoop::PostProcess() const
527{
528 //
529 // execute the post process of all tasks
530 //
531 return fTaskList ? fTaskList->PostProcess() : kTRUE;
532}
533
534// --------------------------------------------------------------------------
535//
536// See class description above. Returns kTRUE if PreProcessing,
537// Processing and PostProcessing was successfull.
538// kFALSE is retuned if something was not successfull, eg:
539// PreProcess or PostProcess returned kFALSE
540// process returned kERRR
541//
542// maxcnt==0 means: all events
543// tlist is the name of the task-list to be used. Be carefull, this
544// feature is not finally implemented - it will only work if no
545// task will access the tasklist.
546//
547Bool_t MEvtLoop::Eventloop(UInt_t maxcnt, const char *tlist)
548{
549 TDatime d;
550 *fLog << inf << underline << "Eventloop: " << fName << " started at " << d.AsString() << endl;
551
552 Bool_t rc = PreProcess(tlist);
553
554 //
555 // If all Tasks were PreProcesses successfully start Processing.
556 //
557 if (rc)
558 rc = Process(maxcnt);
559
560 //
561 // Now postprocess all tasks. Only successfully preprocessed tasks
562 // are postprocessed. If the Postprocessing of one task fails
563 // return an error.
564 //
565 if (!PostProcess())
566 {
567 *fLog << err << "Error detected while PostProcessing." << endl;
568 rc = kFALSE;
569 }
570
571 if (!fDisplay)
572 return rc;
573
574 // Set status lines
575 fDisplay->SetStatusLine1(fName);
576 fDisplay->SetStatusLine2(rc ? "Done." : "Error!");
577 // Stop automatic update
578 fDisplay->StopUpdate();
579 // Reallow context menus
580 fDisplay->SetNoContextMenu(kFALSE);
581 // Reallow user to exit window by File menu
582 fDisplay->UnLock();
583
584 //
585 // If postprocessing of all preprocessed tasks was sucefully return rc.
586 // This gives an error in case the preprocessing has failed already.
587 // Otherwise the eventloop is considered: successfully.
588 //
589 return rc;
590}
591
592// --------------------------------------------------------------------------
593//
594// After you setup (or read) an Evtloop you can use MakeMacro() to write
595// the eventloop setup as a macro. The default name is "evtloop.C". The
596// default extension is .C If the extension is not given, .C is added.
597// If the last character in the argument is a '+' the file is not closed.
598// This is usefull if you have an eventloop which runs three times and
599// you want to write one macro. If the first character is a '+' no
600// opening is written, eg:
601//
602// MEvtLoop evtloop;
603// // some setup
604// evtloop.MakeMacro("mymacro+");
605// // replace the tasklist the first time
606// evtloop.MakeMacro("+mymacro+");
607// // replace the tasklist the second time
608// evtloop.MakeMacro("+mymacro");
609//
610void MEvtLoop::MakeMacro(const char *filename)
611{
612 TString name(filename);
613
614 name = name.Strip(TString::kBoth);
615
616 Bool_t open = kTRUE;
617 Bool_t close = kTRUE;
618 if (name[0]=='+')
619 {
620 open = kFALSE;
621 name.Remove(0, 1);
622 name = name.Strip(TString::kBoth);
623 }
624
625 if (name[name.Length()-1]=='+')
626 {
627 close = kFALSE;
628 name.Remove(name.Length()-1, 1);
629 name = name.Strip(TString::kBoth);
630 }
631
632 if (!name.EndsWith(".C"))
633 name += ".C";
634
635 ofstream fout;
636
637 if (!open)
638 {
639 fout.open(name, ios::app);
640 fout << endl;
641 fout << " // ----------------------------------------------------------------------" << endl;
642 fout << endl;
643 }
644 else
645 {
646 fout.open(name);
647
648 time_t t = time(NULL);
649 fout <<
650 "/* ======================================================================== *\\" << endl <<
651 "!" << endl <<
652 "! *" << endl <<
653 "! * This file is part of MARS, the MAGIC Analysis and Reconstruction" << endl <<
654 "! * Software. It is distributed to you in the hope that it can be a useful" << endl <<
655 "! * and timesaving tool in analysing Data of imaging Cerenkov telescopes." << endl <<
656 "! * It is distributed WITHOUT ANY WARRANTY." << endl <<
657 "! *" << endl <<
658 "! * Permission to use, copy, modify and distribute this software and its" << endl <<
659 "! * documentation for any purpose is hereby granted without fee," << endl <<
660 "! * provided that the above copyright notice appear in all copies and" << endl <<
661 "! * that both that copyright notice and this permission notice appear" << endl <<
662 "! * in supporting documentation. It is provided \"as is\" without express" << endl <<
663 "! * or implied warranty." << endl <<
664 "! *" << endl <<
665 "!" << endl <<
666 "!" << endl <<
667 "! Author(s): Thomas Bretz et al. <mailto:tbretz@astro.uni-wuerzburg.de>" << endl <<
668 "!" << endl <<
669 "! Copyright: MAGIC Software Development, 2000-2002" << endl <<
670 "!" << endl <<
671 "!" << endl <<
672 "\\* ======================================================================== */" << endl << endl <<
673 "// ------------------------------------------------------------------------" << endl <<
674 "//" << endl <<
675 "// This macro was automatically created on" << endl<<
676 "// " << ctime(&t) <<
677 "// with the MEvtLoop::MakeMacro tool." << endl <<
678 "//" << endl <<
679 "// ------------------------------------------------------------------------" << endl << endl <<
680 "void " << name(0, name.Length()-2) << "()" << endl <<
681 "{" << endl;
682 }
683
684 SavePrimitive(fout, (TString)"" + (open?"open":"") + (close?"close":""));
685
686 if (!close)
687 return;
688
689 fout << "}" << endl;
690
691 *fLog << inf << "Macro '" << name << "' written." << endl;
692}
693
694// --------------------------------------------------------------------------
695//
696// Implementation of SavePrimitive. Used to write the call to a constructor
697// to a macro. In the original root implementation it is used to write
698// gui elements to a macro-file.
699//
700void MEvtLoop::StreamPrimitive(ofstream &out) const
701{
702 out << " MEvtLoop " << GetUniqueName();
703 if (fName!="Evtloop")
704 out << "(\"" << fName << "\")";
705 out << ";" << endl;
706}
707
708// --------------------------------------------------------------------------
709//
710//
711void MEvtLoop::SavePrimitive(ofstream &out, Option_t *opt)
712{
713 TString options = opt;
714 options.ToLower();
715
716 if (HasDuplicateNames("MEvtLoop::SavePrimitive"))
717 {
718 out << " // !" << endl;
719 out << " // ! WARNING - Your eventloop (MParList, MTaskList, ...) contains more than" << endl;
720 out << " // ! one object (MParContainer, MTask, ...) with the same name. The created macro" << endl;
721 out << " // ! may need manual intervention before it can be used." << endl;
722 out << " // !" << endl;
723 out << endl;
724 }
725
726 if (!options.Contains("open"))
727 {
728 if (gListOfPrimitives)
729 {
730 *fLog << err << "MEvtLoop::SavePrimitive - Error: old file not closed." << endl;
731 gListOfPrimitives->ForEach(TObject, ResetBit)(BIT(15));
732 delete gListOfPrimitives;
733 }
734 gListOfPrimitives = new TList;
735 }
736
737 if (fParList)
738 fParList->SavePrimitive(out);
739
740 MParContainer::SavePrimitive(out);
741
742 if (fParList)
743 out << " " << GetUniqueName() << ".SetParList(&" << fParList->GetUniqueName() << ");" << endl;
744 else
745 out << " // fParList empty..." << endl;
746 out << " if (!" << GetUniqueName() << ".Eventloop())" << endl;
747 out << " return;" << endl;
748
749 if (!options.Contains("close"))
750 return;
751
752 gListOfPrimitives->ForEach(TObject, ResetBit)(BIT(15));
753 delete gListOfPrimitives;
754 gListOfPrimitives = 0;
755}
756
757// --------------------------------------------------------------------------
758//
759// Get a list of all conmtainer names which are somehow part of the
760// eventloop. Chack for duplicate members and print a warning if
761// duplicates are found. Return kTRUE if duplicates are found, otherwise
762// kFALSE;
763//
764Bool_t MEvtLoop::HasDuplicateNames(TObjArray &arr, const TString txt) const
765{
766 arr.Sort();
767
768 TIter Next(&arr);
769 TObject *obj;
770 TString name;
771 Bool_t found = kFALSE;
772 while ((obj=Next()))
773 {
774 if (name==obj->GetName())
775 {
776 if (!found)
777 {
778 *fLog << warn << endl;
779 *fLog << " ! WARNING (" << txt << ")" << endl;
780 *fLog << " ! Your eventloop (MParList, MTaskList, ...) contains more than" << endl;
781 *fLog << " ! one object (MParContainer, MTask, ...) with the same name." << endl;
782 *fLog << " ! Creating a macro from it using MEvtLoop::MakeMacro may create" << endl;
783 *fLog << " ! a macro which needs manual intervention before it can be used." << endl;
784 found = kTRUE;
785 }
786 *fLog << " ! Please rename: " << obj->GetName() << endl;
787 }
788 name = obj->GetName();
789 }
790
791 return found;
792}
793
794// --------------------------------------------------------------------------
795//
796// Get a list of all conmtainer names which are somehow part of the
797// eventloop. Chack for duplicate members and print a warning if
798// duplicates are found. Return kTRUE if duplicates are found, otherwise
799// kFALSE;
800//
801Bool_t MEvtLoop::HasDuplicateNames(const TString txt) const
802{
803 if (!fParList)
804 return kFALSE;
805
806 TObjArray list;
807 list.SetOwner();
808
809 fParList->GetNames(list);
810
811 return HasDuplicateNames(list, txt);
812}
813
814// --------------------------------------------------------------------------
815//
816// Reads a saved eventloop from a file. The default name is "Evtloop".
817// Therefor an open file must exist (See TFile for more information)
818//
819// eg:
820// TFile file("myfile.root", "READ");
821// MEvtLoop evtloop;
822// evtloop.Read();
823// evtloop.MakeMacro("mymacro");
824//
825Int_t MEvtLoop::Read(const char *name)
826{
827 if (!gFile)
828 {
829 *fLog << err << "MEvtloop::Read: No file found. Please create a TFile first." << endl;
830 return 0;
831 }
832
833 if (!gFile->IsOpen())
834 {
835 *fLog << err << "MEvtloop::Read: File not open. Please open the TFile first." << endl;
836 return 0;
837 }
838
839 Int_t n = 0;
840 TObjArray list;
841
842 n += TObject::Read(name);
843
844 if (n==0)
845 {
846 *fLog << err << "MEvtloop::Read: No objects read." << endl;
847 return 0;
848 }
849
850 n += list.Read((TString)name+"_names");
851
852 fParList->SetNames(list);
853
854 HasDuplicateNames(list, "MEvtLoop::Read");
855
856 *fLog << inf << "Eventloop '" << name << "' read from file." << endl;
857
858 return n;
859}
860
861// --------------------------------------------------------------------------
862//
863// If available print the contents of the parameter list.
864//
865void MEvtLoop::Print(Option_t *opt) const
866{
867 if (fParList)
868 fParList->Print();
869 else
870 *fLog << all << "MEvtloop: No Parameter List available." << endl;
871}
872
873// --------------------------------------------------------------------------
874//
875// Writes a eventloop to a file. The default name is "Evtloop".
876// Therefor an open file must exist (See TFile for more information)
877//
878// eg:
879// TFile file("myfile.root", "RECREATE");
880// MEvtLoop evtloop;
881// evtloop.Write();
882// file.Close();
883//
884Int_t MEvtLoop::Write(const char *name, Int_t option, Int_t bufsize)
885{
886 if (!gFile)
887 {
888 *fLog << err << "MEvtloop::Write: No file found. Please create a TFile first." << endl;
889 return 0;
890 }
891
892 if (!gFile->IsOpen())
893 {
894 *fLog << err << "MEvtloop::Write: File not open. Please open the TFile first." << endl;
895 return 0;
896 }
897
898 if (!gFile->IsWritable())
899 {
900 *fLog << err << "MEvtloop::Write: File not writable." << endl;
901 return 0;
902 }
903
904 Int_t n = 0;
905
906 TObjArray list;
907 list.SetOwner();
908
909 fParList->GetNames(list);
910
911 n += TObject::Write(name, option, bufsize);
912
913 if (n==0)
914 {
915 *fLog << err << "MEvtloop::Read: No objects written." << endl;
916 return 0;
917 }
918
919 n += list.Write((TString)name+"_names", kSingleKey);
920
921 HasDuplicateNames(list, "MEvtLoop::Write");
922
923 *fLog << inf << "Eventloop written to file as " << name << "." << endl;
924
925 return n;
926}
927
928// --------------------------------------------------------------------------
929//
930// Read the contents/setup of a parameter container/task from a TEnv
931// instance (steering card/setup file).
932// The key to search for in the file should be of the syntax:
933// prefix.vname
934// While vname is a name which is specific for a single setup date
935// (variable) of this container and prefix is something like:
936// evtloopname.name
937// While name is the name of the containers/tasks in the parlist/tasklist
938//
939// eg. Job4.MImgCleanStd.CleaningLevel1: 3.0
940// Job4.MImgCleanStd.CleaningLevel2: 2.5
941//
942// If this cannot be found the next step is to search for
943// MImgCleanStd.CleaningLevel1: 3.0
944// And if this doesn't exist, too, we should search for:
945// CleaningLevel1: 3.0
946//
947//
948// With the argument prefix you can overwrite the name of the MEvtLoop object
949// as prefix - use with extreme care! The prifix argument must not end with
950// a dot!
951//
952//
953// Warning: The programmer is responsible for the names to be unique in
954// all Mars classes.
955//
956Int_t MEvtLoop::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
957{
958// if (!prefix.IsNull())
959// *fLog << warn << "WARNING - Second argument in MEvtLoop::ReadEnv has no meaning... ignored." << endl;
960
961 if (prefix.IsNull())
962 prefix = fName;
963 prefix += ".";
964
965 *fLog << inf << "Reading resources for " << prefix /*TEnv::fRcName << " from " << env.GetRcName()*/ << endl;
966
967 fLog->ReadEnv(env, prefix, print);
968
969 if (fParList->ReadEnv(env, prefix, print)==kERROR)
970 {
971 *fLog << err << "ERROR - Reading Environment file." << endl;
972 return kFALSE;
973 }
974
975 return kTRUE;
976}
977
978// --------------------------------------------------------------------------
979//
980// Calls 'ReadEnv' with a TEnv initialized with the given file name.
981// If 'config=0' kTRUE is returned.
982//
983Bool_t MEvtLoop::ReadEnv(const char *config)
984{
985 if (!config)
986 return kTRUE;
987
988 const Bool_t fileexist = !gSystem->AccessPathName(config, kFileExists);
989 if (!fileexist)
990 {
991 *fLog << warn << "WARNING - resource file '" << config << "' not found... no resources applied." << endl;
992 return kFALSE;
993 }
994
995 return ReadEnv(TEnv(config));
996}
997
998// --------------------------------------------------------------------------
999//
1000// Write the contents/setup of a parameter container/task to a TEnv
1001// instance (steering card/setup file).
1002// The key to search for in the file should be of the syntax:
1003// prefix.vname
1004// While vname is a name which is specific for a single setup date
1005// (variable) of this container and prefix is something like:
1006// evtloopname.name
1007// While name is the name of the containers/tasks in the parlist/tasklist
1008//
1009// eg. Job4.MImgCleanStd.CleaningLevel1: 3.0
1010// Job4.MImgCleanStd.CleaningLevel2: 2.5
1011//
1012// If this cannot be found the next step is to search for
1013// MImgCleanStd.CleaningLevel1: 3.0
1014// And if this doesn't exist, too, we should search for:
1015// CleaningLevel1: 3.0
1016//
1017// Warning: The programmer is responsible for the names to be unique in
1018// all Mars classes.
1019//
1020Bool_t MEvtLoop::WriteEnv(TEnv &env, TString prefix, Bool_t print) const
1021{
1022 if (!prefix.IsNull())
1023 *fLog << warn << "WARNING - Second argument in MEvtLoop::WriteEnv has no meaning... ignored." << endl;
1024
1025 prefix = fName;
1026 prefix += ".";
1027
1028 *fLog << inf << "Writing resources: " << prefix /*TEnv::fRcName << " to " << env.GetRcName()*/ << endl;
1029
1030 if (fParList->WriteEnv(env, prefix, print)!=kTRUE)
1031 {
1032 *fLog << err << "ERROR - Writing Environment file." << endl;
1033 return kFALSE;
1034 }
1035
1036 fLog->WriteEnv(env, prefix, print);
1037
1038 return kTRUE;
1039}
1040
1041void MEvtLoop::RecursiveRemove(TObject *obj)
1042{
1043 if (obj==fParList)
1044 {
1045 fParList=NULL;
1046 fTaskList=NULL;
1047 }
1048
1049 if (obj==fProgress)
1050 fProgress = NULL;
1051
1052 if (obj==fDisplay)
1053 SetDisplay(NULL);
1054
1055 if (obj==fLog)
1056 {
1057 if (fParList)
1058 fParList->SetLogStream(NULL);
1059 SetLogStream(NULL);
1060 }
1061}
Note: See TracBrowser for help on using the repository browser.