source: trunk/MagicSoft/Mars/mhbase/MHn.cc@ 8757

Last change on this file since 8757 was 8719, checked in by tbretz, 17 years ago
*** empty log message ***
File size: 13.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 2002 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2007
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MHn
28//
29//
30// Initialization
31// --------------
32//
33// MHn is a histogram class which derives from MH as all Mars histogram
34// classes do, i.e. to fill the histogram use MFillH. (Example below)
35//
36// After instantisation of MHn add the histograms of your interest using
37// AddHist. Before the next AddHist is called you can now set the options
38// of your histogram using InitName, InitTitle, SetLog, SetAutoRange,
39// SetScale and SetDrawOption.
40//
41//
42// Layout
43// ------
44//
45// The layout of the histograms on the screen depends on the number of
46// initialized histograms and the option SetLayout. For details see below.
47//
48// SetLayout(MHn::kSimple)
49// < default > SetLayout(MHn::kComplex)
50// ========================= ==========================
51// +-----------------+
52// |1 |
53// | |
54// | |
55// | |
56// | |
57// +-----------------+
58//
59// +-----------------+ +--------+--------+
60// |1 | |1 |2 |
61// | | | | |
62// +-----------------+ | | |
63// |2 | | | |
64// | | | | |
65// +-----------------+ +--------+--------+
66//
67// +--------+--------+ +-----+-----+-----+
68// |1 |2 | |1 |3 |
69// | | | | | |
70// +--------+--------+ +-----+ +
71// |3 | | |2 | |
72// | | | | | |
73// +--------+--------+ +-----+-----+-----+
74//
75// +--------+--------+ +------+----------+
76// |1 |2 | |1 |4 |
77// | | | +------+ |
78// +--------+--------+ |2 | |
79// |3 |4 | +------+ |
80// | | | |3 | |
81// +--------+--------+ +------+----------+
82//
83// +-----+-----+-----+ +--------+--------+
84// |1 |2 |3 | |1 |2 |
85// | | | | +--------+--------|
86// +-----+-----+-----+ |3 |4 |
87// |4 |5 | | +--------+ |
88// | | | | |5 | |
89// +-----+-----+-----+ +--------+--------+
90//
91// +-----+-----+-----+ +--------+--------+
92// |1 |2 |3 | |1 |2 |
93// | | | | +--------+--------+
94// +-----+-----+-----+ |3 |4 |
95// |4 |5 |6 | +--------+--------+
96// | | | | |5 |6 |
97// +-----+-----+-----+ +--------+--------+
98//
99//
100// For example:
101// ------------
102// // Instatiate the MHn class with default name and title
103// MHn hres("Energy2", "Energy Residual (lg E_{est} - lg E_{mc})");
104//
105// // Initialize your first histogram (here a 2D histogram with x- and y-axis)
106// hres.AddHist("MMcEvt.fEnergy", "log10(MEnergyEst.fVal)-log10(MMcEvt.fEnergy)");
107// // Initialize the name of the histogram (ResEmc) and the title of the
108// // binnings ((here BinningEnergy for x-axis and BinningEnergyResidual for y-axis)
109// hres.InitName("ResEmc;Energy;EnergyResidual");
110// // Initialize the title of the histogram and the axis titles
111// hres.InitTitle(";E_{mc} [GeV];\\Delta lg E;");
112// // Initialize the draw option for the histogram
113// hres.SetDrawOption("colz profx");
114// // for more details on the options and more options see below
115// // or the class reference of MH3
116//
117// // Initialize a second histogram
118// hres.AddHist("MPointingPos.fZd", "log10(MEnergyEst.fVal)-log10(MMcEvt.fEnergy)");
119// hres.InitName("ResTheta;Theta;EnergyResidual");
120// hres.InitTitle(";Zd [\\circ];\\Delta lg E;");
121// hres.SetDrawOption("colz profx");
122//
123// // Note that only AddHist is mandatory. All other options can be omitted
124//
125// // Initialize the filling task which can now be added to the tasklist
126// MFillH fill(&hres);
127// ...
128//
129/////////////////////////////////////////////////////////////////////////////
130#include "MHn.h"
131
132#include <TCanvas.h>
133
134#include "MLog.h"
135#include "MLogManip.h"
136
137#include "MH3.h"
138
139ClassImp(MHn);
140
141using namespace std;
142
143MHn::MHn(const char *name, const char *title) : fLayout(kSimple), fNum(0)
144{
145 fName = name ? name : "MHn";
146 fTitle = title ? title : "Generalized histogram class for up to six histograms";
147
148 memset(fHist, 0, 6*sizeof(MH3*));
149}
150
151MHn::~MHn()
152{
153 for (int i=0; i<fNum; i++)
154 if (fHist[i])
155 delete fHist[i];
156}
157
158// --------------------------------------------------------------------------
159//
160// If a histogram with the same name is found, the pad-number with a
161// training underscore is added.
162//
163// FIXME: How do we handle identical names with different axes?
164//
165void MHn::InitHist()
166{
167 TString name = fName;
168
169 for (int i=0; i<fNum; i++)
170 if (name==fHist[i]->GetName())
171 {
172 name += Form("_%d", fNum);
173 break;
174 }
175
176 fHist[fNum]->SetName(fName);
177 fHist[fNum]->SetTitle(fTitle);
178
179 fHist[fNum]->SetAutoRangeX();
180
181 fNum++;
182}
183
184// --------------------------------------------------------------------------
185//
186// Add a new 1D-MH3 histogram. An internal pointer is set to it, so that
187// InitName and InitTitle can be used for this histogram until a new
188// histogram is added using AddHist
189//
190// e.g. AddHist("MHillas.fSize")
191//
192Bool_t MHn::AddHist(const char *memberx)
193{
194 if (fNum==8)
195 {
196 *fLog << err << "ERROR - MHn doesn't support more than six histograms... AddHist ignored." << endl;
197 return kFALSE;
198 }
199
200 fHist[fNum] = new MH3(memberx);
201
202 InitHist();
203
204 return kTRUE;
205}
206
207// --------------------------------------------------------------------------
208//
209// Add a new 2D-MH3 histogram. An internal pointer is set to it, so that
210// InitName and InitTitle can be used for this histogram until a new
211// histogram is added using AddHist
212//
213// e.g. AddHist("MHillas.fLength", "MHillas.fSize")
214//
215Bool_t MHn::AddHist(const char *memberx, const char *membery)
216{
217 if (fNum==8)
218 {
219 *fLog << err << "ERROR - MHn doesn't support more than six histograms... AddHist ignored." << endl;
220 return kFALSE;
221 }
222
223 fHist[fNum] = new MH3(memberx, membery);
224
225 InitHist();
226
227 return kTRUE;
228}
229
230// --------------------------------------------------------------------------
231//
232// Add a new 3D-MH3 histogram. An internal pointer is set to it, so that
233// InitName and InitTitle can be used for this histogram until a new
234// histogram is added using AddHist
235//
236// e.g. AddHist("MHillas.fWidth", "MHillas.fLength", "MHillas.fSize")
237//
238Bool_t MHn::AddHist(const char *memberx, const char *membery, const char *memberz)
239{
240 if (fNum==8)
241 {
242 *fLog << err << "ERROR - MHn doesn't support more than six histograms... AddHist ignored." << endl;
243 return kFALSE;
244 }
245
246 fHist[fNum] = new MH3(memberx, membery, memberz);
247
248 InitHist();
249
250 return kTRUE;
251}
252
253// --------------------------------------------------------------------------
254//
255// Set the draw option of the n-th MH3. See MH3 for more details of the
256// meaning of it.
257//
258Bool_t MHn::SetDrawOption(Int_t n, const char *opt)
259{
260 if (n<0 || n>=fNum)
261 {
262 *fLog << err << "ERROR - Histogram " << n << " not yet initialized... SetDrawOption ignored." << endl;
263 return kFALSE;
264 }
265
266 fDrawOption[n] = opt;
267
268 return kTRUE;
269}
270
271// --------------------------------------------------------------------------
272//
273// Set the name of the n-th MH3. See MH3 for more details of the meaning
274// of the name. If the given name starts with a semicolon fName is prepend
275// to the string.
276//
277Bool_t MHn::InitName(Int_t n, const char *nam)
278{
279 if (n<0 || n>=fNum)
280 {
281 *fLog << err << "ERROR - Histogram " << n << " not yet initialized... InitName ignored." << endl;
282 return kFALSE;
283 }
284
285 TString name = TString(nam).Strip(TString::kBoth);
286
287 if (name[0]==';')
288 name.Prepend(fName);
289
290 fHist[n]->SetName(name);
291
292 return kTRUE;
293}
294
295// --------------------------------------------------------------------------
296//
297// Set the title of the n-th MH3. See MH3 for more details of the meaning
298// of the title. If the given title starts with a semicolon fTitle is prepend
299// to the string.
300//
301Bool_t MHn::InitTitle(Int_t n, const char *tit)
302{
303 if (n<0 || n>=fNum)
304 {
305 *fLog << err << "ERROR - Histogram " << n << " not yet initialized... InitTitle ignored." << endl;
306 return kFALSE;
307 }
308
309 TString title = TString(tit).Strip(TString::kBoth);
310
311 if (title[0]==';')
312 title.Prepend(fTitle);
313
314 fHist[n]->SetTitle(title);
315
316 return kTRUE;
317}
318
319// --------------------------------------------------------------------------
320//
321// Set additional scale factors for the current histogram
322//
323void MHn::SetScale(Double_t x, Double_t y, Double_t z) const
324{
325 if (fHist[fNum-1])
326 fHist[fNum-1]->SetScale(x, y, z);
327}
328
329// --------------------------------------------------------------------------
330//
331// Enable or disable displaying log-scale for the current histogram.
332// Normally it is retrieved from the corresponding MBinning
333//
334void MHn::SetLog(Bool_t x, Bool_t y, Bool_t z) const
335{
336 if (fHist[fNum-1])
337 fHist[fNum-1]->SetLog(x, y, z);
338}
339
340// --------------------------------------------------------------------------
341//
342// Set the axis range in Finalize automatically to the histogram region
343// containing contents. This is the default for the x-axis.
344// This function can be used to enable this behaviour also for the other
345// axis, or disable it for the x-axis of the current histogram.
346//
347void MHn::SetAutoRange(Bool_t x, Bool_t y, Bool_t z) const
348{
349 if (fHist[fNum-1])
350 fHist[fNum-1]->SetAutoRange(x, y, z);
351}
352
353// --------------------------------------------------------------------------
354//
355// Call Sumw2 for trhe current histogram, enabling errors.
356//
357void MHn::Sumw2() const
358{
359 if (fHist[fNum-1])
360 fHist[fNum-1]->Sumw2();
361}
362
363// --------------------------------------------------------------------------
364//
365// Force the given binning(s) for the current histogram. The binnings
366// do not get owned. The user is responsible for deleting them.
367//
368void MHn::SetBinnings(MBinning *x, MBinning *y, MBinning *z) const
369{
370 if (fHist[fNum-1])
371 fHist[fNum-1]->SetBinnings(x, y, z);
372}
373
374// --------------------------------------------------------------------------
375//
376// Call SetupFill for all initialized histograms
377//
378Bool_t MHn::SetupFill(const MParList *plist)
379{
380 for (int i=0; i<fNum; i++)
381 if (!fHist[i]->SetupFill(plist))
382 return kFALSE;
383
384 return kTRUE;
385}
386
387// --------------------------------------------------------------------------
388//
389// Call Fill for all initialized histograms
390//
391Bool_t MHn::Fill(const MParContainer *par, const Stat_t w)
392{
393 for (int i=0; i<fNum; i++)
394 if (!fHist[i]->Fill(par, w))
395 return kFALSE;
396
397 return kTRUE;
398}
399
400// --------------------------------------------------------------------------
401//
402// Call Finalize for all initialized histograms
403//
404Bool_t MHn::Finalize()
405{
406 for (int i=0; i<fNum; i++)
407 if (!fHist[i]->Finalize())
408 return kFALSE;
409
410 return kTRUE;
411}
412
413// --------------------------------------------------------------------------
414//
415void MHn::Draw(Option_t *opt)
416{
417 TVirtualPad *pad = gPad ? gPad : MakeDefCanvas(this);
418 pad->SetBorderMode(0);
419
420 const Bool_t same = TString(opt).Contains("same");
421
422 if (!same)
423 {
424 AppendPad();
425
426 const Int_t id = fNum + fLayout*10;
427
428 switch (id)
429 {
430 case 0: // 0
431 case 10: // 0
432 return;
433 case 1: // 1
434 case 11: // 1
435 break;
436
437 case 2: // 2
438 pad->Divide(1,2, 1e-5, 1e-5);
439 break;
440 case 3: // 3
441 pad->Divide(2,2, 1e-5, 1e-5);
442 delete pad->GetPad(4);
443 break;
444 case 4: // 4
445 pad->Divide(2,2, 1e-5, 1e-5);
446 break;
447 case 5: // 5
448 pad->Divide(3,2, 1e-5, 1e-5);
449 delete pad->GetPad(6);
450 break;
451 case 6: // 6
452 pad->Divide(3,2, 1e-5, 1e-5);
453 break;
454
455 case 12: // 2
456 pad->Divide(2,1, 1e-5, 1e-5);
457 break;
458 case 13: // 3
459 break;
460 case 14: // 4
461 pad->Divide(2,2, 1e-5, 1e-5);
462 break;
463 case 15: // 5
464 pad->Divide(2,3, 1e-5, 1e-5);
465 pad->GetPad(4)->SetPad(0.51, 0.01, 0.99, 0.65);
466 delete pad->GetPad(6);
467 break;
468 case 16: // 6
469 pad->Divide(2,3);
470 break;
471 }
472 }
473
474 for (int i=0; i<fNum; i++)
475 {
476 TString opt(fDrawOption[i]);
477 if (same)
478 opt += " same";
479
480 pad->cd(i+1);
481 fHist[i]->Draw(opt);
482 }
483}
Note: See TracBrowser for help on using the repository browser.