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

Last change on this file since 5300 was 5300, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 18.9 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 /*
481 if (str.Contains("COL", TString::kIgnoreCase))
482 {
483 SetColors();
484 return;
485 }
486 */
487
488 if (str.Contains("log", TString::kIgnoreCase))
489 {
490 if (fHist->TestBit(kIsLogx) && fHist->GetEntries()>0) gPad->SetLogx();
491 if (fHist->TestBit(kIsLogy) && fHist->GetEntries()>0) gPad->SetLogy();
492 if (fHist->TestBit(kIsLogz) && fHist->GetEntries()>0) gPad->SetLogz();
493 }
494
495 if (str.Contains("upd", TString::kIgnoreCase))
496 {
497
498 // Set pretty color palette
499 // gStyle->SetPalette(1, 0);
500
501 TProfile* h0;
502 if ((h0 = (TProfile*)gPad->FindObject(fNameProfX)))
503 ((TH2*)fHist)->ProfileX(fNameProfX, -1, 9999, "s");
504 if ((h0 = (TProfile*)gPad->FindObject(fNameProfY)))
505 ((TH2*)fHist)->ProfileY(fNameProfY, -1, 9999, "s");
506 }
507}
508
509// --------------------------------------------------------------------------
510//
511// Creates a new canvas and draws the histogram into it.
512//
513// Possible options are:
514// PROFX: Draw a x-profile into the histogram (for 2D histograms only)
515// PROFY: Draw a y-profile into the histogram (for 2D histograms only)
516// ONLY: Draw the profile histogram only (for 2D histograms only)
517//
518// If the kIsLog?-Bit is set the axis is displayed lkogarithmically.
519// eg this is set when applying a logarithmic MBinning
520//
521// Be careful: The histogram belongs to this object and won't get deleted
522// together with the canvas.
523//
524void MH3::Draw(Option_t *opt)
525{
526 TVirtualPad *pad = gPad ? gPad : MakeDefCanvas(fHist);
527 pad->SetBorderMode(0);
528
529 fHist->SetFillStyle(4000);
530
531 TString str(opt);
532 str = str.Strip(TString::kBoth);
533
534 Bool_t only = str.Contains("ONLY", TString::kIgnoreCase) && fDimension==2;
535 // FIXME: We may have to remove all our own options from str!
536 if (!only)
537 fHist->Draw(str);
538
539 AppendPad("upd");
540
541 if (str.Contains("PROFX", TString::kIgnoreCase) && fDimension==2)
542 {
543 TProfile *p = ((TH2*)fHist)->ProfileX(fNameProfX, -1, 9999, "s");
544 p->SetLineColor(kBlue);
545 p->Draw(only?"":"same");
546 p->SetBit(kCanDelete);
547 p->SetDirectory(NULL);
548 }
549 if (str.Contains("PROFY", TString::kIgnoreCase) && fDimension==2)
550 {
551 TProfile *p = ((TH2*)fHist)->ProfileY(fNameProfY, -1, 9999, "s");
552 p->UseCurrentStyle();
553 p->SetLineColor(kBlue);
554 p->Draw(only?"":"same");
555 p->SetBit(kCanDelete);
556 p->SetDirectory(NULL);
557 }
558
559 AppendPad("log");
560}
561
562// --------------------------------------------------------------------------
563//
564// Implementation of SavePrimitive. Used to write the call to a constructor
565// to a macro. In the original root implementation it is used to write
566// gui elements to a macro-file.
567//
568void MH3::StreamPrimitive(ofstream &out) const
569{
570 TString name = GetUniqueName();
571
572 out << " MH3 " << name << "(\"";
573 out << fData[0]->GetRule() << "\"";
574 if (fDimension>1)
575 out << ", \"" << fData[1]->GetRule() << "\"";
576 if (fDimension>2)
577 out << ", \"" << fData[2]->GetRule() << "\"";
578
579 out << ");" << endl;
580
581 if (fName!=gsDefName)
582 out << " " << name << ".SetName(\"" << fName << "\");" << endl;
583
584 if (fTitle!=gsDefTitle)
585 out << " " << name << ".SetTitle(\"" << fTitle << "\");" << endl;
586
587 switch (fDimension)
588 {
589 case 3:
590 if (fScale[2]!=1)
591 out << " " << name << ".SetScaleZ(" << fScale[2] << ");" << endl;
592 case 2:
593 if (fScale[1]!=1)
594 out << " " << name << ".SetScaleY(" << fScale[1] << ");" << endl;
595 case 1:
596 if (fScale[0]!=1)
597 out << " " << name << ".SetScaleX(" << fScale[0] << ");" << endl;
598 }
599}
600
601// --------------------------------------------------------------------------
602//
603// Used to rebuild a MH3 object of the same type (data members,
604// dimension, ...)
605//
606MParContainer *MH3::New() const
607{
608 MH3 *h = NULL;
609
610 if (fData[0] == NULL)
611 h=new MH3(fDimension);
612 else
613 switch (fDimension)
614 {
615 case 1:
616 h=new MH3(fData[0]->GetRule());
617 break;
618 case 2:
619 h=new MH3(fData[0]->GetRule(), fData[1]->GetRule());
620 break;
621 case 3:
622 h=new MH3(fData[0]->GetRule(), fData[1]->GetRule(), fData[2]->GetRule());
623 break;
624 }
625 switch (fDimension)
626 {
627 case 3:
628 h->SetScaleZ(fScale[2]);
629 case 2:
630 h->SetScaleY(fScale[1]);
631 case 1:
632 h->SetScaleX(fScale[0]);
633 }
634 return h;
635}
636
637TString MH3::GetRule(const Char_t axis) const
638{
639 switch (tolower(axis))
640 {
641 case 'x':
642 return fData[0] ? fData[0]->GetRule() : TString("");
643 case 'y':
644 return fData[1] ? fData[1]->GetRule() : TString("");
645 case 'z':
646 return fData[2] ? fData[2]->GetRule() : TString("");
647 default:
648 return "<n/a>";
649 }
650}
651
652// --------------------------------------------------------------------------
653//
654// Returns the total number of bins in a histogram (excluding under- and
655// overflow bins)
656//
657Int_t MH3::GetNbins() const
658{
659 Int_t num = 1;
660
661 switch (fDimension)
662 {
663 case 3:
664 num *= fHist->GetNbinsZ()+2;
665 case 2:
666 num *= fHist->GetNbinsY()+2;
667 case 1:
668 num *= fHist->GetNbinsX()+2;
669 }
670
671 return num;
672}
673
674// --------------------------------------------------------------------------
675//
676// Returns the total number of bins in a histogram (excluding under- and
677// overflow bins) Return -1 if bin is underflow or overflow
678//
679Int_t MH3::FindFixBin(Double_t x, Double_t y, Double_t z) const
680{
681 const TAxis &axex = *fHist->GetXaxis();
682 const TAxis &axey = *fHist->GetYaxis();
683 const TAxis &axez = *fHist->GetZaxis();
684
685 Int_t binz = 0;
686 Int_t biny = 0;
687 Int_t binx = 0;
688
689 switch (fDimension)
690 {
691 case 3:
692 binz = axez.FindFixBin(z);
693 if (binz>axez.GetLast() || binz<axez.GetFirst())
694 return -1;
695 case 2:
696 biny = axey.FindFixBin(y);
697 if (biny>axey.GetLast() || biny<axey.GetFirst())
698 return -1;
699 case 1:
700 binx = axex.FindFixBin(x);
701 if (binx<axex.GetFirst() || binx>axex.GetLast())
702 return -1;
703 }
704
705 const Int_t nx = fHist->GetNbinsX()+2;
706 const Int_t ny = fHist->GetNbinsY()+2;
707
708 return binx + nx*(biny +ny*binz);
709}
Note: See TracBrowser for help on using the repository browser.