source: trunk/MagicSoft/Mars/mhist/MHArray.cc@ 1574

Last change on this file since 1574 was 1574, checked in by tbretz, 22 years ago
*** empty log message ***
File size: 10.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// MHArray
28//
29// is a sequential collection of mars histograms. If the given index to
30// call the Fill function of the histogram excceeds the size of the
31// array by 1 a new entry is created.
32//
33// With Set/Inc/DecIndex you may specify the actual index of the histogram
34// wich should be filles by Fill.
35//
36// Use GetH to get the current histogram, the []-operator get the histogram
37// by its index.
38//
39// To map a key to the index use/see MFillH, which automatically maps a
40// key-value to the array index.
41//
42// In the constructor istempl leads to two different behaviours of the
43// MHArray:
44//
45// - istempl=kTRUE tells MHArray to use the first histogram retrieved from
46// the Parameterlist by hname to be used as a template. New histograms
47// are not created using the root dictionary, but the New-member function
48// (see MParConatiner)
49// - In the case istempl=kFALSE new histograms are created using the root
50// dictionary while hname is the class name. For the creation their
51// default constructor is used.
52//
53//////////////////////////////////////////////////////////////////////////////
54#include "MHArray.h"
55
56#include <TH1.h>
57#include <TH2.h>
58#include <TH3.h>
59#include <TStyle.h>
60#include <TGaxis.h>
61#include <TCanvas.h>
62#include <TLegend.h>
63#include <TPaveStats.h>
64
65#include "MLog.h"
66#include "MLogManip.h"
67
68#include "MParList.h"
69#include "MParContainer.h"
70
71#include "MBinning.h"
72
73ClassImp(MHArray);
74
75// --------------------------------------------------------------------------
76//
77// Default Constructor. hname is the name of the histogram class which
78// is in the array.
79//
80// istempl=kTRUE tells MHArray to use the first histogram retrieved from the
81// ParameterList by hname to be used as a template. New histograms are not
82// created using the root dictionary, but the New-member function (see
83// MParConatiner)
84// In the case istempl=kFALSE new histograms are created using the root
85// dictionary while hname is the class name. For the creation their
86// default constructor is used.
87//
88MHArray::MHArray(const TString hname, Bool_t istempl, const char *name, const char *title)
89 : fIdx(0), fClass(NULL), fTemplate(NULL)
90{
91 //
92 // set the name and title of this object
93 //
94 fName = name ? name : "MHArray";
95 fTitle = title ? TString(title) : (TString("Base class for Mars histogram arrays:") + hname);
96
97 fArray = new TList;
98 fArray->SetOwner();
99
100 if (istempl)
101 {
102 fTemplateName = hname;
103 return;
104 }
105
106 //
107 // try to get class from root environment
108 //
109 fClass = gROOT->GetClass(hname);
110 if (!fClass)
111 {
112 //
113 // if class is not existing in the root environment
114 //
115 *fLog << err << dbginf << "Class '" << hname << "' not existing in dictionary." << endl;
116 }
117
118 //
119 // check for ineritance from MH
120 //
121 if (!fClass->InheritsFrom(MH::Class()))
122 {
123 //
124 // if class doesn't inherit from MH --> error
125 //
126 *fLog << err << dbginf << "Class '" << hname << "' doesn't inherit from MH." << endl;
127 fClass = NULL;
128 }
129}
130
131// --------------------------------------------------------------------------
132//
133// Destructor: Deleteing the array and all histograms which are part of the
134// array.
135//
136MHArray::~MHArray()
137{
138 fArray->Delete();
139 delete fArray;
140}
141
142
143MH &MHArray::operator[](Int_t i)
144{
145 return *(MH*)fArray->At(i);
146}
147
148MH *MHArray::At(Int_t i)
149{
150 return (MH*)fArray->At(i);
151}
152
153MH *MHArray::GetH()
154{
155 return (MH*)fArray->At(fIdx);
156}
157
158// --------------------------------------------------------------------------
159//
160// Tries to create a new histogram, adds it as last entry to the array
161// and tries to call SetupFill for it. In case of success the last entry
162// in the array is the new histogram and kTRUE is returned. Otherwise
163// kFALSE is returned.
164//
165Bool_t MHArray::CreateH()
166{
167 TString cname = fClass ? fClass->GetName() : fTemplate->IsA()->GetName();
168
169 MH *hist = NULL;
170 if (fTemplate)
171 {
172 hist = (MH*)fTemplate->New();
173 }
174 else
175 {
176 //
177 // create the parameter container of the the given class type
178 //
179 hist = (MH*)fClass->New();
180 }
181 if (!hist)
182 {
183 *fLog << err << dbginf << "Cannot create new instance of class '";
184 *fLog << cname << "' (Maybe no def. constructor)" << endl;
185 return kFALSE;
186 }
187
188 //
189 // Set the name of the container
190 //
191 if (!fTemplate)
192 {
193 TString name = TString(hist->GetName())+";";
194 name += fIdx;
195
196 hist->SetName(name);
197 }
198
199 //
200 // Try to setup filling for the histogram
201 //
202 if (!hist->SetupFill(fParList))
203 {
204 *fLog << err << dbginf << "SetupFill for new histogram of type '";
205 *fLog << cname << "' with Index #" << fIdx << " failed." << endl;
206 delete hist;
207 return kFALSE;
208 }
209
210 fArray->AddLast(hist);
211
212 return kTRUE;
213}
214
215// --------------------------------------------------------------------------
216//
217// Returns kFALSE if the class couldn't be found in the root dictionary or
218// if it doesn't inherit from MH.
219// The parameter list is remembert to be used for SetupFill in case a new
220// histogram is created.
221// The index is reset to 0
222//
223Bool_t MHArray::SetupFill(const MParList *pList)
224{
225 fParList = pList;
226 fIdx = 0;
227
228 if (!fTemplateName.IsNull())
229 {
230 fTemplate = (MH*)pList->FindObject(fTemplateName, "MH");
231 return fTemplate ? kTRUE : kFALSE;
232 }
233
234 return fClass ? kTRUE : kFALSE;
235}
236
237// --------------------------------------------------------------------------
238//
239// Call Fill for the present histogram index. If the index is out of
240// bounds the event is skipped. If the index is the number of current
241// histograms in the array a new histogram is created and if creation was
242// successfull filled.
243//
244Bool_t MHArray::Fill(const MParContainer *par)
245{
246 const Int_t n = fArray->GetSize();
247
248 if (fIdx <0 || fIdx>n)
249 {
250 *fLog << warn << "Histogram Index #" << fIdx << " out of bounds (>";
251 *fLog << n << ")... skipped." << endl;
252 return kCONTINUE;
253 }
254
255 if (fIdx==n)
256 if (!CreateH())
257 return kFALSE;
258
259 return GetH()->Fill(par);
260}
261
262// --------------------------------------------------------------------------
263//
264// Calls Finalize for all histograms in the list. If at least one Finalize
265// fails kFALSE is returned.
266//
267Bool_t MHArray::Finalize()
268{
269 Bool_t rc = kTRUE;
270
271 TIter Next(fArray);
272 MH *hist = NULL;
273
274 while ((hist=(MH*)Next()))
275 if (!hist->Finalize())
276 rc = kFALSE;
277
278 return rc;
279}
280
281// --------------------------------------------------------------------------
282//
283// Print the number of entries in the array
284//
285void MHArray::Print(Option_t *option="") const
286{
287 *fLog << all << GetDescriptor() << " contains " << fArray->GetSize();
288 *fLog << " histograms." << endl;
289}
290
291// --------------------------------------------------------------------------
292//
293// The option is the name of the histogram, used to get a histogram from
294// the MH entries by calling their GetHist function.
295//
296void MHArray::Draw(Option_t *opt="")
297{
298 if (!gPad)
299 MH::MakeDefCanvas(this);
300
301 const Stat_t sstyle = gStyle->GetOptStat();
302 gStyle->SetOptStat(0);
303
304 TIter Next(fArray);
305 MH *hist = (MH*)Next();
306
307 Int_t col=2;
308 Double_t max=0;
309 Double_t min=0;
310
311 TH1 *h1=NULL;
312
313 if (hist)
314 {
315 if ((h1 = hist->GetHistByName(opt)))
316 {
317 h1->Draw();
318 h1->SetLineColor(col++);
319 max = h1->GetMaximum();
320 min = h1->GetMinimum();
321 }
322 }
323
324 while ((hist=(MH*)Next()))
325 {
326 TH1 *h=NULL;
327
328 if (!(h = hist->GetHistByName(opt)))
329 continue;
330
331 h->Draw("same");
332 h->SetLineColor(col++);
333 if (max<h->GetMaximum())
334 max = h->GetMaximum();
335 if (min>h->GetMinimum())
336 min = h->GetMinimum();
337 }
338
339 if (h1)
340 {
341 h1->SetMinimum(min>0 ? min*0.95 : min*1.05);
342 h1->SetMaximum(max>0 ? max*1.05 : max*0.95);
343 }
344 gPad->Modified();
345 gPad->Update();
346
347 gStyle->SetOptStat(sstyle);
348}
349
350// --------------------------------------------------------------------------
351//
352// The option is the name of the histogram, used to get a histogram from
353// the MH entries by calling their GetHistByName function.
354// If the option also contains 'nonew' no new canvas is created.
355//
356TObject *MHArray::DrawClone(Option_t *opt="") const
357{
358 TString o(opt);
359
360 TCanvas *c = NULL;
361
362 Int_t nonew = o.Index("nonew", TString::kIgnoreCase);
363 if (nonew>=0)
364 {
365 c = MH::MakeDefCanvas(this);
366
367 //
368 // This is necessary to get the expected bahviour of DrawClone
369 //
370 gROOT->SetSelectedPad(NULL);
371
372 o.Remove(nonew, 5);
373 }
374
375 const Stat_t sstyle = gStyle->GetOptStat();
376 gStyle->SetOptStat(0);
377
378 TIter Next(fArray);
379 MH *hist = (MH*)Next();
380
381 Int_t col=2;
382 Double_t max=0;
383 Double_t min=0;
384
385 TH1 *h1=NULL;
386
387 if (hist)
388 {
389 if ((h1 = hist->GetHistByName(o)))
390 {
391 h1 = (TH1*)h1->DrawCopy();
392 h1->SetMarkerColor(col);
393 h1->SetLineColor(col++);
394 h1->SetFillStyle(4000);
395 max = h1->GetMaximum();
396 min = h1->GetMinimum();
397 }
398 }
399
400 while ((hist=(MH*)Next()))
401 {
402 TH1 *h=NULL;
403
404 if (!(h = hist->GetHistByName(o)))
405 continue;
406
407 h = (TH1*)h->DrawCopy("same");
408 h->SetMarkerColor(col);
409 h->SetLineColor(col++);
410 h->SetFillStyle(4000);
411 if (max<h->GetMaximum())
412 max = h->GetMaximum();
413 if (min>h->GetMinimum())
414 min = h->GetMinimum();
415 }
416
417 if (h1)
418 {
419 h1->SetMinimum(min>0 ? min*0.95 : min*1.05);
420 h1->SetMaximum(max>0 ? max*1.05 : max*0.95);
421 }
422 gPad->Modified();
423 gPad->Update();
424
425 gStyle->SetOptStat(sstyle);
426
427 return c;
428}
Note: See TracBrowser for help on using the repository browser.