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

Last change on this file since 4601 was 4601, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 30.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@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 ((rc=fTaskList->Process())==kTRUE)
459 {
460 numcnts++;
461 if (!ProcessGuiEvents(++dummy))
462 break;
463 }
464 else
465 // check for number and break if unsuccessfull
466 while (dummy-- && (rc=fTaskList->Process())==kTRUE)
467 {
468 numcnts++;
469 if (!ProcessGuiEvents(maxcnt - dummy))
470 break;
471 }
472
473 //
474 // stop stop-watch, print results
475 //
476 clock.Stop();
477
478 if (fProgress && !gROOT->IsBatch())
479 {
480 //fProgress->SetPosition(maxcnt>0 ? TMath::Min(maxcnt, entries) : entries);
481 fProgress->SetPosition(1);
482
483 // FIXME: This is a workaround, because TApplication::Run is not
484 // thread safe against ProcessEvents. We assume, that if
485 // we are not in the Main-Thread ProcessEvents() is
486 // called by the TApplication Event Loop...
487 if (!TThread::Self()/*gApplication->InheritsFrom(TRint::Class())*/)
488 {
489#if ROOT_VERSION_CODE < ROOT_VERSION(3,02,06)
490 gSystem->ProcessEvents();
491#else
492 gClient->ProcessEventsFor(fDisplay ? fDisplay->GetBar() : fProgress);
493#endif
494 }
495 }
496
497 *fLog << all << "Ready!" << endl << endl;
498
499 *fLog << dec << endl << "CPU - Time: ";
500 *fLog << clock.CpuTime() << "s" << " for " << numcnts << " Events";
501 if (numcnts>0)
502 *fLog << " --> " << numcnts/clock.CpuTime() << " Events/s";
503 *fLog << endl << "Real - Time: ";
504 *fLog << clock.RealTime() << "s" << " for " << numcnts << " Events";
505 if (numcnts>0)
506 *fLog << " --> " << numcnts/clock.RealTime() << " Events/s";
507
508 *fLog << endl << endl;
509
510 return rc!=kERROR;
511}
512
513// --------------------------------------------------------------------------
514//
515// The postprocessing part of the eventloop. Be careful, this is
516// for developers or use in special jobs only!
517//
518Bool_t MEvtLoop::PostProcess() const
519{
520 //
521 // execute the post process of all tasks
522 //
523 return fTaskList ? fTaskList->PostProcess() : kTRUE;
524}
525
526// --------------------------------------------------------------------------
527//
528// See class description above. Returns kTRUE if PreProcessing,
529// Processing and PostProcessing was successfull, otherwise kFALSE.
530// maxcnt==0 means: all events
531// tlist is the name of the task-list to be used. Be carefull, this
532// feature is not finally implemented - it will only work if no
533// task will access the tasklist.
534//
535Bool_t MEvtLoop::Eventloop(UInt_t maxcnt, const char *tlist)
536{
537 TDatime d;
538 *fLog << inf << underline << "Eventloop: " << fName << " started at " << d.AsString() << endl;
539
540 Bool_t rc = PreProcess(tlist);
541
542 //
543 // If all Tasks were PreProcesses successfully start Processing.
544 //
545 if (rc)
546 rc = Process(maxcnt);
547
548 //
549 // Now postprocess all tasks. Only successfully preprocessed tasks
550 // are postprocessed. If the Postprocessing of one task fails
551 // return an error.
552 //
553 if (!PostProcess())
554 {
555 *fLog << err << "Error detected while PostProcessing." << endl;
556 rc = kFALSE;
557 }
558
559 if (!fDisplay)
560 return rc;
561
562 // Set status lines
563 fDisplay->SetStatusLine1(fName);
564 fDisplay->SetStatusLine2(rc ? "Done." : "Error!");
565 // Stop automatic update
566 fDisplay->StopUpdate();
567 // Reallow context menus
568 fDisplay->SetNoContextMenu(kFALSE);
569 // Reallow user to exit window by File menu
570 fDisplay->UnLock();
571
572 //
573 // If postprocessing of all preprocessed tasks was sucefully return rc.
574 // This gives an error in case the preprocessing has failed already.
575 // Otherwise the eventloop is considered: successfully.
576 //
577 return rc;
578}
579
580// --------------------------------------------------------------------------
581//
582// After you setup (or read) an Evtloop you can use MakeMacro() to write
583// the eventloop setup as a macro. The default name is "evtloop.C". The
584// default extension is .C If the extension is not given, .C is added.
585// If the last character in the argument is a '+' the file is not closed.
586// This is usefull if you have an eventloop which runs three times and
587// you want to write one macro. If the first character is a '+' no
588// opening is written, eg:
589//
590// MEvtLoop evtloop;
591// // some setup
592// evtloop.MakeMacro("mymacro+");
593// // replace the tasklist the first time
594// evtloop.MakeMacro("+mymacro+");
595// // replace the tasklist the second time
596// evtloop.MakeMacro("+mymacro");
597//
598void MEvtLoop::MakeMacro(const char *filename)
599{
600 TString name(filename);
601
602 name = name.Strip(TString::kBoth);
603
604 Bool_t open = kTRUE;
605 Bool_t close = kTRUE;
606 if (name[0]=='+')
607 {
608 open = kFALSE;
609 name.Remove(0, 1);
610 name = name.Strip(TString::kBoth);
611 }
612
613 if (name[name.Length()-1]=='+')
614 {
615 close = kFALSE;
616 name.Remove(name.Length()-1, 1);
617 name = name.Strip(TString::kBoth);
618 }
619
620 if (!name.EndsWith(".C"))
621 name += ".C";
622
623 ofstream fout;
624
625 if (!open)
626 {
627 fout.open(name, ios::app);
628 fout << endl;
629 fout << " // ----------------------------------------------------------------------" << endl;
630 fout << endl;
631 }
632 else
633 {
634 fout.open(name);
635
636 time_t t = time(NULL);
637 fout <<
638 "/* ======================================================================== *\\" << endl <<
639 "!" << endl <<
640 "! *" << endl <<
641 "! * This file is part of MARS, the MAGIC Analysis and Reconstruction" << endl <<
642 "! * Software. It is distributed to you in the hope that it can be a useful" << endl <<
643 "! * and timesaving tool in analysing Data of imaging Cerenkov telescopes." << endl <<
644 "! * It is distributed WITHOUT ANY WARRANTY." << endl <<
645 "! *" << endl <<
646 "! * Permission to use, copy, modify and distribute this software and its" << endl <<
647 "! * documentation for any purpose is hereby granted without fee," << endl <<
648 "! * provided that the above copyright notice appear in all copies and" << endl <<
649 "! * that both that copyright notice and this permission notice appear" << endl <<
650 "! * in supporting documentation. It is provided \"as is\" without express" << endl <<
651 "! * or implied warranty." << endl <<
652 "! *" << endl <<
653 "!" << endl <<
654 "!" << endl <<
655 "! Author(s): Thomas Bretz et al. <mailto:tbretz@astro.uni-wuerzburg.de>" << endl <<
656 "!" << endl <<
657 "! Copyright: MAGIC Software Development, 2000-2002" << endl <<
658 "!" << endl <<
659 "!" << endl <<
660 "\\* ======================================================================== */" << endl << endl <<
661 "// ------------------------------------------------------------------------" << endl <<
662 "//" << endl <<
663 "// This macro was automatically created on" << endl<<
664 "// " << ctime(&t) <<
665 "// with the MEvtLoop::MakeMacro tool." << endl <<
666 "//" << endl <<
667 "// ------------------------------------------------------------------------" << endl << endl <<
668 "void " << name(0, name.Length()-2) << "()" << endl <<
669 "{" << endl;
670 }
671
672 SavePrimitive(fout, (TString)"" + (open?"open":"") + (close?"close":""));
673
674 if (!close)
675 return;
676
677 fout << "}" << endl;
678
679 *fLog << inf << "Macro '" << name << "' written." << endl;
680}
681
682// --------------------------------------------------------------------------
683//
684// Implementation of SavePrimitive. Used to write the call to a constructor
685// to a macro. In the original root implementation it is used to write
686// gui elements to a macro-file.
687//
688void MEvtLoop::StreamPrimitive(ofstream &out) const
689{
690 out << " MEvtLoop " << GetUniqueName();
691 if (fName!="Evtloop")
692 out << "(\"" << fName << "\")";
693 out << ";" << endl;
694}
695
696// --------------------------------------------------------------------------
697//
698//
699void MEvtLoop::SavePrimitive(ofstream &out, Option_t *opt)
700{
701 TString options = opt;
702 options.ToLower();
703
704 if (HasDuplicateNames("MEvtLoop::SavePrimitive"))
705 {
706 out << " // !" << endl;
707 out << " // ! WARNING - Your eventloop (MParList, MTaskList, ...) contains more than" << endl;
708 out << " // ! one object (MParContainer, MTask, ...) with the same name. The created macro" << endl;
709 out << " // ! may need manual intervention before it can be used." << endl;
710 out << " // !" << endl;
711 out << endl;
712 }
713
714 if (!options.Contains("open"))
715 {
716 if (gListOfPrimitives)
717 {
718 *fLog << err << "MEvtLoop::SavePrimitive - Error: old file not closed." << endl;
719 gListOfPrimitives->ForEach(TObject, ResetBit)(BIT(15));
720 delete gListOfPrimitives;
721 }
722 gListOfPrimitives = new TList;
723 }
724
725 if (fParList)
726 fParList->SavePrimitive(out);
727
728 MParContainer::SavePrimitive(out);
729
730 if (fParList)
731 out << " " << GetUniqueName() << ".SetParList(&" << fParList->GetUniqueName() << ");" << endl;
732 else
733 out << " // fParList empty..." << endl;
734 out << " if (!" << GetUniqueName() << ".Eventloop())" << endl;
735 out << " return;" << endl;
736
737 if (!options.Contains("close"))
738 return;
739
740 gListOfPrimitives->ForEach(TObject, ResetBit)(BIT(15));
741 delete gListOfPrimitives;
742 gListOfPrimitives = 0;
743}
744
745// --------------------------------------------------------------------------
746//
747// Get a list of all conmtainer names which are somehow part of the
748// eventloop. Chack for duplicate members and print a warning if
749// duplicates are found. Return kTRUE if duplicates are found, otherwise
750// kFALSE;
751//
752Bool_t MEvtLoop::HasDuplicateNames(TObjArray &arr, const TString txt) const
753{
754 arr.Sort();
755
756 TIter Next(&arr);
757 TObject *obj;
758 TString name;
759 Bool_t found = kFALSE;
760 while ((obj=Next()))
761 {
762 if (name==obj->GetName())
763 {
764 if (!found)
765 {
766 *fLog << warn << endl;
767 *fLog << " ! WARNING (" << txt << ")" << endl;
768 *fLog << " ! Your eventloop (MParList, MTaskList, ...) contains more than" << endl;
769 *fLog << " ! one object (MParContainer, MTask, ...) with the same name." << endl;
770 *fLog << " ! Creating a macro from it using MEvtLoop::MakeMacro may create" << endl;
771 *fLog << " ! a macro which needs manual intervention before it can be used." << endl;
772 found = kTRUE;
773 }
774 *fLog << " ! Please rename: " << obj->GetName() << endl;
775 }
776 name = obj->GetName();
777 }
778
779 return found;
780}
781
782// --------------------------------------------------------------------------
783//
784// Get a list of all conmtainer names which are somehow part of the
785// eventloop. Chack for duplicate members and print a warning if
786// duplicates are found. Return kTRUE if duplicates are found, otherwise
787// kFALSE;
788//
789Bool_t MEvtLoop::HasDuplicateNames(const TString txt) const
790{
791 if (!fParList)
792 return kFALSE;
793
794 TObjArray list;
795 list.SetOwner();
796
797 fParList->GetNames(list);
798
799 return HasDuplicateNames(list, txt);
800}
801
802// --------------------------------------------------------------------------
803//
804// Reads a saved eventloop from a file. The default name is "Evtloop".
805// Therefor an open file must exist (See TFile for more information)
806//
807// eg:
808// TFile file("myfile.root", "READ");
809// MEvtLoop evtloop;
810// evtloop.Read();
811// evtloop.MakeMacro("mymacro");
812//
813Int_t MEvtLoop::Read(const char *name)
814{
815 if (!gFile)
816 {
817 *fLog << err << "MEvtloop::Read: No file found. Please create a TFile first." << endl;
818 return 0;
819 }
820
821 if (!gFile->IsOpen())
822 {
823 *fLog << err << "MEvtloop::Read: File not open. Please open the TFile first." << endl;
824 return 0;
825 }
826
827 Int_t n = 0;
828 TObjArray list;
829
830 n += TObject::Read(name);
831
832 if (n==0)
833 {
834 *fLog << err << "MEvtloop::Read: No objects read." << endl;
835 return 0;
836 }
837
838 n += list.Read((TString)name+"_names");
839
840 fParList->SetNames(list);
841
842 HasDuplicateNames(list, "MEvtLoop::Read");
843
844 *fLog << inf << "Eventloop '" << name << "' read from file." << endl;
845
846 return n;
847}
848
849// --------------------------------------------------------------------------
850//
851// If available print the contents of the parameter list.
852//
853void MEvtLoop::Print(Option_t *opt) const
854{
855 if (fParList)
856 fParList->Print();
857 else
858 *fLog << all << "MEvtloop: No Parameter List available." << endl;
859}
860
861// --------------------------------------------------------------------------
862//
863// Writes a eventloop to a file. The default name is "Evtloop".
864// Therefor an open file must exist (See TFile for more information)
865//
866// eg:
867// TFile file("myfile.root", "RECREATE");
868// MEvtLoop evtloop;
869// evtloop.Write();
870// file.Close();
871//
872Int_t MEvtLoop::Write(const char *name, Int_t option, Int_t bufsize)
873{
874 if (!gFile)
875 {
876 *fLog << err << "MEvtloop::Write: No file found. Please create a TFile first." << endl;
877 return 0;
878 }
879
880 if (!gFile->IsOpen())
881 {
882 *fLog << err << "MEvtloop::Write: File not open. Please open the TFile first." << endl;
883 return 0;
884 }
885
886 if (!gFile->IsWritable())
887 {
888 *fLog << err << "MEvtloop::Write: File not writable." << endl;
889 return 0;
890 }
891
892 Int_t n = 0;
893
894 TObjArray list;
895 list.SetOwner();
896
897 fParList->GetNames(list);
898
899 n += TObject::Write(name, option, bufsize);
900
901 if (n==0)
902 {
903 *fLog << err << "MEvtloop::Read: No objects written." << endl;
904 return 0;
905 }
906
907 n += list.Write((TString)name+"_names", kSingleKey);
908
909 HasDuplicateNames(list, "MEvtLoop::Write");
910
911 *fLog << inf << "Eventloop written to file as " << name << "." << endl;
912
913 return n;
914}
915
916// --------------------------------------------------------------------------
917//
918// Read the contents/setup of a parameter container/task from a TEnv
919// instance (steering card/setup file).
920// The key to search for in the file should be of the syntax:
921// prefix.vname
922// While vname is a name which is specific for a single setup date
923// (variable) of this container and prefix is something like:
924// evtloopname.name
925// While name is the name of the containers/tasks in the parlist/tasklist
926//
927// eg. Job4.MImgCleanStd.CleaningLevel1: 3.0
928// Job4.MImgCleanStd.CleaningLevel2: 2.5
929//
930// If this cannot be found the next step is to search for
931// MImgCleanStd.CleaningLevel1: 3.0
932// And if this doesn't exist, too, we should search for:
933// CleaningLevel1: 3.0
934//
935// Warning: The programmer is responsible for the names to be unique in
936// all Mars classes.
937//
938Int_t MEvtLoop::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
939{
940 if (!prefix.IsNull())
941 *fLog << warn << "WARNING - Second argument in MEvtLoop::ReadEnv has no meaning... ignored." << endl;
942
943 prefix = fName;
944 prefix += ".";
945
946 *fLog << inf << "Reading resources for " << prefix /*TEnv::fRcName << " from " << env.GetRcName()*/ << endl;
947
948 fLog->ReadEnv(env, prefix, print);
949
950 if (fParList->ReadEnv(env, prefix, print)==kERROR)
951 {
952 *fLog << err << "ERROR - Reading Environment file." << endl;
953 return kFALSE;
954 }
955
956 return kTRUE;
957}
958
959// --------------------------------------------------------------------------
960//
961// Calls 'ReadEnv' with a TEnv initialized with the given file name.
962// If 'config=0' kTRUE is returned.
963//
964Bool_t MEvtLoop::ReadEnv(const char *config)
965{
966 return config ? ReadEnv(TEnv(config)) : kTRUE;
967}
968
969// --------------------------------------------------------------------------
970//
971// Write the contents/setup of a parameter container/task to a TEnv
972// instance (steering card/setup file).
973// The key to search for in the file should be of the syntax:
974// prefix.vname
975// While vname is a name which is specific for a single setup date
976// (variable) of this container and prefix is something like:
977// evtloopname.name
978// While name is the name of the containers/tasks in the parlist/tasklist
979//
980// eg. Job4.MImgCleanStd.CleaningLevel1: 3.0
981// Job4.MImgCleanStd.CleaningLevel2: 2.5
982//
983// If this cannot be found the next step is to search for
984// MImgCleanStd.CleaningLevel1: 3.0
985// And if this doesn't exist, too, we should search for:
986// CleaningLevel1: 3.0
987//
988// Warning: The programmer is responsible for the names to be unique in
989// all Mars classes.
990//
991Bool_t MEvtLoop::WriteEnv(TEnv &env, TString prefix, Bool_t print) const
992{
993 if (!prefix.IsNull())
994 *fLog << warn << "WARNING - Second argument in MEvtLoop::WriteEnv has no meaning... ignored." << endl;
995
996 prefix = fName;
997 prefix += ".";
998
999 *fLog << inf << "Writing resources: " << prefix /*TEnv::fRcName << " to " << env.GetRcName()*/ << endl;
1000
1001 if (fParList->WriteEnv(env, prefix, print)!=kTRUE)
1002 {
1003 *fLog << err << "ERROR - Writing Environment file." << endl;
1004 return kFALSE;
1005 }
1006
1007 fLog->WriteEnv(env, prefix, print);
1008
1009 return kTRUE;
1010}
1011
1012void MEvtLoop::RecursiveRemove(TObject *obj)
1013{
1014 if (obj==fParList)
1015 {
1016 fParList=NULL;
1017 fTaskList=NULL;
1018 }
1019
1020 if (obj==fProgress)
1021 fProgress = NULL;
1022
1023 if (obj==fDisplay)
1024 SetDisplay(NULL);
1025
1026 if (obj==fLog)
1027 {
1028 if (fParList)
1029 fParList->SetLogStream(NULL);
1030 SetLogStream(NULL);
1031 }
1032}
Note: See TracBrowser for help on using the repository browser.