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

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