source: trunk/MagicSoft/Mars/mhbase/MFillH.cc@ 5250

Last change on this file since 5250 was 4997, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 17.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, 07/2001 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2004
21!
22!
23\* ======================================================================== */
24
25//////////////////////////////////////////////////////////////////////////////
26//
27// MFillH
28//
29// This is a common interface (task) to fill mars histograms. Every mars
30// histogram which is derived from MH can be filled with this task.
31//
32// There are two options to use:
33//
34// 1) You specifiy the parameter container with which data the
35// histogram container should be filled, and the histogram container
36// which has to be filled. This can be done by either specifing the
37// name of the objects in the parameter list or by specifiing a pointer
38// to the object. (s. Constructor)
39//
40// 2) You specify the name and/or type of the histogram to become filled.
41// Any other action imust be taken by the histogram class.
42//
43// PreProcess: In the preprocessing of this task we setup all pointers
44// to instances which are needed and call FillSetup of the
45// histogram class with the parameter list as an argument.
46//
47// Process: The process function calls the Fill member function of the
48// histogram class instance (inheriting from MH) with either
49// a NULL pointer or a pointer to the corresponding container
50// as an argument.
51//
52// To use a weight for each event filled in a histogram call
53// SetWeight(). You can eithe use the name of a MWeight container stored
54// in the parameter list or a pointer to it as an argument.
55//
56//
57// WARNING:
58// Because MFillH is a generalized task to fill histograms it doesn't
59// know about which branches from a file are necessary to fill the
60// histograms. If you are reading data from a file which is directly
61// filled into a histogram via MFillH, please call either
62// MReadTree::DisableAutoScheme() or enable the necessary branches by
63// yourself, using MReadTree::EnableBranch()
64//
65// Checkout the Warning in MTaskList.
66//
67// Version 2:
68// ----------
69// - added fNumExcutions
70//
71//
72// Input Containers:
73// A parameter container
74//
75// Output Containers:
76// A histogram container
77//
78//////////////////////////////////////////////////////////////////////////////
79#include "MFillH.h"
80
81#include <fstream>
82
83#include <TClass.h>
84#include <TCanvas.h>
85
86#include "MDataChain.h"
87
88#include "MLog.h"
89#include "MLogManip.h"
90
91#include "MH.h"
92#include "MHArray.h"
93
94#include "MWeight.h"
95
96#include "MParList.h"
97#include "MStatusDisplay.h"
98
99ClassImp(MFillH);
100
101using namespace std;
102
103// --------------------------------------------------------------------------
104//
105// Initializes name and title of the object. It is called by all
106// constructors.
107//
108void MFillH::Init(const char *name, const char *title)
109{
110 fName = name ? name : "MFillH";
111 fTitle = title ? title : "Task to fill Mars histograms";
112
113 fH = NULL;
114 fParContainer = NULL;
115
116 fIndex = NULL;
117 fCanvas = NULL;
118
119 fWeight = NULL;
120 fWeightName = "";
121}
122
123// --------------------------------------------------------------------------
124//
125// Default Constructor. This is to support some root-stuff.
126// Never try to use it yourself!
127//
128MFillH::MFillH()
129{
130 Init(NULL, NULL);
131}
132
133// --------------------------------------------------------------------------
134//
135// Constructor.
136//
137// 1) - par is the name of the parameter container which should be filled into
138// the histogram
139// - hist is the name of the histogram container (which must have been
140// derived from MH)
141//
142// In this case MH::Fill is called with a pointer to the corresponding
143// histogram instance.
144//
145// 2) - hist is the name and/or type of the histogram.
146// 1) The name and type is identical, eg: "MHHillas"
147// 2) They are not identical, eg: "MyHistogram [MHHillas]"
148// This searches for a class instance of MHHillas with the name
149// "MyHistogram". If it doesn't exist one is created.
150//
151// In this case PreProcess calls MH::SetupFill with a pointer to the
152// parameter list and MH::Fill is called with a NULL-pointer.
153//
154MFillH::MFillH(const char *hist, const char *par, const char *name, const char *title)
155{
156 Init(name, title);
157
158 fHName = hist;
159 fParContainerName = par;
160
161 AddToBranchList(Form("%s.*", (const char*)ExtractName(hist)));
162 if (par)
163 AddToBranchList(Form("%s.*", (const char*)ExtractName(par)));
164
165 if (title)
166 return;
167
168 fTitle = "Fill " + fHName;
169 if (fParContainerName.IsNull())
170 return;
171
172 fTitle += " from " + fParContainerName;
173}
174
175// --------------------------------------------------------------------------
176//
177// Constructor.
178//
179// 1) - par is a pointer to the instance of your parameter container from which
180// the data should be used to fill the histogram.
181// - hist is the name of the histogram container (which must have been
182// derived from MH)
183//
184// In this case MH::Fill is called with a pointer to the corresponding
185// histogram instance.
186//
187// 2) - hist is the name and/or type of the histogram.
188// 1) The name and type is identical, eg: "MHHillas"
189// 2) They are not identical, eg: "MyHistogram [MHHillas]"
190// This searches for a class instance of MHHillas with the name
191// "MyHistogram". If it doesn't exist one is created. Everything
192// which is between the first '[' and the last ']' in the string
193// is used as the histogram type.
194//
195// In this case PreProcess calls MH::SetupFill with a pointer to the
196// parameter list and MH::Fill is called with a NULL-pointer.
197//
198//
199MFillH::MFillH(const char *hist, MParContainer *par, const char *name, const char *title)
200{
201 Init(name, title);
202
203 fHName = hist;
204 fParContainer = par;
205 fParContainerName = par->GetName();
206
207 AddToBranchList(Form("%s.*", (const char*)ExtractName(hist)));
208 AddToBranchList(Form("%s.*", par->GetName()));
209
210 if (!title)
211 fTitle = "Fill " + fHName + " from " + par->GetDescriptor();
212}
213
214// --------------------------------------------------------------------------
215//
216// Constructor.
217//
218// - par is a pointer to the instance of your parameter container from which
219// the data should be used to fill the histogram.
220// - hist is a pointer to the instance of your histogram container (which must
221// have been derived from MH) into which the data should flow
222//
223MFillH::MFillH(MH *hist, const char *par, const char *name, const char *title)
224{
225 Init(name, title);
226
227 fH = hist;
228 fHName = hist->GetName();
229 fParContainerName = par;
230
231 AddToBranchList(fH->GetDataMember());
232 if (par)
233 AddToBranchList(Form("%s.*", (const char*)ExtractName(par)));
234
235 if (title)
236 return;
237
238 fTitle = (TString)"Fill " + hist->GetDescriptor();
239 if (!par)
240 return;
241
242 fTitle += " from " + fParContainerName;
243}
244
245// --------------------------------------------------------------------------
246//
247// Constructor.
248//
249// - par is a pointer to the instance of your parameter container from which
250// the data should be used to fill the histogram.
251// - hist is the name of the histogram container (which must have been
252// derived from MH)
253//
254MFillH::MFillH(MH *hist, MParContainer *par, const char *name, const char *title)
255{
256 Init(name, title);
257
258 fH = hist;
259 fHName = hist->GetName();
260 fParContainer = par;
261 fParContainerName = par->GetName();
262
263 AddToBranchList(fH->GetDataMember());
264 AddToBranchList(Form("%s.*", par->GetName()));
265
266 if (!title)
267 fTitle = (TString)"Fill " + hist->GetDescriptor() + " from " + par->GetDescriptor();
268}
269
270// --------------------------------------------------------------------------
271//
272// Destructor. Delete fData if existing and kCanDelete is set.
273//
274MFillH::~MFillH()
275{
276 if (fIndex)
277 if (fIndex->TestBit(kCanDelete))
278 delete fIndex;
279}
280
281// --------------------------------------------------------------------------
282//
283// If the histogram to be filles is a MHArray you can specify a 'rule'
284// This rule is used to create an MDataChain. The return value of the chain
285// is casted to int. Each int acts as a key. For each (new) key a new
286// histogram is created in the array. (eg for the rule
287// "MRawEvtHeader::fRunNumber" you would get one histogram per run-number)
288//
289void MFillH::SetRuleForIdx(const TString rule)
290{
291 fIndex = new MDataChain(rule);
292 fIndex->SetBit(kCanDelete);
293}
294
295// --------------------------------------------------------------------------
296//
297// If the histogram to be filles is a MHArray you can specify a MData-object
298// The return value of the object is casted to int. Each int acts as a key.
299// For each (new) key a new histogram is created in the array. (eg for
300// MDataMember("MRawEvtHeader::fRunNumber") you would get one histogram per
301// run-number)
302//
303void MFillH::SetRuleForIdx(MData *data)
304{
305 fIndex = data;
306}
307
308// --------------------------------------------------------------------------
309//
310// Extracts the name of the histogram from the MFillH argument
311//
312TString MFillH::ExtractName(const char *name) const
313{
314 TString type = name;
315
316 const Ssiz_t first = type.First('[');
317 const Ssiz_t last = type.First(']');
318
319 if (!first || !last || first>=last)
320 return type;
321
322 return type.Remove(first).Strip(TString::kBoth);
323}
324
325// --------------------------------------------------------------------------
326//
327// Extracts the class-name of the histogram from the MFillH argument
328//
329TString MFillH::ExtractClass(const char *name) const
330{
331 TString type = name;
332
333 const Ssiz_t first = type.First('[');
334 const Ssiz_t last = type.First(']');
335
336 if (!first || !last || first>=last)
337 return type;
338
339 const Ssiz_t length = last-first-1;
340
341 TString strip = fHName(first+1, length);
342 return strip.Strip(TString::kBoth);
343}
344
345// --------------------------------------------------------------------------
346//
347// Use this to set a draw option used when drawing automatically to the
348// status display.
349//
350void MFillH::SetDrawOption(Option_t *option)
351{
352 fDrawOption = option;
353}
354
355// --------------------------------------------------------------------------
356//
357// Creates a new tab in a status display with the name of the MH class,
358// if fDisplay is set and the MH-class overwrites the Draw function
359//
360Bool_t MFillH::DrawToDisplay()
361{
362 fCanvas = NULL;
363
364 if (!fDisplay)
365 return kTRUE;
366
367 if (!fH->OverwritesDraw())
368 return kTRUE;
369
370 if (TestBit(kDoNotDisplay))
371 return kTRUE;
372
373 fCanvas = &fDisplay->AddTab(fNameTab.IsNull() ? fH->GetName() : fNameTab.Data());
374 fH->Draw(fDrawOption);
375
376 return kTRUE;
377}
378
379// --------------------------------------------------------------------------
380//
381// Checks the parameter list for the existance of the parameter container. If
382// the name of it was given in the constructor. It checks also for the
383// existance of the histogram container in the parameter list if a name was
384// given. If it is not available it tried to create a histogram container
385// with the same type as the given object name.
386//
387Int_t MFillH::PreProcess(MParList *pList)
388{
389 if (fIndex)
390 {
391 if (!fIndex->PreProcess(pList))
392 {
393 *fLog << all << "PreProcessing of Index rule failed... aborting." << endl;
394 return kFALSE;
395 }
396
397 if (!fIndex->IsValid())
398 {
399 *fLog << all << "Given Index rule invalid... aborting." << endl;
400 return kFALSE;
401 }
402 }
403
404 //
405 // If the user defined the use of a weight: search for it.
406 //
407 if (!fWeight && !fWeightName.IsNull())
408 {
409 fWeight = (MWeight*)pList->FindObject(fWeightName, "MWeight");
410
411 if (!fWeight)
412 {
413 *fLog << err << fWeightName << " [MWeight] not found... aborting." << endl;
414 return kFALSE;
415 }
416 }
417
418 //
419 // Try to get the histogram container with name fHName from list
420 // or create one with this name
421 //
422 if (!fH)
423 {
424 const TString cls = ExtractClass(fHName);
425 const TString name = ExtractName(fHName);
426
427 TObject *obj=NULL;
428 if (cls==name)
429 obj = pList->FindObject(fHName);
430
431 if (!obj)
432 {
433 /*
434 if (cls==name)
435 *fLog << inf << "Object '" << fHName << "' not found in parlist... creating." << endl;
436 */
437 obj = pList->FindCreateObj(cls, name);
438 }
439
440 if (!obj)
441 return kFALSE;
442
443 //
444 // We were successfull getting it. Check whether it really inherits
445 // from MH, FindCreateObj does only check for inheritance from
446 // 'type'.
447 //
448 TClass *tcls = fIndex ? MHArray::Class() : MH::Class();
449 if (!obj->InheritsFrom(tcls))
450 {
451 *fLog << err << obj->GetName() << " doesn't inherit ";
452 *fLog << "from " << tcls->GetName() << " - cannot be used for MFillH...";
453 *fLog << "aborting." << endl;
454 return kFALSE;
455 }
456
457 fH = (MH*)obj;
458 }
459
460 //
461 // Now we have the histogram container available. Try to Setup Fill.
462 //
463 fH->SetSerialNumber(GetSerialNumber());
464 fH->SetNumExecutions(0);
465 if (!fH->SetupFill(pList))
466 {
467 *fLog << (TestBit(kCanSkip) ? warn : err);
468 *fLog << (TestBit(kCanSkip) ? "WARNING" : "ERROR");
469 *fLog << " - Calling SetupFill for " << fH->GetDescriptor() << "...";
470 *fLog << (TestBit(kCanSkip) ? "skipped." : "aborting.") << endl;
471
472 return TestBit(kCanSkip) ? kSKIP : kFALSE;
473 }
474
475 //
476 // If also a parameter container is already set we are done.
477 //
478 if (fParContainer)
479 return DrawToDisplay();
480
481 //
482 // This case means, that the MH sets up its container to be filled
483 // by itself. Check there if it has something to be filled with!
484 //
485 if (fParContainerName.IsNull())
486 {
487 fParContainer = NULL;
488 return DrawToDisplay();
489 }
490
491 fParContainer = (MParContainer*)pList->FindObject(fParContainerName);
492 if (fParContainer)
493 return DrawToDisplay();
494
495 if (TestBit(kCanSkip))
496 {
497 *fLog << warn << fParContainerName << " [MParContainer] not found... skipped." << endl;
498 return kSKIP;
499 }
500
501 *fLog << err << fParContainerName << " [MParContainer] not found... aborting." << endl;
502 return kFALSE;
503}
504
505// --------------------------------------------------------------------------
506//
507// Call the ReInit function of the contained Histogram
508//
509Bool_t MFillH::ReInit(MParList *pList)
510{
511 return fH->ReInit(pList);
512}
513
514// --------------------------------------------------------------------------
515//
516// Fills the data from the parameter conatiner into the histogram container
517//
518Int_t MFillH::Process()
519{
520 if (fIndex)
521 ((MHArray*)fH)->SetIndexByKey(fIndex->GetValue());
522 /*
523 const Int_t key = (Int_t)fIndex->GetValue();
524 const Int_t idx = fMapIdx->Add(key);
525 ((MHArray*)fH)->SetIndex(idx);
526 */
527
528 TVirtualPad *save = gPad;
529 if (fCanvas)
530 fCanvas->cd();
531
532 const Bool_t rc = fH->Fill(fParContainer, fWeight?fWeight->GetWeight():1);
533 fH->SetNumExecutions(GetNumExecutions()+1);
534
535 if (save && fCanvas)
536 save->cd();
537
538 return rc;
539}
540
541// --------------------------------------------------------------------------
542//
543// Set the ReadyToSave flag of the histogram container, because now all data
544// has been filled into the histogram.
545//
546Int_t MFillH::PostProcess()
547{
548 //
549 // Now all data is in the histogram. Maybe some final action is
550 // necessary.
551 //
552 if (!fH->Finalize())
553 {
554 *fLog << err << "ERROR - Calling Finalize for ";
555 *fLog << fH->GetDescriptor() << "... aborting." << endl;
556 return kFALSE;
557 }
558
559 fH->SetReadyToSave();
560
561 //
562 // Check whether fDisplay has previously been used (fCanvas),
563 // fDisplay is still open and the corresponding Canvas/Tab is
564 // still existing.
565 //
566 if (fDisplay && fDisplay->HasCanvas(fCanvas))
567 {
568 const TString opt(Form("nonew %s", fDrawOption.Data()));
569 fCanvas->cd();
570 fH->DrawClone(opt);
571 fCanvas->Modified();
572 fCanvas->Update();
573 }
574
575 return kTRUE;
576}
577
578// --------------------------------------------------------------------------
579//
580// Implementation of SavePrimitive. Used to write the call to a constructor
581// to a macro. In the original root implementation it is used to write
582// gui elements to a macro-file.
583//
584void MFillH::StreamPrimitive(ofstream &out) const
585{
586 if (fH)
587 fH->SavePrimitive(out);
588
589 if (fParContainer)
590 fParContainer->SavePrimitive(out);
591
592 if (fWeight)
593 fWeight->SavePrimitive(out);
594
595 out << " MFillH " << GetUniqueName() << "(";
596
597 if (fH)
598 out << "&" << fH->GetUniqueName();
599 else
600 out << "\"" << fHName << "\"";
601
602 if (fParContainer)
603 out << ", &" << fParContainer->GetUniqueName();
604 else
605 if (!fParContainerName.IsNull())
606 out << ", \"" << fParContainerName << "\"";
607
608 out << ");" << endl;
609
610 if (fWeight || !fWeightName.IsNull())
611 {
612 out << " " << GetUniqueName() << ".SetWeight(";
613 if (fWeight)
614 out << "&" << fWeight->GetUniqueName() << ");" << endl;
615 else
616 if (!fWeightName.IsNull())
617 out << "\"" << fWeightName << "\");" << endl;
618 }
619
620 if (fIndex)
621 {
622 out << " " << GetUniqueName() << ".SetRuleForIdx(\"";
623 out << fIndex->GetRule() << "\");" << endl;
624 }
625}
Note: See TracBrowser for help on using the repository browser.