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

Last change on this file since 1573 was 1502, checked in by tbretz, 22 years ago
*** empty log message ***
File size: 13.4 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 if (title)
134 return;
135
136 fTitle = "Fill " + fHName;
137 if (!par)
138 return;
139
140 fTitle += " from " + fParContainerName;
141}
142
143// --------------------------------------------------------------------------
144//
145// Constructor.
146//
147// 1) - par is a pointer to the instance of your parameter container from which
148// the data should be used to fill the histogram.
149// - hist is the name of the histogram container (which must have been
150// derived from MH)
151//
152// In this case MH::Fill is called with a pointer to the corresponding
153// histogram instance.
154//
155// 2) - hist is the name and/or type of the histogram.
156// 1) The name and type is identical, eg: "MHHillas"
157// 2) They are not identical, eg: "MyHistogram [MHHillas]"
158// This searches for a class instance of MHHillas with the name
159// "MyHistogram". If it doesn't exist one is created. Everything
160// which is between the first '[' and the last ']' in the string
161// is used as the histogram type.
162//
163// In this case PreProcess calls MH::SetupFill with a pointer to the
164// parameter list and MH::Fill is called with a NULL-pointer.
165//
166//
167MFillH::MFillH(const char *hist, MParContainer *par, const char *name, const char *title)
168{
169 Init(name, title);
170
171 fHName = hist;
172 fParContainer = par;
173 fParContainerName = par->GetName();
174
175 if (!title)
176 fTitle = "Fill " + fHName + " from " + par->GetDescriptor();
177}
178
179// --------------------------------------------------------------------------
180//
181// Constructor.
182//
183// - par is a pointer to the instance of your parameter container from which
184// the data should be used to fill the histogram.
185// - hist is a pointer to the instance of your histogram container (which must
186// have been derived from MH) into which the data should flow
187//
188MFillH::MFillH(MH *hist, const char *par, const char *name, const char *title)
189{
190 Init(name, title);
191
192 fH = hist;
193 fHName = hist->GetName();
194 fParContainerName = par;
195
196 if (title)
197 return;
198
199 fTitle = (TString)"Fill " + hist->GetDescriptor();
200 if (!par)
201 return;
202
203 fTitle += " from " + fParContainerName;
204}
205
206// --------------------------------------------------------------------------
207//
208// Constructor.
209//
210// - par is a pointer to the instance of your parameter container from which
211// the data should be used to fill the histogram.
212// - hist is the name of the histogram container (which must have been
213// derived from MH)
214//
215MFillH::MFillH(MH *hist, MParContainer *par, const char *name, const char *title)
216{
217 Init(name, title);
218
219 fH = hist;
220 fHName = hist->GetName();
221 fParContainer = par;
222 fParContainerName = par->GetName();
223
224 if (!title)
225 fTitle = (TString)"Fill " + hist->GetDescriptor() + " from " + par->GetDescriptor();
226}
227
228TString MFillH::ExtractName(const char *name) const
229{
230 TString type = name;
231
232 const Ssiz_t first = type.First('[');
233 const Ssiz_t last = type.First(']');
234
235 if (!first || !last || first>=last)
236 return type;
237
238 return type.Remove(first).Strip(TString::kBoth);
239}
240
241TString MFillH::ExtractClass(const char *name) const
242{
243 TString type = name;
244
245 const Ssiz_t first = type.First('[');
246 const Ssiz_t last = type.First(']');
247
248 if (!first || !last || first>=last)
249 return type;
250
251 const Ssiz_t length = last-first-1;
252
253 TString strip = fHName(first+1, length);
254 return strip.Strip(TString::kBoth);
255}
256
257// --------------------------------------------------------------------------
258//
259// Checks the parameter list for the existance of the parameter container. If
260// the name of it was given in the constructor. It checks also for the
261// existance of the histogram container in the parameter list if a name was
262// given. If it is not available it tried to create a histogram container
263// with the same type as the given object name.
264//
265Bool_t MFillH::PreProcess(MParList *pList)
266{
267 //
268 // Try to get the histogram container with name fHName from list
269 // or create one with this name
270 //
271 if (!fH)
272 {
273 const TString cls = ExtractClass(fHName);
274 const TString name = ExtractName(fHName);
275
276 TObject *obj=NULL;
277 if (cls==name)
278 obj = pList->FindObject(fHName);
279
280 if (!obj)
281 {
282 if (cls==name)
283 *fLog << inf << "Object '" << fHName << "' not found in parlist... trying to create one." << endl;
284 obj = pList->FindCreateObj(cls, name);
285 }
286
287 if (!obj)
288 return kFALSE;
289
290 //
291 // We were successfull getting it. Check whether it really inherits
292 // from MH, FindCreateObj does only check for inheritance from
293 // 'type'.
294 //
295 if (!obj->InheritsFrom(MH::Class()))
296 {
297 *fLog << err << dbginf << obj->GetName() << " doesn't inherit ";
298 *fLog << "from MH - cannot be used for MFillH... aborting." << endl;
299 return kFALSE;
300 }
301
302 fH = (MH*)obj;
303 }
304
305 //
306 // Now we have the histogram container available. Try to Setup Fill.
307 //
308 if (!fH->SetupFill(pList))
309 {
310 *fLog << err << dbginf << "Error: calling SetupFill for ";
311 *fLog << fH->GetDescriptor() << "... aborting." << endl;
312 return kFALSE;
313 }
314
315 //
316 // If also a parameter container is already set we are done.
317 //
318 if (fParContainer)
319 return kTRUE;
320
321 //
322 // If a name is given try to find the input container in the
323 // list. If it could not be found we cannot proceed.
324 //
325 if (fParContainerName.IsNull())
326 {
327 fParContainer = pList;
328 return kTRUE;
329 }
330
331 fParContainer = (MParContainer*)pList->FindObject(fParContainerName);
332 if (fParContainer)
333 return kTRUE;
334
335 *fLog << err << dbginf << "'" << fParContainerName << "' [MParContainer] not found... aborting." << endl;
336 return kFALSE;
337}
338
339// --------------------------------------------------------------------------
340//
341// Fills the data from the parameter conatiner into the histogram container
342//
343Bool_t MFillH::Process()
344{
345 return fH->Fill(fParContainer);
346}
347
348// --------------------------------------------------------------------------
349//
350// Set the ReadyToSave flag of the histogram container, because now all data
351// has been filled into the histogram.
352//
353Bool_t MFillH::PostProcess()
354{
355 //
356 // Now all data is in the histogram. Maybe some final action is
357 // necessary.
358 //
359 if (!fH->Finalize())
360 {
361 *fLog << err << dbginf << "Error: calling Finalize for ";
362 *fLog << fH->GetDescriptor() << "... aborting." << endl;
363 return kFALSE;
364 }
365
366 fH->SetReadyToSave();
367 return kTRUE;
368}
369
370// --------------------------------------------------------------------------
371//
372// Implementation of SavePrimitive. Used to write the call to a constructor
373// to a macro. In the original root implementation it is used to write
374// gui elements to a macro-file.
375//
376void MFillH::StreamPrimitive(ofstream &out) const
377{
378 if (fH)
379 fH->SavePrimitive(out);
380
381 if (fParContainer)
382 fParContainer->SavePrimitive(out);
383
384 out << " MFillH " << GetUniqueName() << "(";
385
386 if (fH)
387 out << "&" << fH->GetUniqueName();
388 else
389 out << "\"" << fHName << "\"";
390
391 if (fParContainer)
392 out << ", &" << fParContainer->GetUniqueName();
393 else
394 if (!fParContainerName.IsNull())
395 out << ", \"" << fParContainerName << "\"";
396
397 out << ");" << endl;
398}
Note: See TracBrowser for help on using the repository browser.