source: trunk/MagicSoft/Mars/mhist/MFillH.cc@ 1483

Last change on this file since 1483 was 1483, checked in by tbretz, 22 years ago
*** empty log message ***
File size: 12.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-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// WARNING: //
53// Because MFillH is a generalized task to fill histograms it doesn't //
54// know about which branches from a file are necessary to fill the //
55// histograms. If you are reading data from a file which is directly //
56// filled into a histogram via MFillH, please call either //
57// MReadTree::DisableAutoScheme() or enable the necessary branches by //
58// yourself, using MReadTree::EnableBranch() //
59// //
60// Checkout the Warning in MTaskList. //
61// //
62// Input Containers: //
63// A parameter container //
64// //
65// Output Containers: //
66// A histogram container //
67// //
68//////////////////////////////////////////////////////////////////////////////
69#include "MFillH.h"
70
71#include <fstream.h>
72
73#include "MLog.h"
74#include "MLogManip.h"
75
76#include "MH.h"
77#include "MParList.h"
78
79ClassImp(MFillH);
80
81// --------------------------------------------------------------------------
82//
83// Initializes name and title of the object. It is called by all
84// constructors.
85//
86void MFillH::Init(const char *name, const char *title)
87{
88 fName = name ? name : "MFillH";
89 fTitle = title ? title : "Task to fill Mars histograms";
90
91 fH = NULL;
92 fParContainer = NULL;
93}
94
95// --------------------------------------------------------------------------
96//
97// Default Constructor. This is to support some root-stuff.
98// Never try to use it yourself!
99//
100MFillH::MFillH()
101{
102 Init(NULL, NULL);
103}
104
105// --------------------------------------------------------------------------
106//
107// Constructor.
108//
109// 1) - par is the name of the parameter container which should be filled into
110// the histogram
111// - hist is the name of the histogram container (which must have been
112// derived from MH)
113//
114// In this case MH::Fill is called with a pointer to the corresponding
115// histogram instance.
116//
117// 2) - hist is the name and/or type of the histogram.
118// 1) The name and type is identical, eg: "MHHillas"
119// 2) They are not identical, eg: "MyHistogram [MHHillas]"
120// This searches for a class instance of MHHillas with the name
121// "MyHistogram". If it doesn't exist one is created.
122//
123// In this case PreProcess calls MH::SetupFill with a pointer to the
124// parameter list and MH::Fill is called with a NULL-pointer.
125//
126MFillH::MFillH(const char *hist, const char *par, const char *name, const char *title)
127{
128 Init(name, title);
129
130 fHName = hist;
131 fParContainerName = par;
132}
133
134// --------------------------------------------------------------------------
135//
136// Constructor.
137//
138// 1) - par is a pointer to the instance of your parameter container from which
139// the data should be used to fill the histogram.
140// - hist is the name of the histogram container (which must have been
141// derived from MH)
142//
143// In this case MH::Fill is called with a pointer to the corresponding
144// histogram instance.
145//
146// 2) - hist is the name and/or type of the histogram.
147// 1) The name and type is identical, eg: "MHHillas"
148// 2) They are not identical, eg: "MyHistogram [MHHillas]"
149// This searches for a class instance of MHHillas with the name
150// "MyHistogram". If it doesn't exist one is created. Everything
151// which is between the first '[' and the last ']' in the string
152// is used as the histogram type.
153//
154// In this case PreProcess calls MH::SetupFill with a pointer to the
155// parameter list and MH::Fill is called with a NULL-pointer.
156//
157//
158MFillH::MFillH(const char *hist, MParContainer *par, const char *name, const char *title)
159{
160 Init(name, title);
161
162 fHName = hist;
163 fParContainer = par;
164 fParContainerName = par->GetName();
165}
166
167// --------------------------------------------------------------------------
168//
169// Constructor.
170//
171// - par is a pointer to the instance of your parameter container from which
172// the data should be used to fill the histogram.
173// - hist is a pointer to the instance of your histogram container (which must
174// have been derived from MH) into which the data should flow
175//
176MFillH::MFillH(MH *hist, const char *par, const char *name, const char *title)
177{
178 Init(name, title);
179
180 fH = hist;
181 fHName = hist->GetName();
182 fParContainerName = par;
183}
184
185// --------------------------------------------------------------------------
186//
187// Constructor.
188//
189// - par is a pointer to the instance of your parameter container from which
190// the data should be used to fill the histogram.
191// - hist is the name of the histogram container (which must have been
192// derived from MH)
193//
194MFillH::MFillH(MH *hist, MParContainer *par, const char *name, const char *title)
195{
196 Init(name, title);
197
198 fH = hist;
199 fHName = hist->GetName();
200 fParContainer = par;
201 fParContainerName = par->GetName();
202}
203
204TString MFillH::ExtractName(const char *name) const
205{
206 TString type = name;
207
208 const Ssiz_t first = type.First('[');
209 const Ssiz_t last = type.First(']');
210
211 if (!first || !last || first>=last)
212 return type;
213
214 return type.Remove(first).Strip(TString::kBoth);
215}
216
217TString MFillH::ExtractClass(const char *name) const
218{
219 TString type = name;
220
221 const Ssiz_t first = type.First('[');
222 const Ssiz_t last = type.First(']');
223
224 if (!first || !last || first>=last)
225 return type;
226
227 const Ssiz_t length = last-first-1;
228
229 TString strip = fHName(first+1, length);
230 return strip.Strip(TString::kBoth);
231}
232
233// --------------------------------------------------------------------------
234//
235// Checks the parameter list for the existance of the parameter container. If
236// the name of it was given in the constructor. It checks also for the
237// existance of the histogram container in the parameter list if a name was
238// given. If it is not available it tried to create a histogram container
239// with the same type as the given object name.
240//
241Bool_t MFillH::PreProcess(MParList *pList)
242{
243 //
244 // Try to get the histogram container with name fHName from list
245 // or create one with this name
246 //
247 if (!fH)
248 {
249 const TString cls = ExtractClass(fHName);
250 const TString name = ExtractName(fHName);
251
252 TObject *obj=NULL;
253 if (cls==name)
254 obj = pList->FindObject(fHName);
255
256 if (!obj)
257 {
258 if (cls==name)
259 *fLog << inf << "Object '" << fHName << "' not found in parlist... trying to create one." << endl;
260 obj = pList->FindCreateObj(cls, name);
261 }
262
263 if (!obj)
264 return kFALSE;
265
266 //
267 // We were successfull getting it. Check whether it really inherits
268 // from MH, FindCreateObj does only check for inheritance from
269 // 'type'.
270 //
271 if (!obj->InheritsFrom(MH::Class()))
272 {
273 *fLog << err << dbginf << obj->GetName() << " doesn't inherit ";
274 *fLog << "from MH - cannot be used for MFillH... aborting." << endl;
275 return kFALSE;
276 }
277
278 fH = (MH*)obj;
279 }
280
281 //
282 // Now we have the histogram container available. Try to Setup Fill.
283 //
284 if (!fH->SetupFill(pList))
285 {
286 *fLog << err << dbginf << "Error: calling SetupFill for ";
287 *fLog << fH->GetDescriptor() << "... aborting." << endl;
288 return kFALSE;
289 }
290
291 //
292 // If also a parameter container is already set we are done.
293 //
294 if (fParContainer)
295 return kTRUE;
296
297 //
298 // If a name is given try to find the input container in the
299 // list. If it could not be found we cannot proceed.
300 //
301 if (fParContainerName.IsNull())
302 {
303 fParContainer = pList;
304 return kTRUE;
305 }
306
307 fParContainer = (MParContainer*)pList->FindObject(fParContainerName);
308 if (fParContainer)
309 return kTRUE;
310
311 *fLog << err << dbginf << "'" << fParContainerName << "' [MParContainer] not found... aborting." << endl;
312 return kFALSE;
313}
314
315// --------------------------------------------------------------------------
316//
317// Fills the data from the parameter conatiner into the histogram container
318//
319Bool_t MFillH::Process()
320{
321 return fH->Fill(fParContainer);
322}
323
324// --------------------------------------------------------------------------
325//
326// Set the ReadyToSave flag of the histogram container, because now all data
327// has been filled into the histogram.
328//
329Bool_t MFillH::PostProcess()
330{
331 //
332 // Now all data is in the histogram. Maybe some final action is
333 // necessary.
334 //
335 if (!fH->Finalize())
336 {
337 *fLog << err << dbginf << "Error: calling Finalize for ";
338 *fLog << fH->GetDescriptor() << "... aborting." << endl;
339 return kFALSE;
340 }
341
342 fH->SetReadyToSave();
343 return kTRUE;
344}
345
346// --------------------------------------------------------------------------
347//
348// Implementation of SavePrimitive. Used to write the call to a constructor
349// to a macro. In the original root implementation it is used to write
350// gui elements to a macro-file.
351//
352void MFillH::StreamPrimitive(ofstream &out) const
353{
354 if (fH)
355 fH->SavePrimitive(out);
356
357 if (fParContainer)
358 fParContainer->SavePrimitive(out);
359
360 out << " MFillH " << GetUniqueName() << "(";
361
362 if (fH)
363 out << "&" << fH->GetUniqueName();
364 else
365 out << "\"" << fHName << "\"";
366
367 if (fParContainer)
368 out << ", &" << fParContainer->GetUniqueName();
369 else
370 if (!fParContainerName.IsNull())
371 out << ", \"" << fParContainerName << "\"";
372
373 out << ");" << endl;
374}
Note: See TracBrowser for help on using the repository browser.