source: trunk/MagicSoft/Mars/mhist/MH3.cc@ 1788

Last change on this file since 1788 was 1666, checked in by tbretz, 22 years ago
*** empty log message ***
File size: 16.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 2002 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2002
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MH3
28//
29// With this histogram you can fill a histogram with up to three
30// variables from Mars parameter containers in an eventloop.
31//
32// In the constructor you can give up to three variables which should be
33// filled in the histogram. Dependend on the number of given variables
34// (data members) a TH1F, TH2F or TH3F is created.
35// Specify the data mamber like the following:
36// "MHillas.fLength"
37// Where MHillas is the name of the parameter container in the parameter
38// list and fLength is the name of the data member which should be filled
39// in the histogram. Assuming that your MHillas container has a different
40// name (MyHillas) the name to give would be:
41// "MyHillas.fLength"
42//
43// The axis binning is retrieved from the parameter list, too. Create a
44// MBinning with the name "Binning" plus the name of your MH3 container
45// plus the axis name ("X", "Y" or "Z") and add it to the parameter list.
46//
47// If you want to use a different unit for histogramming use SetScaleX,
48// SetScaleY and SetScaleZ.
49//
50// For example:
51// MH3 myhist("MHillas.fLength");
52// myhist.SetName("MyHist");
53// myhist.SetScaleX(geomcam.GetConvMm2Deg()); //convert length to degree
54// MBinning bins("BinningMyHistX");
55// bins.SetEdges(10, 0, 150);
56// plist.AddToList(&myhist);
57// plist.AddToList(&bins);
58//
59/////////////////////////////////////////////////////////////////////////////
60#include "MH3.h"
61
62#include <fstream.h>
63
64#include <TPad.h>
65#include <TStyle.h>
66#include <TCanvas.h>
67
68#include <TH2.h>
69#include <TH3.h>
70#include <TProfile.h>
71#include <TProfile2D.h>
72
73#include "MLog.h"
74#include "MLogManip.h"
75
76#include "MParList.h"
77#include "MBinning.h"
78#include "MDataChain.h"
79
80ClassImp(MH3);
81
82static const TString gsDefName = "MH3";
83static const TString gsDefTitle = "Container for a %dD Mars Histogram";
84
85// --------------------------------------------------------------------------
86//
87// Default constructor.
88//
89MH3::MH3() : fDimension(0), fHist(NULL)
90{
91 fName = gsDefName;
92 fTitle = Form(gsDefTitle.Data(), 0);
93
94 fData[0] = fData[1] = fData[2] = NULL;
95 fScale[0] = fScale[1] = fScale[2] = 1;
96}
97
98// --------------------------------------------------------------------------
99//
100// Creates an TH1F. memberx is filled into the X-bins. For a more detailed
101// description see the class description above.
102//
103MH3::MH3(const char *memberx)
104 : fDimension(1)
105{
106 fHist = new TH1F;
107
108 fData[0] = new MDataChain(memberx);
109 fData[1] = NULL;
110 fData[2] = NULL;
111
112 fName = gsDefName;
113 fTitle = Form(gsDefTitle.Data(), 1);
114
115 fHist->SetDirectory(NULL);
116 fHist->SetYTitle("Counts");
117
118 fScale[0] = 1;
119 fScale[1] = 1;
120 fScale[2] = 1;
121}
122
123// --------------------------------------------------------------------------
124//
125// Creates an TH2F. memberx is filled into the X-bins. membery is filled
126// into the Y-bins. For a more detailed description see the class
127// description above.
128//
129MH3::MH3(const char *memberx, const char *membery)
130 : fDimension(2)
131{
132 fHist = new TH2F;
133
134 fData[0] = new MDataChain(memberx);
135 fData[1] = new MDataChain(membery);
136 fData[2] = NULL;
137
138 fName = gsDefName;
139 fTitle = Form(gsDefTitle.Data(), 2);
140
141 fHist->SetDirectory(NULL);
142 fHist->SetZTitle("Counts");
143
144 fScale[0] = 1;
145 fScale[1] = 1;
146 fScale[2] = 1;
147}
148
149// --------------------------------------------------------------------------
150//
151// Creates an TH3F. memberx is filled into the X-bins. membery is filled
152// into the Y-bins. membery is filled into the Z-bins. For a more detailed
153// description see the class description above.
154//
155MH3::MH3(const char *memberx, const char *membery, const char *memberz)
156 : fDimension(3)
157{
158 fHist = new TH3F;
159
160 fData[0] = new MDataChain(memberx);
161 fData[1] = new MDataChain(membery);
162 fData[2] = new MDataChain(memberz);
163
164 fName = gsDefName;
165 fTitle = Form(gsDefTitle.Data(), 3);
166
167 fHist->SetDirectory(NULL);
168
169 fScale[0] = 1;
170 fScale[1] = 1;
171 fScale[2] = 1;
172}
173
174// --------------------------------------------------------------------------
175//
176// Deletes the histogram
177//
178MH3::~MH3()
179{
180 delete fHist;
181
182 for (int i=0; i<3; i++)
183 if (fData[i])
184 delete fData[i];
185}
186
187// --------------------------------------------------------------------------
188//
189// Return the data members used by the data chain to be used in
190// MTask::AddBranchToList
191//
192TString MH3::GetDataMember() const
193{
194 TString str=fData[0]->GetDataMember();
195 if (fData[1])
196 {
197 str += ";";
198 str += fData[1]->GetDataMember();
199 }
200 if (fData[2])
201 {
202 str += ";";
203 str += fData[2]->GetDataMember();
204 }
205 return str;
206}
207
208// --------------------------------------------------------------------------
209//
210// Setup the Binning for the histograms automatically if the correct
211// instances of MBinning are found in the parameter list
212// For a more detailed description see class description above.
213//
214Bool_t MH3::SetupFill(const MParList *plist)
215{
216 TString bname("Binning");
217 bname += fName;
218
219 MBinning *binsx = NULL;
220 MBinning *binsy = NULL;
221 MBinning *binsz = NULL;
222 switch (fDimension)
223 {
224 case 3:
225 binsz = (MBinning*)plist->FindObject(bname+"Z", "MBinning");
226 if (!binsz)
227 {
228 *fLog << err << dbginf << "MBinning '" << bname << "X' not found... aborting." << endl;
229 return kFALSE;
230 }
231 if (binsz->IsLogarithmic())
232 fHist->SetBit(kIsLogz);
233 fHist->SetZTitle(fData[2]->GetTitle());
234 if (!fData[2]->PreProcess(plist))
235 return kFALSE;
236 case 2:
237 binsy = (MBinning*)plist->FindObject(bname+"Y", "MBinning");
238 if (!binsy)
239 {
240 *fLog << err << dbginf << "MBinning '" << bname << "Y' not found... aborting." << endl;
241 return kFALSE;
242 }
243 if (binsy->IsLogarithmic())
244 fHist->SetBit(kIsLogy);
245 fHist->SetYTitle(fData[1]->GetTitle());
246 if (!fData[1]->PreProcess(plist))
247 return kFALSE;
248 case 1:
249 binsx = (MBinning*)plist->FindObject(bname+"X", "MBinning");
250 if (!binsx)
251 {
252 if (fDimension==1)
253 binsx = (MBinning*)plist->FindObject(bname, "MBinning");
254
255 if (!binsx)
256 {
257 *fLog << err << dbginf << "Neither MBinning '" << bname << "X' nor '" << bname << "' found... aborting." << endl;
258 return kFALSE;
259 }
260 }
261 if (binsx->IsLogarithmic())
262 fHist->SetBit(kIsLogx);
263 fHist->SetXTitle(fData[0]->GetTitle());
264 if (!fData[0]->PreProcess(plist))
265 return kFALSE;
266 }
267
268 fHist->SetName(fName);
269
270 TString title("Histogram for ");
271 title += fName;
272
273 switch (fDimension)
274 {
275 case 1:
276 fHist->SetTitle(title+" (1D)");
277 SetBinning(fHist, binsx);
278 return kTRUE;
279 case 2:
280 fHist->SetTitle(title+" (2D)");
281 SetBinning((TH2*)fHist, binsx, binsy);
282 return kTRUE;
283 case 3:
284 fHist->SetTitle(title+" (3D)");
285 SetBinning((TH3*)fHist, binsx, binsy, binsz);
286 return kTRUE;
287 }
288
289 return kTRUE;
290}
291
292// --------------------------------------------------------------------------
293//
294// Set the name of the histogram ant the MH3 container
295//
296void MH3::SetName(const char *name)
297{
298 fHist->SetName(name);
299 MParContainer::SetName(name);
300}
301
302// --------------------------------------------------------------------------
303//
304// Set the title of the histogram ant the MH3 container
305//
306void MH3::SetTitle(const char *title)
307{
308 fHist->SetTitle(title);
309 MParContainer::SetTitle(title);
310}
311
312// --------------------------------------------------------------------------
313//
314// Fills the one, two or three data members into our histogram
315//
316Bool_t MH3::Fill(const MParContainer *par)
317{
318 Double_t x=0;
319 Double_t y=0;
320 Double_t z=0;
321
322 switch (fDimension)
323 {
324 case 3:
325 z = fData[2]->GetValue()*fScale[2];
326 case 2:
327 y = fData[1]->GetValue()*fScale[1];
328 case 1:
329 x = fData[0]->GetValue()*fScale[0];
330 }
331
332 switch (fDimension)
333 {
334 case 3:
335 ((TH3*)fHist)->Fill(x, y, z);
336 return kTRUE;
337 case 2:
338 ((TH2*)fHist)->Fill(x, y);
339 return kTRUE;
340 case 1:
341 fHist->Fill(x);
342 return kTRUE;
343 }
344
345 return kFALSE;
346}
347/*
348// --------------------------------------------------------------------------
349//
350// Set the palette you wanna use:
351// - you could set the root "Pretty Palette Violet->Red" by
352// gStyle->SetPalette(1, 0), but in some cases this may look
353// confusing
354// - The maximum colors root allowes us to set by ourself
355// is 50 (idx: 51-100). This colors are set to a grayscaled
356// palette
357// - the number of contours must be two less than the number
358// of palette entries
359//
360void MHStarMap::PrepareDrawing() const
361{
362 const Int_t numg = 32; // number of gray scaled colors
363 const Int_t numw = 32; // number of white
364
365 Int_t palette[numg+numw];
366
367 //
368 // The first half of the colors are white.
369 // This is some kind of optical background supression
370 //
371 gROOT->GetColor(51)->SetRGB(1, 1, 1);
372
373 Int_t i;
374 for (i=0; i<numw; i++)
375 palette[i] = 51;
376
377 //
378 // now the (gray) scaled part is coming
379 //
380 for (;i<numw+numg; i++)
381 {
382 const Float_t gray = 1.0-(float)(i-numw)/(numg-1.0);
383
384 gROOT->GetColor(52+i)->SetRGB(gray, gray, gray);
385 palette[i] = 52+i;
386 }
387
388 //
389 // Set the palette and the number of contour levels
390 //
391 gStyle->SetPalette(numg+numw, palette);
392 fStarMap->SetContour(numg+numw-2);
393}
394*/
395// --------------------------------------------------------------------------
396//
397// Setup a inversed deep blue sea palette for the fCenter histogram.
398//
399void MH3::SetColors() const
400{
401 // FIXME: This must be redone each time the canvas is repainted....
402 gStyle->SetPalette(51, NULL);
403 Int_t c[50];
404 for (int i=0; i<50; i++)
405 c[49-i] = gStyle->GetColorPalette(i);
406 gStyle->SetPalette(50, c);
407}
408
409// --------------------------------------------------------------------------
410//
411// Draw clone of histogram. So that the object can be deleted
412//
413// Possible options are:
414// PROFX: Draw a x-profile into the histogram (for 2D histograms only)
415// PROFY: Draw a y-profile into the histogram (for 2D histograms only)
416// ONLY: Draw the profile histogram only (for 2D histograms only)
417//
418// If the kIsLog?-Bit is set the axis is displayed lkogarithmically.
419// eg this is set when applying a logarithmic MBinning
420//
421// and the histogram is still visible in the canvas.
422// The cloned object are deleted together with the canvas if the canvas is
423// destroyed. If you want to handle destroying the canvas you can get a
424// pointer to it from this function
425//
426TObject *MH3::DrawClone(Option_t *opt) const
427{
428 TString str(opt);
429
430 TVirtualPad *c = gPad;
431 if (!str.Contains("nonew", TString::kIgnoreCase))
432 {
433 c = MH::MakeDefCanvas(fHist);
434
435 //
436 // This is necessary to get the expected bahviour of DrawClone
437 //
438 gROOT->SetSelectedPad(NULL);
439 }
440
441 if (str.Contains("COL", TString::kIgnoreCase))
442 SetColors();
443
444 Bool_t only = str.Contains("ONLY", TString::kIgnoreCase) && fDimension==2;
445 if (!only)
446 fHist->DrawCopy(opt);
447
448 if (str.Contains("PROFX", TString::kIgnoreCase) && fDimension==2)
449 {
450 TProfile *p = ((TH2*)fHist)->ProfileX("_pfx", -1, 9999, "s");
451 p->SetLineColor(kBlue);
452 p->Draw(only?"":"same");
453 p->SetBit(kCanDelete);
454 p->SetDirectory(NULL);
455 }
456 if (str.Contains("PROFY", TString::kIgnoreCase) && fDimension==2)
457 {
458 TProfile *p = ((TH2*)fHist)->ProfileY("_pfy", -1, 9999, "s");
459 p->SetLineColor(kBlue);
460 p->Draw(only?"":"same");
461 p->SetBit(kCanDelete);
462 p->SetDirectory(NULL);
463 }
464
465 if (fHist->TestBit(kIsLogx)) c->SetLogx();
466 if (fHist->TestBit(kIsLogy)) c->SetLogy();
467 if (fHist->TestBit(kIsLogz)) c->SetLogz();
468
469 c->Modified();
470 c->Update();
471
472 return c;
473}
474
475// --------------------------------------------------------------------------
476//
477// Creates a new canvas and draws the histogram into it.
478//
479// Possible options are:
480// PROFX: Draw a x-profile into the histogram (for 2D histograms only)
481// PROFY: Draw a y-profile into the histogram (for 2D histograms only)
482// ONLY: Draw the profile histogram only (for 2D histograms only)
483//
484// If the kIsLog?-Bit is set the axis is displayed lkogarithmically.
485// eg this is set when applying a logarithmic MBinning
486//
487// Be careful: The histogram belongs to this object and won't get deleted
488// together with the canvas.
489//
490void MH3::Draw(Option_t *opt)
491{
492 if (!gPad)
493 MH::MakeDefCanvas(fHist);
494
495 TString str(opt);
496
497 if (str.Contains("COL", TString::kIgnoreCase))
498 SetColors();
499
500 Bool_t only = str.Contains("ONLY", TString::kIgnoreCase) && fDimension==2;
501 if (!only)
502 fHist->Draw(opt);
503
504 if (str.Contains("PROFX", TString::kIgnoreCase) && fDimension==2)
505 {
506 TProfile *p = ((TH2*)fHist)->ProfileX("_pfx", -1, 9999, "s");
507 p->SetLineColor(kBlue);
508 p->Draw(only?"":"same");
509 p->SetBit(kCanDelete);
510 p->SetDirectory(NULL);
511 }
512 if (str.Contains("PROFY", TString::kIgnoreCase) && fDimension==2)
513 {
514 TProfile *p = ((TH2*)fHist)->ProfileY("_pfy", -1, 9999, "s");
515 p->SetLineColor(kBlue);
516 p->Draw(only?"":"same");
517 p->SetBit(kCanDelete);
518 p->SetDirectory(NULL);
519 }
520
521 if (fHist->TestBit(kIsLogx)) gPad->SetLogx();
522 if (fHist->TestBit(kIsLogy)) gPad->SetLogy();
523 if (fHist->TestBit(kIsLogz)) gPad->SetLogz();
524
525 gPad->Modified();
526 gPad->Update();
527}
528
529// --------------------------------------------------------------------------
530//
531// Implementation of SavePrimitive. Used to write the call to a constructor
532// to a macro. In the original root implementation it is used to write
533// gui elements to a macro-file.
534//
535void MH3::StreamPrimitive(ofstream &out) const
536{
537 TString name = GetUniqueName();
538
539 out << " MH3 " << name << "(\"";
540 out << fData[0]->GetRule() << "\"";
541 if (fDimension>1)
542 out << ", \"" << fData[1]->GetRule() << "\"";
543 if (fDimension>2)
544 out << ", \"" << fData[2]->GetRule() << "\"";
545
546 out << ");" << endl;
547
548 if (fName!=gsDefName)
549 out << " " << name << ".SetName(\"" << fName << "\");" << endl;
550
551 if (fTitle!=Form(gsDefTitle.Data(), fDimension))
552 out << " " << name << ".SetTitle(\"" << fTitle << "\");" << endl;
553
554 switch (fDimension)
555 {
556 case 3:
557 if (fScale[2]!=1)
558 out << " " << name << ".SetScaleZ(" << fScale[2] << ");" << endl;
559 case 2:
560 if (fScale[1]!=1)
561 out << " " << name << ".SetScaleY(" << fScale[1] << ");" << endl;
562 case 1:
563 if (fScale[0]!=1)
564 out << " " << name << ".SetScaleX(" << fScale[0] << ");" << endl;
565 }
566}
567
568// --------------------------------------------------------------------------
569//
570// Used to rebuild a MH3 object of the same type (data members,
571// dimension, ...)
572//
573MParContainer *MH3::New() const
574{
575 MH3 *h = NULL;
576 switch (fDimension)
577 {
578 case 1:
579 h=new MH3(fData[0]->GetRule());
580 break;
581 case 2:
582 h=new MH3(fData[0]->GetRule(), fData[1]->GetRule());
583 break;
584 case 3:
585 h=new MH3(fData[0]->GetRule(), fData[1]->GetRule(), fData[2]->GetRule());
586 break;
587 }
588 switch (fDimension)
589 {
590 case 3:
591 h->SetScaleZ(fScale[2]);
592 case 2:
593 h->SetScaleY(fScale[1]);
594 case 1:
595 h->SetScaleX(fScale[0]);
596 }
597 return h;
598}
Note: See TracBrowser for help on using the repository browser.