source: trunk/MagicSoft/Mars/mhbase/MH3.cc@ 5250

Last change on this file since 5250 was 4928, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 19.2 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//
51// Axis titles
52// ===========
53//
54// 1) If no other title is given the rule for this axis is used.
55// 2) If the MBinning used for this axis has a non-default Title
56// (MBinning::HasTitle) this title is used for the corresponding axis
57// 3) If the MH3 has a non-default title (MH3::SetTitle called)
58// this title is set as the histogram title. It can be used to overwrite
59// the axis titles. For more information see TH1::SetTitle, eg.
60// SetTitle("MyHist;x[mm];y[cm];Counts");
61//
62// For example:
63// MH3 myhist("MHillas.fLength");
64// myhist.SetName("MyHist");
65// myhist.SetScaleX(geomcam.GetConvMm2Deg()); //convert length to degree
66// MBinning bins("BinningMyHistX");
67// bins.SetEdges(10, 0, 150);
68// plist.AddToList(&myhist);
69// plist.AddToList(&bins);
70//
71/////////////////////////////////////////////////////////////////////////////
72#include "MH3.h"
73
74#include <ctype.h> // tolower
75#include <fstream>
76
77#include <TPad.h>
78#include <TStyle.h>
79#include <TCanvas.h>
80
81#include <TH2.h>
82#include <TH3.h>
83#include <TProfile.h>
84#include <TProfile2D.h>
85
86#include "MLog.h"
87#include "MLogManip.h"
88
89#include "MParList.h"
90#include "MBinning.h"
91#include "MDataChain.h"
92
93ClassImp(MH3);
94
95using namespace std;
96
97const TString MH3::gsDefName = "MH3";
98const TString MH3::gsDefTitle = "Container for a n-D Mars Histogram";
99
100// --------------------------------------------------------------------------
101//
102// Default constructor.
103//
104MH3::MH3(const unsigned int dim)
105 : fDimension(dim>3?3:dim), fHist(NULL),
106 fNameProfX(Form("ProfX_%p", this)),
107 fNameProfY(Form("ProfY_%p", this))
108{
109 switch (fDimension)
110 {
111 case 1:
112 fHist = new TH1F;
113 fHist->SetYTitle("Counts");
114 break;
115 case 2:
116 fHist = new TH2F;
117 fHist->SetZTitle("Counts");
118 break;
119 case 3:
120 fHist = new TH3F;
121 break;
122 }
123
124 fData[0] = NULL;
125 fData[1] = NULL;
126 fData[2] = NULL;
127
128 fName = gsDefName;
129 fTitle = gsDefTitle;
130
131 if (fHist)
132 {
133 fHist->SetDirectory(NULL);
134 fHist->UseCurrentStyle();
135 }
136
137 fScale[0] = 1;
138 fScale[1] = 1;
139 fScale[2] = 1;
140}
141
142// --------------------------------------------------------------------------
143//
144// Creates an TH1F. memberx is filled into the X-bins. For a more detailed
145// description see the class description above.
146//
147MH3::MH3(const char *memberx)
148 : fDimension(1)
149{
150 fHist = new TH1F;
151
152 fData[0] = new MDataChain(memberx);
153 fData[1] = NULL;
154 fData[2] = NULL;
155
156 fName = gsDefName;
157 fTitle = gsDefTitle;
158
159 fHist->UseCurrentStyle();
160 fHist->SetDirectory(NULL);
161 fHist->SetYTitle("Counts");
162
163 fScale[0] = 1;
164 fScale[1] = 1;
165 fScale[2] = 1;
166}
167
168// --------------------------------------------------------------------------
169//
170// Creates an TH2F. memberx is filled into the X-bins. membery is filled
171// into the Y-bins. For a more detailed description see the class
172// description above.
173//
174MH3::MH3(const char *memberx, const char *membery)
175 : fDimension(2), fNameProfX(Form("ProjX_%p", this)), fNameProfY(Form("ProjY_%p", this))
176{
177 fHist = new TH2F;
178
179 fData[0] = new MDataChain(memberx);
180 fData[1] = new MDataChain(membery);
181 fData[2] = NULL;
182
183 fName = gsDefName;
184 fTitle = gsDefTitle;
185
186 fHist->UseCurrentStyle();
187 fHist->SetDirectory(NULL);
188 fHist->SetZTitle("Counts");
189
190 fScale[0] = 1;
191 fScale[1] = 1;
192 fScale[2] = 1;
193}
194
195// --------------------------------------------------------------------------
196//
197// Creates an TH3F. memberx is filled into the X-bins. membery is filled
198// into the Y-bins. membery is filled into the Z-bins. For a more detailed
199// description see the class description above.
200//
201MH3::MH3(const char *memberx, const char *membery, const char *memberz)
202 : fDimension(3)
203{
204 fHist = new TH3F;
205
206 fData[0] = new MDataChain(memberx);
207 fData[1] = new MDataChain(membery);
208 fData[2] = new MDataChain(memberz);
209
210 fName = gsDefName;
211 fTitle = gsDefTitle;
212
213 fHist->UseCurrentStyle();
214 fHist->SetDirectory(NULL);
215
216 fScale[0] = 1;
217 fScale[1] = 1;
218 fScale[2] = 1;
219}
220
221// --------------------------------------------------------------------------
222//
223// Deletes the histogram
224//
225MH3::~MH3()
226{
227 delete fHist;
228
229 for (int i=0; i<3; i++)
230 if (fData[i])
231 delete fData[i];
232}
233
234// --------------------------------------------------------------------------
235//
236// Return the data members used by the data chain to be used in
237// MTask::AddBranchToList
238//
239TString MH3::GetDataMember() const
240{
241 TString str=fData[0]->GetDataMember();
242 if (fData[1])
243 {
244 str += ";";
245 str += fData[1]->GetDataMember();
246 }
247 if (fData[2])
248 {
249 str += ";";
250 str += fData[2]->GetDataMember();
251 }
252 return str;
253}
254
255// --------------------------------------------------------------------------
256//
257// Setup the Binning for the histograms automatically if the correct
258// instances of MBinning are found in the parameter list
259// For a more detailed description see class description above.
260//
261Bool_t MH3::SetupFill(const MParList *plist)
262{
263 // reset histogram (necessary if the same eventloop is run more than once)
264 fHist->Reset();
265
266 TString bname("Binning");
267 bname += fName;
268
269 MBinning *binsx = NULL;
270 MBinning *binsy = NULL;
271 MBinning *binsz = NULL;
272
273 switch (fDimension)
274 {
275 case 3:
276 binsz = (MBinning*)plist->FindObject(bname+"Z", "MBinning");
277 if (!binsz)
278 {
279 *fLog << err << dbginf << "MBinning '" << bname << "X' not found... aborting." << endl;
280 return kFALSE;
281 }
282 if (fData[2] && !fData[2]->PreProcess(plist))
283 return kFALSE;
284 if (fData[2])
285 fHist->SetZTitle(fData[2]->GetTitle());
286 if (binsz->HasTitle())
287 fHist->SetZTitle(binsz->GetTitle());
288 if (binsz->IsLogarithmic())
289 fHist->SetBit(kIsLogz);
290 case 2:
291 binsy = (MBinning*)plist->FindObject(bname+"Y", "MBinning");
292 if (!binsy)
293 {
294 *fLog << err << dbginf << "MBinning '" << bname << "Y' not found... aborting." << endl;
295 return kFALSE;
296 }
297 if (fData[1] && !fData[1]->PreProcess(plist))
298 return kFALSE;
299 if (fData[1])
300 fHist->SetYTitle(fData[1]->GetTitle());
301 if (binsy->HasTitle())
302 fHist->SetYTitle(binsy->GetTitle());
303 if (binsy->IsLogarithmic())
304 fHist->SetBit(kIsLogy);
305 case 1:
306 binsx = (MBinning*)plist->FindObject(bname+"X", "MBinning");
307 if (!binsx)
308 {
309 if (fDimension==1)
310 binsx = (MBinning*)plist->FindObject(bname, "MBinning");
311
312 if (!binsx)
313 {
314 *fLog << err << dbginf << "Neither MBinning '" << bname << "X' nor '" << bname << "' found... aborting." << endl;
315 return kFALSE;
316 }
317 }
318 if (fData[0] && !fData[0]->PreProcess(plist))
319 return kFALSE;
320 if (fData[0]!=NULL)
321 fHist->SetXTitle(fData[0]->GetTitle());
322 if (binsx->HasTitle())
323 fHist->SetXTitle(binsx->GetTitle());
324 if (binsx->IsLogarithmic())
325 fHist->SetBit(kIsLogx);
326 }
327
328 TString title("Histogram for ");
329 title += fName;
330 title += Form(" (%dD)", fDimension);
331
332 fHist->SetName(fName);
333 fHist->SetTitle(fTitle==gsDefTitle ? title : fTitle);
334 fHist->SetDirectory(0);
335
336 switch (fDimension)
337 {
338 case 1:
339 SetBinning(fHist, binsx);
340 return kTRUE;
341 case 2:
342 SetBinning((TH2*)fHist, binsx, binsy);
343 return kTRUE;
344 case 3:
345 SetBinning((TH3*)fHist, binsx, binsy, binsz);
346 return kTRUE;
347 }
348
349 *fLog << err << "ERROR - MH3 has " << fDimension << " dimensions!" << endl;
350 return kFALSE;
351}
352
353// --------------------------------------------------------------------------
354//
355// Set the name of the histogram ant the MH3 container
356//
357void MH3::SetName(const char *name)
358{
359 if (fHist)
360 {
361 fHist->SetName(name);
362 fHist->SetDirectory(0);
363 }
364 MParContainer::SetName(name);
365}
366
367// --------------------------------------------------------------------------
368//
369// Set the title of the histogram ant the MH3 container
370//
371void MH3::SetTitle(const char *title)
372{
373 if (fHist)
374 fHist->SetTitle(title);
375 MParContainer::SetTitle(title);
376}
377
378// --------------------------------------------------------------------------
379//
380// Fills the one, two or three data members into our histogram
381//
382Bool_t MH3::Fill(const MParContainer *par, const Stat_t w)
383{
384 Double_t x=0;
385 Double_t y=0;
386 Double_t z=0;
387
388 switch (fDimension)
389 {
390 case 3:
391 z = fData[2]->GetValue()*fScale[2];
392 case 2:
393 y = fData[1]->GetValue()*fScale[1];
394 case 1:
395 x = fData[0]->GetValue()*fScale[0];
396 }
397
398 switch (fDimension)
399 {
400 case 3:
401 ((TH3*)fHist)->Fill(x, y, z, w);
402 return kTRUE;
403 case 2:
404 ((TH2*)fHist)->Fill(x, y, w);
405 return kTRUE;
406 case 1:
407 fHist->Fill(x, w);
408 return kTRUE;
409 }
410
411 return kFALSE;
412}
413/*
414// --------------------------------------------------------------------------
415//
416// Set the palette you wanna use:
417// - you could set the root "Pretty Palette Violet->Red" by
418// gStyle->SetPalette(1, 0), but in some cases this may look
419// confusing
420// - The maximum colors root allowes us to set by ourself
421// is 50 (idx: 51-100). This colors are set to a grayscaled
422// palette
423// - the number of contours must be two less than the number
424// of palette entries
425//
426void MHStarMap::PrepareDrawing() const
427{
428 const Int_t numg = 32; // number of gray scaled colors
429 const Int_t numw = 32; // number of white
430
431 Int_t palette[numg+numw];
432
433 //
434 // The first half of the colors are white.
435 // This is some kind of optical background supression
436 //
437 gROOT->GetColor(51)->SetRGB(1, 1, 1);
438
439 Int_t i;
440 for (i=0; i<numw; i++)
441 palette[i] = 51;
442
443 //
444 // now the (gray) scaled part is coming
445 //
446 for (;i<numw+numg; i++)
447 {
448 const Float_t gray = 1.0-(float)(i-numw)/(numg-1.0);
449
450 gROOT->GetColor(52+i)->SetRGB(gray, gray, gray);
451 palette[i] = 52+i;
452 }
453
454 //
455 // Set the palette and the number of contour levels
456 //
457 gStyle->SetPalette(numg+numw, palette);
458 fStarMap->SetContour(numg+numw-2);
459}
460*/
461// --------------------------------------------------------------------------
462//
463// Setup a inversed deep blue sea palette for the fCenter histogram.
464//
465void MH3::SetColors() const
466{
467 // FIXME: This must be redone each time the canvas is repainted....
468 gStyle->SetPalette(51, NULL);
469 Int_t c[50];
470 for (int i=0; i<50; i++)
471 c[49-i] = gStyle->GetColorPalette(i);
472 gStyle->SetPalette(50, c);
473}
474
475void MH3::Paint(Option_t *o)
476{
477 TString str(o);
478
479 // FIXME: Do it in Paint()
480 if (str.Contains("COL", TString::kIgnoreCase))
481 SetColors();
482
483 if (fHist->TestBit(kIsLogx) && fHist->GetEntries()>0) gPad->SetLogx();
484 if (fHist->TestBit(kIsLogy) && fHist->GetEntries()>0) gPad->SetLogy();
485 if (fHist->TestBit(kIsLogz) && fHist->GetEntries()>0) gPad->SetLogz();
486
487 // Set pretty color palette
488 gStyle->SetPalette(1, 0);
489
490 TVirtualPad *padsave = gPad;
491
492 TProfile* h0;
493 if ((h0 = (TProfile*)gPad->FindObject(fNameProfX)))
494 {
495 // Get projection for range
496 TProfile *p = ((TH2*)fHist)->ProfileX(fNameProfX, -1, 9999, "s");
497
498 // Move contents from projection to h3
499 h0->Reset();
500 h0->Add(p);
501 delete p;
502
503 // Set Minimum as minimum value Greater Than 0
504 //h0->SetMinimum(GetMinimumGT(*h0));
505 }
506 if ((h0 = (TProfile*)gPad->FindObject(fNameProfY)))
507 {
508 // Get projection for range
509 TProfile *p = ((TH2*)fHist)->ProfileY(fNameProfY, -1, 9999, "s");
510
511 // Move contents from projection to h3
512 h0->Reset();
513 h0->Add(p);
514 delete p;
515
516 // Set Minimum as minimum value Greater Than 0
517 //h0->SetMinimum(GetMinimumGT(*h0));
518 }
519
520 gPad = padsave;
521}
522
523// --------------------------------------------------------------------------
524//
525// Creates a new canvas and draws the histogram into it.
526//
527// Possible options are:
528// PROFX: Draw a x-profile into the histogram (for 2D histograms only)
529// PROFY: Draw a y-profile into the histogram (for 2D histograms only)
530// ONLY: Draw the profile histogram only (for 2D histograms only)
531//
532// If the kIsLog?-Bit is set the axis is displayed lkogarithmically.
533// eg this is set when applying a logarithmic MBinning
534//
535// Be careful: The histogram belongs to this object and won't get deleted
536// together with the canvas.
537//
538void MH3::Draw(Option_t *opt)
539{
540 TVirtualPad *pad = gPad ? gPad : MakeDefCanvas(fHist);
541 pad->SetBorderMode(0);
542
543 fHist->SetFillStyle(4000);
544
545 TString str(opt);
546 str = str.Strip(TString::kBoth);
547
548 Bool_t only = str.Contains("ONLY", TString::kIgnoreCase) && fDimension==2;
549 // FIXME: We may have to remove all our own options from str!
550 if (!only)
551 fHist->Draw(str);
552
553 if (str.Contains("PROFX", TString::kIgnoreCase) && fDimension==2)
554 {
555 TProfile *p = ((TH2*)fHist)->ProfileX(fNameProfX, -1, 9999, "s");
556 p->SetLineColor(kBlue);
557 p->Draw(only?"":"same");
558 p->SetBit(kCanDelete);
559 p->SetDirectory(NULL);
560 }
561 if (str.Contains("PROFY", TString::kIgnoreCase) && fDimension==2)
562 {
563 TProfile *p = ((TH2*)fHist)->ProfileY(fNameProfY, -1, 9999, "s");
564 p->SetLineColor(kBlue);
565 p->Draw(only?"":"same");
566 p->SetBit(kCanDelete);
567 p->SetDirectory(NULL);
568 }
569
570 AppendPad("");
571}
572
573// --------------------------------------------------------------------------
574//
575// Implementation of SavePrimitive. Used to write the call to a constructor
576// to a macro. In the original root implementation it is used to write
577// gui elements to a macro-file.
578//
579void MH3::StreamPrimitive(ofstream &out) const
580{
581 TString name = GetUniqueName();
582
583 out << " MH3 " << name << "(\"";
584 out << fData[0]->GetRule() << "\"";
585 if (fDimension>1)
586 out << ", \"" << fData[1]->GetRule() << "\"";
587 if (fDimension>2)
588 out << ", \"" << fData[2]->GetRule() << "\"";
589
590 out << ");" << endl;
591
592 if (fName!=gsDefName)
593 out << " " << name << ".SetName(\"" << fName << "\");" << endl;
594
595 if (fTitle!=gsDefTitle)
596 out << " " << name << ".SetTitle(\"" << fTitle << "\");" << endl;
597
598 switch (fDimension)
599 {
600 case 3:
601 if (fScale[2]!=1)
602 out << " " << name << ".SetScaleZ(" << fScale[2] << ");" << endl;
603 case 2:
604 if (fScale[1]!=1)
605 out << " " << name << ".SetScaleY(" << fScale[1] << ");" << endl;
606 case 1:
607 if (fScale[0]!=1)
608 out << " " << name << ".SetScaleX(" << fScale[0] << ");" << endl;
609 }
610}
611
612// --------------------------------------------------------------------------
613//
614// Used to rebuild a MH3 object of the same type (data members,
615// dimension, ...)
616//
617MParContainer *MH3::New() const
618{
619 MH3 *h = NULL;
620
621 if (fData[0] == NULL)
622 h=new MH3(fDimension);
623 else
624 switch (fDimension)
625 {
626 case 1:
627 h=new MH3(fData[0]->GetRule());
628 break;
629 case 2:
630 h=new MH3(fData[0]->GetRule(), fData[1]->GetRule());
631 break;
632 case 3:
633 h=new MH3(fData[0]->GetRule(), fData[1]->GetRule(), fData[2]->GetRule());
634 break;
635 }
636 switch (fDimension)
637 {
638 case 3:
639 h->SetScaleZ(fScale[2]);
640 case 2:
641 h->SetScaleY(fScale[1]);
642 case 1:
643 h->SetScaleX(fScale[0]);
644 }
645 return h;
646}
647
648TString MH3::GetRule(const Char_t axis) const
649{
650 switch (tolower(axis))
651 {
652 case 'x':
653 return fData[0] ? fData[0]->GetRule() : TString("");
654 case 'y':
655 return fData[1] ? fData[1]->GetRule() : TString("");
656 case 'z':
657 return fData[2] ? fData[2]->GetRule() : TString("");
658 default:
659 return "<n/a>";
660 }
661}
662
663// --------------------------------------------------------------------------
664//
665// Returns the total number of bins in a histogram (excluding under- and
666// overflow bins)
667//
668Int_t MH3::GetNbins() const
669{
670 Int_t num = 1;
671
672 switch (fDimension)
673 {
674 case 3:
675 num *= fHist->GetNbinsZ()+2;
676 case 2:
677 num *= fHist->GetNbinsY()+2;
678 case 1:
679 num *= fHist->GetNbinsX()+2;
680 }
681
682 return num;
683}
684
685// --------------------------------------------------------------------------
686//
687// Returns the total number of bins in a histogram (excluding under- and
688// overflow bins) Return -1 if bin is underflow or overflow
689//
690Int_t MH3::FindFixBin(Double_t x, Double_t y, Double_t z) const
691{
692 const TAxis &axex = *fHist->GetXaxis();
693 const TAxis &axey = *fHist->GetYaxis();
694 const TAxis &axez = *fHist->GetZaxis();
695
696 Int_t binz = 0;
697 Int_t biny = 0;
698 Int_t binx = 0;
699
700 switch (fDimension)
701 {
702 case 3:
703 binz = axez.FindFixBin(z);
704 if (binz>axez.GetLast() || binz<axez.GetFirst())
705 return -1;
706 case 2:
707 biny = axey.FindFixBin(y);
708 if (biny>axey.GetLast() || biny<axey.GetFirst())
709 return -1;
710 case 1:
711 binx = axex.FindFixBin(x);
712 if (binx<axex.GetFirst() || binx>axex.GetLast())
713 return -1;
714 }
715
716 const Int_t nx = fHist->GetNbinsX()+2;
717 const Int_t ny = fHist->GetNbinsY()+2;
718
719 return binx + nx*(biny +ny*binz);
720}
Note: See TracBrowser for help on using the repository browser.