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-2002
|
---|
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 | // Input Containers: //
|
---|
68 | // A parameter container //
|
---|
69 | // //
|
---|
70 | // Output Containers: //
|
---|
71 | // A histogram container //
|
---|
72 | // //
|
---|
73 | //////////////////////////////////////////////////////////////////////////////
|
---|
74 | #include "MFillH.h"
|
---|
75 |
|
---|
76 | #include <fstream.h>
|
---|
77 |
|
---|
78 | #include <TClass.h>
|
---|
79 | #include <TCanvas.h>
|
---|
80 |
|
---|
81 | #include "MDataChain.h"
|
---|
82 |
|
---|
83 | #include "MLog.h"
|
---|
84 | #include "MLogManip.h"
|
---|
85 |
|
---|
86 | #include "MH.h"
|
---|
87 | #include "MHArray.h"
|
---|
88 |
|
---|
89 | #include "MWeight.h"
|
---|
90 |
|
---|
91 | #include "MParList.h"
|
---|
92 | #include "MStatusDisplay.h"
|
---|
93 |
|
---|
94 | ClassImp(MFillH);
|
---|
95 |
|
---|
96 | // --------------------------------------------------------------------------
|
---|
97 | //
|
---|
98 | // Initializes name and title of the object. It is called by all
|
---|
99 | // constructors.
|
---|
100 | //
|
---|
101 | void MFillH::Init(const char *name, const char *title)
|
---|
102 | {
|
---|
103 | fName = name ? name : "MFillH";
|
---|
104 | fTitle = title ? title : "Task to fill Mars histograms";
|
---|
105 |
|
---|
106 | fH = NULL;
|
---|
107 | fParContainer = NULL;
|
---|
108 |
|
---|
109 | fIndex = NULL;
|
---|
110 | fCanvas = NULL;
|
---|
111 |
|
---|
112 | fWeight = NULL;
|
---|
113 | fWeightName = "";
|
---|
114 | }
|
---|
115 |
|
---|
116 | // --------------------------------------------------------------------------
|
---|
117 | //
|
---|
118 | // Default Constructor. This is to support some root-stuff.
|
---|
119 | // Never try to use it yourself!
|
---|
120 | //
|
---|
121 | MFillH::MFillH()
|
---|
122 | {
|
---|
123 | Init(NULL, NULL);
|
---|
124 | }
|
---|
125 |
|
---|
126 | // --------------------------------------------------------------------------
|
---|
127 | //
|
---|
128 | // Constructor.
|
---|
129 | //
|
---|
130 | // 1) - par is the name of the parameter container which should be filled into
|
---|
131 | // the histogram
|
---|
132 | // - hist is the name of the histogram container (which must have been
|
---|
133 | // derived from MH)
|
---|
134 | //
|
---|
135 | // In this case MH::Fill is called with a pointer to the corresponding
|
---|
136 | // histogram instance.
|
---|
137 | //
|
---|
138 | // 2) - hist is the name and/or type of the histogram.
|
---|
139 | // 1) The name and type is identical, eg: "MHHillas"
|
---|
140 | // 2) They are not identical, eg: "MyHistogram [MHHillas]"
|
---|
141 | // This searches for a class instance of MHHillas with the name
|
---|
142 | // "MyHistogram". If it doesn't exist one is created.
|
---|
143 | //
|
---|
144 | // In this case PreProcess calls MH::SetupFill with a pointer to the
|
---|
145 | // parameter list and MH::Fill is called with a NULL-pointer.
|
---|
146 | //
|
---|
147 | MFillH::MFillH(const char *hist, const char *par, const char *name, const char *title)
|
---|
148 | {
|
---|
149 | Init(name, title);
|
---|
150 |
|
---|
151 | fHName = hist;
|
---|
152 | fParContainerName = par;
|
---|
153 |
|
---|
154 | if (title)
|
---|
155 | return;
|
---|
156 |
|
---|
157 | fTitle = "Fill " + fHName;
|
---|
158 | if (fParContainerName.IsNull())
|
---|
159 | return;
|
---|
160 |
|
---|
161 | fTitle += " from " + fParContainerName;
|
---|
162 | }
|
---|
163 |
|
---|
164 | // --------------------------------------------------------------------------
|
---|
165 | //
|
---|
166 | // Constructor.
|
---|
167 | //
|
---|
168 | // 1) - par is a pointer to the instance of your parameter container from which
|
---|
169 | // the data should be used to fill the histogram.
|
---|
170 | // - hist is the name of the histogram container (which must have been
|
---|
171 | // derived from MH)
|
---|
172 | //
|
---|
173 | // In this case MH::Fill is called with a pointer to the corresponding
|
---|
174 | // histogram instance.
|
---|
175 | //
|
---|
176 | // 2) - hist is the name and/or type of the histogram.
|
---|
177 | // 1) The name and type is identical, eg: "MHHillas"
|
---|
178 | // 2) They are not identical, eg: "MyHistogram [MHHillas]"
|
---|
179 | // This searches for a class instance of MHHillas with the name
|
---|
180 | // "MyHistogram". If it doesn't exist one is created. Everything
|
---|
181 | // which is between the first '[' and the last ']' in the string
|
---|
182 | // is used as the histogram type.
|
---|
183 | //
|
---|
184 | // In this case PreProcess calls MH::SetupFill with a pointer to the
|
---|
185 | // parameter list and MH::Fill is called with a NULL-pointer.
|
---|
186 | //
|
---|
187 | //
|
---|
188 | MFillH::MFillH(const char *hist, MParContainer *par, const char *name, const char *title)
|
---|
189 | {
|
---|
190 | Init(name, title);
|
---|
191 |
|
---|
192 | fHName = hist;
|
---|
193 | fParContainer = par;
|
---|
194 | fParContainerName = par->GetName();
|
---|
195 |
|
---|
196 | if (!title)
|
---|
197 | fTitle = "Fill " + fHName + " from " + par->GetDescriptor();
|
---|
198 | }
|
---|
199 |
|
---|
200 | // --------------------------------------------------------------------------
|
---|
201 | //
|
---|
202 | // Constructor.
|
---|
203 | //
|
---|
204 | // - par is a pointer to the instance of your parameter container from which
|
---|
205 | // the data should be used to fill the histogram.
|
---|
206 | // - hist is a pointer to the instance of your histogram container (which must
|
---|
207 | // have been derived from MH) into which the data should flow
|
---|
208 | //
|
---|
209 | MFillH::MFillH(MH *hist, const char *par, const char *name, const char *title)
|
---|
210 | {
|
---|
211 | Init(name, title);
|
---|
212 |
|
---|
213 | fH = hist;
|
---|
214 | fHName = hist->GetName();
|
---|
215 | fParContainerName = par;
|
---|
216 |
|
---|
217 | AddToBranchList(fH->GetDataMember());
|
---|
218 |
|
---|
219 | if (title)
|
---|
220 | return;
|
---|
221 |
|
---|
222 | fTitle = (TString)"Fill " + hist->GetDescriptor();
|
---|
223 | if (!par)
|
---|
224 | return;
|
---|
225 |
|
---|
226 | fTitle += " from " + fParContainerName;
|
---|
227 | }
|
---|
228 |
|
---|
229 | // --------------------------------------------------------------------------
|
---|
230 | //
|
---|
231 | // Constructor.
|
---|
232 | //
|
---|
233 | // - par is a pointer to the instance of your parameter container from which
|
---|
234 | // the data should be used to fill the histogram.
|
---|
235 | // - hist is the name of the histogram container (which must have been
|
---|
236 | // derived from MH)
|
---|
237 | //
|
---|
238 | MFillH::MFillH(MH *hist, MParContainer *par, const char *name, const char *title)
|
---|
239 | {
|
---|
240 | Init(name, title);
|
---|
241 |
|
---|
242 | fH = hist;
|
---|
243 | fHName = hist->GetName();
|
---|
244 | fParContainer = par;
|
---|
245 | fParContainerName = par->GetName();
|
---|
246 |
|
---|
247 | AddToBranchList(fH->GetDataMember());
|
---|
248 |
|
---|
249 | if (!title)
|
---|
250 | fTitle = (TString)"Fill " + hist->GetDescriptor() + " from " + par->GetDescriptor();
|
---|
251 | }
|
---|
252 |
|
---|
253 | // --------------------------------------------------------------------------
|
---|
254 | //
|
---|
255 | // Destructor. Delete fData if existing and kCanDelete is set.
|
---|
256 | //
|
---|
257 | MFillH::~MFillH()
|
---|
258 | {
|
---|
259 | if (fIndex)
|
---|
260 | if (fIndex->TestBit(kCanDelete))
|
---|
261 | delete fIndex;
|
---|
262 | }
|
---|
263 |
|
---|
264 | // --------------------------------------------------------------------------
|
---|
265 | //
|
---|
266 | // If the histogram to be filles is a MHArray you can specify a 'rule'
|
---|
267 | // This rule is used to create an MDataChain. The return value of the chain
|
---|
268 | // is casted to int. Each int acts as a key. For each (new) key a new
|
---|
269 | // histogram is created in the array. (eg for the rule
|
---|
270 | // "MRawEvtHeader::fRunNumber" you would get one histogram per run-number)
|
---|
271 | //
|
---|
272 | void MFillH::SetRuleForIdx(const TString rule)
|
---|
273 | {
|
---|
274 | fIndex = new MDataChain(rule);
|
---|
275 | fIndex->SetBit(kCanDelete);
|
---|
276 | }
|
---|
277 |
|
---|
278 | // --------------------------------------------------------------------------
|
---|
279 | //
|
---|
280 | // If the histogram to be filles is a MHArray you can specify a MData-object
|
---|
281 | // The return value of the object is casted to int. Each int acts as a key.
|
---|
282 | // For each (new) key a new histogram is created in the array. (eg for
|
---|
283 | // MDataMember("MRawEvtHeader::fRunNumber") you would get one histogram per
|
---|
284 | // run-number)
|
---|
285 | //
|
---|
286 | void MFillH::SetRuleForIdx(MData *data)
|
---|
287 | {
|
---|
288 | fIndex = data;
|
---|
289 | }
|
---|
290 |
|
---|
291 | // --------------------------------------------------------------------------
|
---|
292 | //
|
---|
293 | // Extracts the name of the histogram from the MFillH argument
|
---|
294 | //
|
---|
295 | TString MFillH::ExtractName(const char *name) const
|
---|
296 | {
|
---|
297 | TString type = name;
|
---|
298 |
|
---|
299 | const Ssiz_t first = type.First('[');
|
---|
300 | const Ssiz_t last = type.First(']');
|
---|
301 |
|
---|
302 | if (!first || !last || first>=last)
|
---|
303 | return type;
|
---|
304 |
|
---|
305 | return type.Remove(first).Strip(TString::kBoth);
|
---|
306 | }
|
---|
307 |
|
---|
308 | // --------------------------------------------------------------------------
|
---|
309 | //
|
---|
310 | // Extracts the class-name of the histogram from the MFillH argument
|
---|
311 | //
|
---|
312 | TString MFillH::ExtractClass(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 | const Ssiz_t length = last-first-1;
|
---|
323 |
|
---|
324 | TString strip = fHName(first+1, length);
|
---|
325 | return strip.Strip(TString::kBoth);
|
---|
326 | }
|
---|
327 |
|
---|
328 | Bool_t MFillH::DrawToDisplay()
|
---|
329 | {
|
---|
330 | if (!fDisplay)
|
---|
331 | return kTRUE;
|
---|
332 |
|
---|
333 | fCanvas = &fDisplay->AddTab(fH->GetName());
|
---|
334 | fH->Draw();
|
---|
335 |
|
---|
336 | return kTRUE;
|
---|
337 | }
|
---|
338 |
|
---|
339 | // --------------------------------------------------------------------------
|
---|
340 | //
|
---|
341 | // Checks the parameter list for the existance of the parameter container. If
|
---|
342 | // the name of it was given in the constructor. It checks also for the
|
---|
343 | // existance of the histogram container in the parameter list if a name was
|
---|
344 | // given. If it is not available it tried to create a histogram container
|
---|
345 | // with the same type as the given object name.
|
---|
346 | //
|
---|
347 | Bool_t MFillH::PreProcess(MParList *pList)
|
---|
348 | {
|
---|
349 | if (fIndex)
|
---|
350 | {
|
---|
351 | if (!fIndex->PreProcess(pList))
|
---|
352 | {
|
---|
353 | *fLog << all << "PreProcessing of Index rule failed... aborting." << endl;
|
---|
354 | return kFALSE;
|
---|
355 | }
|
---|
356 |
|
---|
357 | if (!fIndex->IsValid())
|
---|
358 | {
|
---|
359 | *fLog << all << "Given Index rule invalid... aborting." << endl;
|
---|
360 | return kFALSE;
|
---|
361 | }
|
---|
362 | }
|
---|
363 |
|
---|
364 | //
|
---|
365 | // If the user defined the use of a weight: search for it.
|
---|
366 | //
|
---|
367 | if (!fWeight && !fWeightName.IsNull())
|
---|
368 | {
|
---|
369 | fWeight = (MWeight*)pList->FindObject(fWeightName, "MWeight");
|
---|
370 | *fLog << err << fWeightName << " [MWeight] not found... aborting." << endl;
|
---|
371 | return kFALSE;
|
---|
372 | }
|
---|
373 |
|
---|
374 | //
|
---|
375 | // Try to get the histogram container with name fHName from list
|
---|
376 | // or create one with this name
|
---|
377 | //
|
---|
378 | if (!fH)
|
---|
379 | {
|
---|
380 | const TString cls = ExtractClass(fHName);
|
---|
381 | const TString name = ExtractName(fHName);
|
---|
382 |
|
---|
383 | TObject *obj=NULL;
|
---|
384 | if (cls==name)
|
---|
385 | obj = pList->FindObject(fHName);
|
---|
386 |
|
---|
387 | if (!obj)
|
---|
388 | {
|
---|
389 | /*
|
---|
390 | if (cls==name)
|
---|
391 | *fLog << inf << "Object '" << fHName << "' not found in parlist... creating." << endl;
|
---|
392 | */
|
---|
393 | obj = pList->FindCreateObj(cls, name);
|
---|
394 | }
|
---|
395 |
|
---|
396 | if (!obj)
|
---|
397 | return kFALSE;
|
---|
398 |
|
---|
399 | //
|
---|
400 | // We were successfull getting it. Check whether it really inherits
|
---|
401 | // from MH, FindCreateObj does only check for inheritance from
|
---|
402 | // 'type'.
|
---|
403 | //
|
---|
404 | TClass *tcls = fIndex ? MHArray::Class() : MH::Class();
|
---|
405 | if (!obj->InheritsFrom(tcls))
|
---|
406 | {
|
---|
407 | *fLog << err << obj->GetName() << " doesn't inherit ";
|
---|
408 | *fLog << "from " << tcls->GetName() << " - cannot be used for MFillH...";
|
---|
409 | *fLog << "aborting." << endl;
|
---|
410 | return kFALSE;
|
---|
411 | }
|
---|
412 |
|
---|
413 | fH = (MH*)obj;
|
---|
414 | }
|
---|
415 |
|
---|
416 | //
|
---|
417 | // Now we have the histogram container available. Try to Setup Fill.
|
---|
418 | //
|
---|
419 | if (!fH->SetupFill(pList))
|
---|
420 | {
|
---|
421 | *fLog << err << "ERROR - Calling SetupFill for ";
|
---|
422 | *fLog << fH->GetDescriptor() << "... aborting." << endl;
|
---|
423 | return kFALSE;
|
---|
424 | }
|
---|
425 |
|
---|
426 | //
|
---|
427 | // If also a parameter container is already set we are done.
|
---|
428 | //
|
---|
429 | if (fParContainer)
|
---|
430 | return DrawToDisplay();
|
---|
431 |
|
---|
432 | //
|
---|
433 | // This case means, that the MH sets up its container to be filled
|
---|
434 | // by itself. Check there if it has something to be filled with!
|
---|
435 | //
|
---|
436 | if (fParContainerName.IsNull())
|
---|
437 | {
|
---|
438 | fParContainer = NULL;
|
---|
439 | return DrawToDisplay();
|
---|
440 | }
|
---|
441 |
|
---|
442 | fParContainer = (MParContainer*)pList->FindObject(fParContainerName);
|
---|
443 | if (fParContainer)
|
---|
444 | return DrawToDisplay();
|
---|
445 |
|
---|
446 | *fLog << err << fParContainerName << " [MParContainer] not found... aborting." << endl;
|
---|
447 | return kFALSE;
|
---|
448 | }
|
---|
449 |
|
---|
450 | // --------------------------------------------------------------------------
|
---|
451 | //
|
---|
452 | // Fills the data from the parameter conatiner into the histogram container
|
---|
453 | //
|
---|
454 | Bool_t MFillH::Process()
|
---|
455 | {
|
---|
456 | if (fIndex)
|
---|
457 | ((MHArray*)fH)->SetIndexByKey(fIndex->GetValue());
|
---|
458 | /*
|
---|
459 | const Int_t key = (Int_t)fIndex->GetValue();
|
---|
460 | const Int_t idx = fMapIdx->Add(key);
|
---|
461 | ((MHArray*)fH)->SetIndex(idx);
|
---|
462 | */
|
---|
463 |
|
---|
464 | return fH->Fill(fParContainer, fWeight?fWeight->GetWeight():1);
|
---|
465 | }
|
---|
466 |
|
---|
467 | // --------------------------------------------------------------------------
|
---|
468 | //
|
---|
469 | // Set the ReadyToSave flag of the histogram container, because now all data
|
---|
470 | // has been filled into the histogram.
|
---|
471 | //
|
---|
472 | Bool_t MFillH::PostProcess()
|
---|
473 | {
|
---|
474 | //
|
---|
475 | // Now all data is in the histogram. Maybe some final action is
|
---|
476 | // necessary.
|
---|
477 | //
|
---|
478 | if (!fH->Finalize())
|
---|
479 | {
|
---|
480 | *fLog << err << "ERROR - Calling Finalize for ";
|
---|
481 | *fLog << fH->GetDescriptor() << "... aborting." << endl;
|
---|
482 | return kFALSE;
|
---|
483 | }
|
---|
484 |
|
---|
485 | fH->SetReadyToSave();
|
---|
486 |
|
---|
487 | if (fDisplay)
|
---|
488 | {
|
---|
489 | fCanvas->cd();
|
---|
490 | fH->DrawClone("nonew");
|
---|
491 | fCanvas->Modified();
|
---|
492 | fCanvas->Update();
|
---|
493 | }
|
---|
494 |
|
---|
495 | return kTRUE;
|
---|
496 | }
|
---|
497 |
|
---|
498 | // --------------------------------------------------------------------------
|
---|
499 | //
|
---|
500 | // Implementation of SavePrimitive. Used to write the call to a constructor
|
---|
501 | // to a macro. In the original root implementation it is used to write
|
---|
502 | // gui elements to a macro-file.
|
---|
503 | //
|
---|
504 | void MFillH::StreamPrimitive(ofstream &out) const
|
---|
505 | {
|
---|
506 | if (fH)
|
---|
507 | fH->SavePrimitive(out);
|
---|
508 |
|
---|
509 | if (fParContainer)
|
---|
510 | fParContainer->SavePrimitive(out);
|
---|
511 |
|
---|
512 | if (fWeight)
|
---|
513 | fWeight->SavePrimitive(out);
|
---|
514 |
|
---|
515 | out << " MFillH " << GetUniqueName() << "(";
|
---|
516 |
|
---|
517 | if (fH)
|
---|
518 | out << "&" << fH->GetUniqueName();
|
---|
519 | else
|
---|
520 | out << "\"" << fHName << "\"";
|
---|
521 |
|
---|
522 | if (fParContainer)
|
---|
523 | out << ", &" << fParContainer->GetUniqueName();
|
---|
524 | else
|
---|
525 | if (!fParContainerName.IsNull())
|
---|
526 | out << ", \"" << fParContainerName << "\"";
|
---|
527 |
|
---|
528 | out << ");" << endl;
|
---|
529 |
|
---|
530 | if (fWeight || !fWeightName.IsNull())
|
---|
531 | {
|
---|
532 | out << " " << GetUniqueName() << ".SetWeight(";
|
---|
533 | if (fWeight)
|
---|
534 | out << "&" << fWeight->GetUniqueName() << ");" << endl;
|
---|
535 | else
|
---|
536 | if (!fWeightName.IsNull())
|
---|
537 | out << "\"" << fWeightName << "\");" << endl;
|
---|
538 | }
|
---|
539 |
|
---|
540 | if (fIndex)
|
---|
541 | {
|
---|
542 | out << " " << GetUniqueName() << ".SetRuleForIdx(\"";
|
---|
543 | out << fIndex->GetRule() << "\");" << endl;
|
---|
544 | }
|
---|
545 | }
|
---|