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

Last change on this file since 9492 was 9362, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 35.7 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-2008
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 TH1D, TH2D or TH3D 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// If you want to use a different unit for histogramming use SetScaleX,
44// SetScaleY and SetScaleZ.
45//
46//
47// Binning/Binning name
48// =====================
49//
50// The axis binning is retrieved from the parameter list, too. Create a
51// MBinning with the name "Binning" plus the name of your MH3 container
52// plus the axis name ("X", "Y" or "Z") and add it to the parameter list.
53//
54// If the binning should have a different name than the histogram name
55// the binning name can be added to the name, eg.:
56// SetName("MyHistName;MyXBins;MyYBins")
57// Instead of BinningMyHistName[XYZ] the parameter list will be searched
58// for BinningMyXBinning, BinningMyYBins and BinningMyHistNameZ
59//
60// If you don't want to use a MBinning object from the parameter list
61// you can also set one directly, for example
62// MBinning bins(10, 0, 1);
63// mh3.SetBinningX(&bins);
64// You must not delete the MBinning object before the class has been
65// PreProcessed.
66//
67//
68// Axis titles
69// ===========
70//
71// 1) If no other title is given the rule for this axis is used.
72// 2) If the MBinning used for this axis has a non-default Title
73// (MBinning::HasTitle) this title is used for the corresponding axis
74// 3) If the MH3 has a non-default title (MH3::SetTitle called)
75// this title is set as the histogram title. It can be used to overwrite
76// the axis titles. For more information see TH1::SetTitle, eg.
77// SetTitle("MyHist;x[mm];y[cm];Counts");
78//
79//
80// Labels
81// ======
82//
83// To use labels at an axis you have to initialize this for the axis
84// by either calling InitLabels(Labels_t) or setiting a DefaultLabel.
85// For the axis for which the labels have been initialized the
86// number returned by the given corresponding phrase is converted
87// to int with TMath::Nint and used as a label. If you want to replace
88// this id by a named label you can call DefineLabel to do that.
89// Several ids can be replaced by the same label. If you define
90// named labels for every label which was not defined the default
91// is used, if any, otherwise an unnamed label is created.
92//
93// In the case of an axis with labels the axis-title cannot be
94// set via a MBinning, because the MBinning is not evaluated.
95//
96// Please note that for some reason not all combinations of
97// labels, dimensions and weights are available in the root-
98// histogram classes. Please check the MH3::Fill function to see
99// whether your combination is supported.
100//
101//
102// Examples:
103// =========
104//
105// 1) MH3 myhist("MHillas.fLength");
106// myhist.SetName("MyHist");
107// myhist.SetScaleX(geomcam.GetConvMm2Deg()); //convert length to degree
108// MBinning bins("BinningMyHistX", "Title for my x-axis [Hz]");
109// bins.SetEdges(10, 0, 150);
110// plist.AddToList(&bins);
111//
112// 2) MH3 myhist("MHillas.fLength");
113// myhist.SetName("MyHist;MyX");
114// myhist.SetTitle("Histogram Title;X-Title [mm];Counts");
115// MBinning bins("BinningMyX");
116// bins.SetEdges(10, 0, 150);
117// plist.AddToList(&bins);
118//
119// 3) MH3 myhist("MTriggerPatter.GetUnprescaled");
120// myhist.SetWeight("1./MRawRunHeader.GetRunLength");
121// myhist.SetTitle("Rate of the trigger pattern [Hz];Run Number;Trigger Pattern;Rate [Hz]");
122// myhist.InitLabels(MH3::kLabelsXY);
123// myhist.DefaultLabelY("UNKNOWN"); // Lvl1
124// myhist.DefineLabelY( 1, "Trig"); // Lvl1
125// myhist.DefineLabelY( 2, "Cal"); // Cal
126// myhist.DefineLabelY( 4, "Trig"); // Lvl2
127// myhist.DefineLabelY( 8, "Ped"); // Ped
128//
129//
130// Class Version 3:
131// ----------------
132// - MData *fData[3];
133// + MData *fData[4];
134//
135// Class Version 2:
136// ----------------
137// - MDataChain *fData[3]; // Object from which the data is filled
138// + MData *fData[3]; // Object from which the data is filled
139//
140// Class Version 3:
141// ----------------
142// - Byte_t fStyleBits
143// + MBinning fBins[3]
144//
145/////////////////////////////////////////////////////////////////////////////
146#include "MH3.h"
147
148#include <ctype.h> // tolower
149#include <stdlib.h> // atoi (Ubuntu 8.10)
150#include <fstream>
151
152#include <TMath.h>
153
154#include <THashList.h>
155#include <TObjString.h>
156
157//#include <TPad.h>
158#include <TStyle.h>
159#include <TCanvas.h>
160
161#include <TH2.h>
162#include <TH3.h>
163#include <TProfile.h>
164#include <TProfile2D.h>
165
166#include "MLog.h"
167#include "MLogManip.h"
168
169#include "MString.h"
170
171#include "MParList.h"
172#include "MBinning.h"
173#include "MDataPhrase.h"
174
175ClassImp(MH3);
176
177using namespace std;
178
179const TString MH3::gsDefName = "MH3";
180const TString MH3::gsDefTitle = "Container for a n-D Mars Histogram";
181
182// --------------------------------------------------------------------------
183//
184// Set fStyleBits to 0, Reset fBins to NULL, fScale to 1, set name and title
185// to gsDefName and gsDefTitle and if fHist!=NULL UseCurrentStyle and
186// SetDirectory(0)
187//
188void MH3::Init()
189{
190 fStyleBits = 0;
191
192 fData[3] = NULL;
193
194 fBins[0] = NULL;
195 fBins[1] = NULL;
196 fBins[2] = NULL;
197
198 fScale[0] = 1;
199 fScale[1] = 1;
200 fScale[2] = 1;
201
202 fName = gsDefName;
203 fTitle = gsDefTitle;
204
205 if (!fHist)
206 return;
207
208 fHist->UseCurrentStyle();
209 fHist->SetDirectory(NULL);
210}
211
212// --------------------------------------------------------------------------
213//
214// Default constructor.
215//
216MH3::MH3(const Int_t dim, Type_t type) : fDimension(dim), fHist(NULL)
217{
218 // FIXME?
219 switch (type)
220 {
221 case kHistogram:
222 if (fDimension>3)
223 fDimension=3;
224 break;
225 case kProfile:
226 fDimension = -TMath::Abs(fDimension);
227 if (fDimension<-2)
228 fDimension = -2;
229 break;
230 }
231
232 switch (fDimension)
233 {
234 case 1:
235 fHist = new TH1D;
236 fHist->SetYTitle("Counts");
237 break;
238 case -1:
239 fHist = new TProfile;
240 fHist->SetYTitle("Average");
241 break;
242 case 2:
243 fHist = new TH2D;
244 fHist->SetZTitle("Counts");
245 break;
246 case -2:
247 fHist = new TProfile2D;
248 fHist->SetZTitle("Average");
249 break;
250 case 3:
251 fHist = new TH3D;
252 break;
253 }
254
255 fData[0] = NULL;
256 fData[1] = NULL;
257 fData[2] = NULL;
258
259 Init();
260}
261
262// --------------------------------------------------------------------------
263//
264// Creates an TH1D. memberx is filled into the X-bins. For a more detailed
265// description see the class description above.
266//
267MH3::MH3(const char *memberx, Type_t type) : fDimension(1)
268{
269 fHist = new TH1D;
270 fHist->SetYTitle("Counts");
271
272 fData[0] = new MDataPhrase(memberx);
273 fData[1] = NULL;
274 fData[2] = NULL;
275
276 Init();
277}
278
279// --------------------------------------------------------------------------
280//
281// Adapt a given histogram
282//
283MH3::MH3(const TH1 &h1) : fDimension(1)
284{
285 if (h1.InheritsFrom(TH3::Class()))
286 fDimension = 3;
287 if (h1.InheritsFrom(TH2::Class()))
288 fDimension = 2;
289
290 if (h1.InheritsFrom(TProfile2D::Class()))
291 fDimension = -2;
292 if (h1.InheritsFrom(TProfile::Class()))
293 fDimension = -1;
294
295 fHist = (TH1*)h1.Clone();
296
297 fData[0] = NULL;
298 fData[1] = NULL;
299 fData[2] = NULL;
300
301 switch (fDimension)
302 {
303 case 3:
304 case -2:
305 fData[2] = new MDataPhrase(h1.GetZaxis()->GetTitle());
306 case 2:
307 case -1:
308 fData[1] = new MDataPhrase(h1.GetYaxis()->GetTitle());
309 case 1:
310 fData[0] = new MDataPhrase(h1.GetXaxis()->GetTitle());
311 }
312
313 Init(); // Before without SeUseCurrentStyle!
314}
315
316// --------------------------------------------------------------------------
317//
318// Creates an TH2D. memberx is filled into the X-bins. membery is filled
319// into the Y-bins. For a more detailed description see the class
320// description above.
321//
322MH3::MH3(const char *memberx, const char *membery, Type_t type)
323 : fDimension(type&kProfile?-1:2)
324{
325
326 switch (fDimension)
327 {
328 case 2:
329 fHist = static_cast<TH1*>(new TH2D);
330 break;
331 case -1:
332 fHist = static_cast<TH1*>(new TProfile);
333 break;
334 }
335
336 fHist->SetZTitle(fDimension>0?"Counts":"Average");
337
338 fData[0] = new MDataPhrase(memberx);
339 fData[1] = new MDataPhrase(membery);
340 fData[2] = NULL;
341
342 Init();
343}
344
345// --------------------------------------------------------------------------
346//
347// Creates an TH3D. memberx is filled into the X-bins. membery is filled
348// into the Y-bins. membery is filled into the Z-bins. For a more detailed
349// description see the class description above.
350//
351MH3::MH3(const char *memberx, const char *membery, const char *memberz, Type_t type)
352 : fDimension(type==kHistogram?3:-2)
353{
354 fHist = type&kProfile ? static_cast<TH1*>(new TProfile2D) : static_cast<TH1*>(new TH3D);
355
356 fData[0] = new MDataPhrase(memberx);
357 fData[1] = new MDataPhrase(membery);
358 fData[2] = new MDataPhrase(memberz);
359
360 Init();
361}
362
363// --------------------------------------------------------------------------
364//
365// Deletes the histogram
366//
367MH3::~MH3()
368{
369 delete fHist;
370
371 for (int i=0; i<4; i++)
372 if (fData[i])
373 delete fData[i];
374
375 for (int i=0; i<3; i++)
376 if (fLabels[i].GetDefault())
377 delete fLabels[i].GetDefault();
378}
379
380// --------------------------------------------------------------------------
381//
382// You can set a weight as a phrase additionally to the one given
383// as an argument to Fill (most likely from MFillH). The two weights
384// are multiplied together.
385//
386void MH3::SetWeight(const char *phrase)
387{
388 if (fData[3])
389 delete fData[3];
390 fData[3] = new MDataPhrase(phrase);
391}
392
393// --------------------------------------------------------------------------
394//
395// The axis label is centered and the labeling of the axis is initialized.
396//
397// This function must not be called after any label has been created!
398//
399void MH3::InitLabels(TAxis &x) const
400{
401 x.CenterTitle();
402 x.SetBinLabel(1, "");
403 x.LabelsOption("h"); // FIXME: Is "a" thread safe? (Paint and Fill?)
404 x.GetLabels()->Delete();
405}
406
407// --------------------------------------------------------------------------
408//
409// Depending on the bits set the InitLabels(TAxis&) function for
410// the corresponding axes are called. In any case the kCanRebin bit
411// is set.
412//
413// This function must not be called after any label has been created!
414//
415void MH3::InitLabels(Labels_t type) const
416{
417 if (!fHist)
418 return;
419
420 if (type&kLabelsX && fHist->GetXaxis())
421 InitLabels(*fHist->GetXaxis());
422
423 if (type&kLabelsY && fHist->GetYaxis())
424 InitLabels(*fHist->GetYaxis());
425
426 if (type&kLabelsZ && fHist->GetZaxis())
427 InitLabels(*fHist->GetZaxis());
428
429 if (type&kLabelsXYZ)
430 fHist->SetBit(TH1::kCanRebin);
431}
432
433// --------------------------------------------------------------------------
434//
435// Return the corresponding Labels_t describing for which axis
436// axis-labels are switched on.
437//
438MH3::Labels_t MH3::GetLabels() const
439{
440 UInt_t type = kNoLabels;
441 if (fHist->GetXaxis() && fHist->GetXaxis()->GetLabels())
442 type |= kLabelsX;
443 if (fHist->GetYaxis() && fHist->GetYaxis()->GetLabels())
444 type |= kLabelsY;
445 if (fHist->GetZaxis() && fHist->GetZaxis()->GetLabels())
446 type |= kLabelsZ;
447 return (Labels_t)type;
448}
449
450// --------------------------------------------------------------------------
451//
452// Calls the LabelsDeflate from the histogram for all axes.
453// LabelsDeflate will just do nothing if the axis has no labels
454// initialized.
455//
456void MH3::DeflateLabels() const
457{
458 fHist->LabelsDeflate("X");
459 fHist->LabelsDeflate("Y");
460 fHist->LabelsDeflate("Z");
461}
462
463// --------------------------------------------------------------------------
464//
465// Returns the named label corresponding to the given value
466// and the given axis. The names are defined with the
467// DefineLabel-functions. if no name is defined the value
468// is converted to a string with %d and TMath::Nint.
469// If names are defined, but not for the given value, the default
470// label is returned instead. If no default is defined the
471// %d-converted string is returned.
472//
473const char *MH3::GetLabel(Int_t axe, Double_t val) const
474{
475 const Int_t v = TMath::Nint(val);
476
477 if (fLabels[axe].GetSize())
478 {
479 const char *l = fLabels[axe].GetObjName(v);
480 if (l)
481 return l;
482 }
483
484 return Form("%d", v);
485}
486
487// --------------------------------------------------------------------------
488//
489// Return the data members used by the data chain to be used in
490// MTask::AddBranchToList
491//
492TString MH3::GetDataMember() const
493{
494 TString str=fData[0]->GetDataMember();
495
496 for (int i=1; i<4; i++)
497 if (fData[i])
498 {
499 str += ";";
500 str += fData[i]->GetDataMember();
501 }
502
503 return str;
504}
505
506// --------------------------------------------------------------------------
507//
508// Setup the Binning for the histograms automatically if the correct
509// instances of MBinning are found in the parameter list
510// For a more detailed description see class description above.
511//
512Bool_t MH3::SetupFill(const MParList *plist)
513{
514 // reset histogram (necessary if the same eventloop is run more than once)
515 fHist->Reset();
516
517 // Tokenize name into name and binnings names
518 TObjArray *tok = fName.Tokenize(";");
519
520 const TString name = (*tok)[0] ? (*tok)[0]->GetName() : fName.Data();
521
522 TString bx = (*tok)[1] ? (*tok)[1]->GetName() : Form("%sX", name.Data());
523 TString by = (*tok)[2] ? (*tok)[2]->GetName() : Form("%sY", name.Data());
524 TString bz = (*tok)[3] ? (*tok)[3]->GetName() : Form("%sZ", name.Data());
525
526 bx.Prepend("Binning");
527 by.Prepend("Binning");
528 bz.Prepend("Binning");
529
530 delete tok;
531
532 MBinning *binsx = NULL;
533 MBinning *binsy = NULL;
534 MBinning *binsz = NULL;
535
536 const Labels_t labels = GetLabels();
537
538 switch (TMath::Abs(fDimension))
539 {
540 case 3:
541 if (fData[2])
542 fHist->SetZTitle(fData[2]->GetTitle());
543 if (!(labels&kLabelsZ))
544 {
545 binsz = fBins[2] ? fBins[2] : (MBinning*)plist->FindObject(bz, "MBinning");
546 if (!binsz)
547 {
548 *fLog << err << dbginf << "MBinning '" << bz << "' not found... aborting." << endl;
549 return kFALSE;
550 }
551 if (binsz->HasTitle())
552 fHist->SetZTitle(binsz->GetTitle());
553 if (binsz->IsLogarithmic())
554 fHist->SetBit(kIsLogz);
555 }
556 case 2:
557 if (fData[1])
558 fHist->SetYTitle(fData[1]->GetTitle());
559 if (!(labels&kLabelsY))
560 {
561 binsy = fBins[1] ? fBins[1] : (MBinning*)plist->FindObject(by, "MBinning");
562 if (!binsy)
563 {
564 *fLog << err << dbginf << "MBinning '" << by << "' not found... aborting." << endl;
565 return kFALSE;
566 }
567 if (binsy->HasTitle())
568 fHist->SetYTitle(binsy->GetTitle());
569 if (binsy->IsLogarithmic())
570 fHist->SetBit(kIsLogy);
571 }
572 case 1:
573 if (fData[0]!=NULL)
574 fHist->SetXTitle(fData[0]->GetTitle());
575 if (!(labels&kLabelsX))
576 {
577 binsx = fBins[0] ? fBins[0] : (MBinning*)plist->FindObject(bx, "MBinning");
578 if (!binsx)
579 {
580 if (fDimension==1)
581 binsx = (MBinning*)plist->FindObject("Binning"+fName, "MBinning");
582
583 if (!binsx)
584 {
585 *fLog << err << dbginf << "Neither '" << bx << "' nor 'Binning" << fName << "' found... aborting." << endl;
586 return kFALSE;
587 }
588 }
589 if (binsx->HasTitle())
590 fHist->SetXTitle(binsx->GetTitle());
591 if (binsx->IsLogarithmic())
592 fHist->SetBit(kIsLogx);
593 }
594 }
595
596 // PreProcess existing fData members
597 for (int i=0; i<4; i++)
598 if (fData[i] && !fData[i]->PreProcess(plist))
599 return kFALSE;
600
601 TString title(fDimension>0?"Histogram":"Profile");
602 title += " for ";
603 title += name;
604 title += Form(" (%dD)", TMath::Abs(fDimension));
605
606 fHist->SetName(name);
607 fHist->SetTitle(fTitle==gsDefTitle ? title : fTitle);
608 fHist->SetDirectory(0);
609
610 // This is for the case we have set lables
611 const MBinning def(1, 0, 1);
612 if (!binsx)
613 binsx = const_cast<MBinning*>(&def);
614 if (!binsy)
615 binsy = const_cast<MBinning*>(&def);
616 if (!binsz)
617 binsz = const_cast<MBinning*>(&def);
618
619 // set binning
620 switch (TMath::Abs(fDimension))
621 {
622 case 1:
623 SetBinning(fHist, binsx);
624 return kTRUE;
625 case 2:
626 SetBinning(static_cast<TH2*>(fHist), binsx, binsy);
627 return kTRUE;
628 case 3:
629 SetBinning(static_cast<TH3*>(fHist), binsx, binsy, binsz);
630 return kTRUE;
631 }
632
633 *fLog << err << "ERROR - MH3 has " << TMath::Abs(fDimension) << " dimensions!" << endl;
634 return kFALSE;
635}
636
637// --------------------------------------------------------------------------
638//
639// Set the name of the histogram ant the MH3 container
640//
641void MH3::SetName(const char *name)
642{
643 if (fHist)
644 {
645 if (gPad)
646 {
647 const TString pfx(MString::Format("%sProfX", fHist->GetName()));
648 const TString pfy(MString::Format("%sProfY", fHist->GetName()));
649
650 TProfile *p = 0;
651 if ((p=dynamic_cast<TProfile*>(gPad->FindObject(pfx))))
652 p->SetName(MString::Format("%sProfX", name));
653 if ((p=dynamic_cast<TProfile*>(gPad->FindObject(pfy))))
654 p->SetName(MString::Format("%sProfY", name));
655 }
656
657 fHist->SetName(name);
658 fHist->SetDirectory(0);
659
660 }
661 MParContainer::SetName(name);
662}
663
664// --------------------------------------------------------------------------
665//
666// Set the title of the histogram ant the MH3 container
667//
668void MH3::SetTitle(const char *title)
669{
670 if (fHist)
671 fHist->SetTitle(title);
672 MParContainer::SetTitle(title);
673}
674
675// --------------------------------------------------------------------------
676//
677// Fills the one, two or three data members into our histogram
678//
679Int_t MH3::Fill(const MParContainer *par, const Stat_t ww)
680{
681 // Get Information about labels (UInt_t, to supress warning about
682 // unhandeled cases in switch)
683 const UInt_t type = GetLabels();
684
685 // Get values for axis
686 Double_t x=0;
687 Double_t y=0;
688 Double_t z=0;
689 Double_t w=ww;
690
691 switch (fDimension)
692 {
693 case -2:
694 case 3:
695 z = fData[2]->GetValue()*fScale[2];
696 case -1:
697 case 2:
698 y = fData[1]->GetValue()*fScale[1];
699 case 1:
700 x = fData[0]->GetValue()*fScale[0];
701 }
702
703 if (fData[3])
704 w *= fData[3]->GetValue();
705
706 // If label option is set, convert value to label
707 TString labelx, labely, labelz;
708 if (type&kLabelsX)
709 labelx = GetLabel(0, x);
710 if (type&kLabelsY)
711 labely = GetLabel(1, y);
712 if (type&kLabelsZ)
713 labelz = GetLabel(2, z);
714
715 // Fill histogram
716 switch (fDimension)
717 {
718 case 3:
719 switch (type)
720 {
721 case kNoLabels:
722 static_cast<TH3*>(fHist)->Fill(x, y, z, w);
723 return kTRUE;
724 case kLabelsX:
725 static_cast<TH3*>(fHist)->Fill(labelx, y, z);
726 return kTRUE;
727 case kLabelsY:
728 static_cast<TH3*>(fHist)->Fill(x, labely, z, w);
729 return kTRUE;
730 case kLabelsZ:
731 static_cast<TH3*>(fHist)->Fill(x, y, labelz, w);
732 return kTRUE;
733 case kLabelsXY:
734 static_cast<TH3*>(fHist)->Fill(labelx, labely, z, w);
735 return kTRUE;
736 case kLabelsXZ:
737 static_cast<TH3*>(fHist)->Fill(labelx, y, labelz, w);
738 return kTRUE;
739 case kLabelsYZ:
740 static_cast<TH3*>(fHist)->Fill(x, labely, labelz, w);
741 return kTRUE;
742 case kLabelsXYZ:
743 static_cast<TH3*>(fHist)->Fill(labelx, labely, labelz, w);
744 return kTRUE;
745 }
746 break;
747 case 2:
748 switch (type)
749 {
750 case kNoLabels:
751 static_cast<TH2*>(fHist)->Fill(x, y, w);
752 return kTRUE;
753 case kLabelsX:
754 static_cast<TH2*>(fHist)->Fill(x, labely, w);
755 return kTRUE;
756 case kLabelsY:
757 static_cast<TH2*>(fHist)->Fill(labelx, y, w);
758 return kTRUE;
759 case kLabelsXY:
760 static_cast<TH2*>(fHist)->Fill(labelx, labely, w);
761 return kTRUE;
762 }
763 break;
764 case 1:
765 switch (type)
766 {
767 case kNoLabels:
768 fHist->Fill(x, w);
769 return kTRUE;
770 case kLabelsX:
771 fHist->Fill(labelx, w);
772 return kTRUE;
773 }
774 break;
775 case -1:
776 switch (type)
777 {
778 case kNoLabels:
779 static_cast<TProfile*>(fHist)->Fill(x, y, w);
780 return kTRUE;
781 case kLabelsX:
782 static_cast<TProfile*>(fHist)->Fill(labelx, y, w);
783 return kTRUE;
784 }
785 break;
786 case -2:
787 switch (type)
788 {
789 case kNoLabels:
790 static_cast<TProfile2D*>(fHist)->Fill(x, y, z, w);
791 return kTRUE;
792 case kLabelsX:
793 static_cast<TProfile2D*>(fHist)->Fill(labelx, y, z);
794 return kTRUE;
795 case kLabelsY:
796 static_cast<TProfile2D*>(fHist)->Fill(x, labely, z);
797 return kTRUE;
798 case kLabelsXY:
799 static_cast<TProfile2D*>(fHist)->Fill(labelx, labely, z);
800 return kTRUE;
801 }
802 break;
803 }
804
805 *fLog << err << "MH3::Fill: ERROR - A fatal error occured." << endl;
806 return kERROR;
807}
808
809// --------------------------------------------------------------------------
810//
811// If an auto range bit is set the histogram range of the corresponding
812// axis is set to show only the non-empty bins (with a single empty bin
813// on both sides)
814//
815Bool_t MH3::Finalize()
816{
817 DeflateLabels();
818
819 Bool_t autorangex=TESTBIT(fStyleBits, 0);
820 Bool_t autorangey=TESTBIT(fStyleBits, 1);
821 //Bool_t autorangez=TESTBIT(fStyleBits, 2);
822
823 Int_t lo, hi;
824 if (autorangex)
825 {
826 GetRangeX(*fHist, lo, hi);
827 fHist->GetXaxis()->SetRange(lo-2, hi+1);
828 }
829 if (autorangey)
830 {
831 GetRangeY(*fHist, lo, hi);
832 fHist->GetYaxis()->SetRange(lo-2, hi+1);
833 }
834 /*
835 if (autorangez)
836 {
837 GetRangeZ(*fHist, lo, hi);
838 fHist->GetZaxis()->SetRange(lo-2, hi+1);
839 }
840 */
841 return kTRUE;
842}
843
844// --------------------------------------------------------------------------
845//
846// FIXME
847//
848void MH3::Paint(Option_t *o)
849{
850 TProfile *p=0;
851
852 if (TMath::Abs(fDimension)==2)
853 MH::SetPalette("pretty");
854
855 const TString pfx(MString::Format("%sProfX", fHist->GetName()));
856 if ((p=dynamic_cast<TProfile*>(gPad->FindObject(pfx))))
857 {
858 Int_t col = p->GetLineColor();
859 p = ((TH2*)fHist)->ProfileX(pfx, -1, -1, "s");
860 p->SetLineColor(col);
861 }
862
863 const TString pfy(MString::Format("%sProfY", fHist->GetName()));
864 if ((p=dynamic_cast<TProfile*>(gPad->FindObject(pfy))))
865 {
866 Int_t col = p->GetLineColor();
867 p = ((TH2*)fHist)->ProfileY(pfy, -1, -1, "s");
868 p->SetLineColor(col);
869 }
870/*
871 if (fHist->TestBit(kIsLogx) && fHist->GetEntries()>0) gPad->SetLogx();
872 if (fHist->TestBit(kIsLogy) && fHist->GetEntries()>0) gPad->SetLogy();
873 if (fHist->TestBit(kIsLogz) && fHist->GetEntries()>0) gPad->SetLogz();
874 */
875}
876
877// --------------------------------------------------------------------------
878//
879// If Xmax is < 3000*Xmin SetMoreLogLabels is called. If Xmax<5000
880// the exponent is switched off (SetNoExponent)
881//
882void MH3::HandleLogAxis(TAxis &axe) const
883{
884 if (axe.GetXmax()>3000*axe.GetXmin())
885 return;
886
887 axe.SetMoreLogLabels();
888 if (axe.GetXmax()<5000)
889 axe.SetNoExponent();
890}
891
892// --------------------------------------------------------------------------
893//
894// Creates a new canvas and draws the histogram into it.
895//
896// Possible options are:
897// PROFX: Draw a x-profile into the histogram (for 2D histograms only)
898// PROFY: Draw a y-profile into the histogram (for 2D histograms only)
899// ONLY: Draw the profile histogram only (for 2D histograms only)
900// BLUE: Draw the profile in blue color instead of the histograms
901// line color
902//
903// If the kIsLog?-Bit is set the axis is displayed lkogarithmically.
904// eg this is set when applying a logarithmic MBinning
905//
906// Be careful: The histogram belongs to this object and won't get deleted
907// together with the canvas.
908//
909void MH3::Draw(Option_t *opt)
910{
911 TVirtualPad *pad = gPad ? gPad : MakeDefCanvas(fHist);
912 pad->SetBorderMode(0);
913 pad->SetGridx();
914 pad->SetGridy();
915
916 if (fHist->TestBit(kIsLogx))
917 {
918 pad->SetLogx();
919 HandleLogAxis(*fHist->GetXaxis());
920 }
921 if (fHist->TestBit(kIsLogy))
922 {
923 pad->SetLogy();
924 HandleLogAxis(*fHist->GetYaxis());
925 }
926 if (fHist->TestBit(kIsLogz))
927 {
928 pad->SetLogz();
929 HandleLogAxis(*fHist->GetZaxis());
930 }
931
932 fHist->SetFillStyle(4000);
933
934 TString str(opt);
935 str.ToLower();
936
937 if (GetLabels())
938 {
939 if (str.IsNull() && fDimension==2)
940 str = "colz";
941
942 if (str.Contains("box", TString::kIgnoreCase) && fDimension==2)
943 fHist->SetLineColor(kBlue);
944 }
945
946 const Bool_t only = str.Contains("only") && TMath::Abs(fDimension)==2;
947 const Bool_t same = str.Contains("same") && TMath::Abs(fDimension)<3;
948 const Bool_t blue = str.Contains("blue") && TMath::Abs(fDimension)==2;
949 const Bool_t profx = str.Contains("profx") && TMath::Abs(fDimension)==2;
950 const Bool_t profy = str.Contains("profy") && TMath::Abs(fDimension)==2;
951
952 str.ReplaceAll("only", "");
953 str.ReplaceAll("blue", "");
954 str.ReplaceAll("profx", "");
955 str.ReplaceAll("profy", "");
956 str.ReplaceAll(" ", "");
957
958 if (same && TMath::Abs(fDimension)==1)
959 {
960 fHist->SetLineColor(kBlue);
961 fHist->SetMarkerColor(kBlue);
962 }
963
964 // FIXME: We may have to remove all our own options from str!
965 if (!only)
966 fHist->Draw(str);
967
968 AppendPad();
969
970 TProfile *p=0;
971 if (profx)
972 {
973 const TString pfx(MString::Format("%sProfX", fHist->GetName()));
974
975 if (same && (p=dynamic_cast<TProfile*>(gPad->FindObject(pfx))))
976 *fLog << warn << "TProfile " << pfx << " already in pad." << endl;
977
978 p = ((TH2*)fHist)->ProfileX(pfx, -1, -1, "s");
979 p->UseCurrentStyle();
980 p->SetLineColor(blue ? kBlue : fHist->GetLineColor());
981 p->SetBit(kCanDelete);
982 p->SetDirectory(NULL);
983 p->SetXTitle(fHist->GetXaxis()->GetTitle());
984 p->SetYTitle(fHist->GetYaxis()->GetTitle());
985 p->Draw(only&&!same?"":"same");
986 }
987 if (profy)
988 {
989 const TString pfy(MString::Format("%sProfY", fHist->GetName()));
990
991 if (same && (p=dynamic_cast<TProfile*>(gPad->FindObject(pfy))))
992 *fLog << warn << "TProfile " << pfy << " already in pad." << endl;
993
994 p = ((TH2*)fHist)->ProfileY(pfy, -1, -1, "s");
995 p->UseCurrentStyle();
996 p->SetLineColor(blue ? kBlue : fHist->GetLineColor());
997 p->SetBit(kCanDelete);
998 p->SetDirectory(NULL);
999 p->SetYTitle(fHist->GetXaxis()->GetTitle());
1000 p->SetXTitle(fHist->GetYaxis()->GetTitle());
1001 p->Draw(only&&!same?"":"same");
1002 }
1003
1004 //AppendPad("log");
1005}
1006
1007// --------------------------------------------------------------------------
1008//
1009// Implementation of SavePrimitive. Used to write the call to a constructor
1010// to a macro. In the original root implementation it is used to write
1011// gui elements to a macro-file.
1012//
1013void MH3::StreamPrimitive(ostream &out) const
1014{
1015 TString name = GetUniqueName();
1016
1017 out << " MH3 " << name << "(\"";
1018 out << fData[0]->GetRule() << "\"";
1019 if (fDimension>1 || fDimension<0)
1020 out << ", \"" << fData[1]->GetRule() << "\"";
1021 if (fDimension>2 || fDimension<-1)
1022 out << ", \"" << fData[2]->GetRule() << "\"";
1023
1024 out << ");" << endl;
1025
1026 if (fName!=gsDefName)
1027 out << " " << name << ".SetName(\"" << fName << "\");" << endl;
1028
1029 if (fTitle!=gsDefTitle)
1030 out << " " << name << ".SetTitle(\"" << fTitle << "\");" << endl;
1031
1032 if (fData[3])
1033 out << " " << name << ".SetWeight(\"" << fData[3]->GetRule() << "\");" << endl;
1034
1035 switch (fDimension)
1036 {
1037 case -2:
1038 case 3:
1039 if (fScale[2]!=1)
1040 out << " " << name << ".SetScaleZ(" << fScale[2] << ");" << endl;
1041 case -1:
1042 case 2:
1043 if (fScale[1]!=1)
1044 out << " " << name << ".SetScaleY(" << fScale[1] << ");" << endl;
1045 case 1:
1046 if (fScale[0]!=1)
1047 out << " " << name << ".SetScaleX(" << fScale[0] << ");" << endl;
1048 }
1049}
1050
1051// --------------------------------------------------------------------------
1052//
1053// Used to rebuild a MH3 object of the same type (data members,
1054// dimension, ...)
1055//
1056MParContainer *MH3::New() const
1057{
1058 // FIXME: TREAT THE NEW OPTIONS CORRECTLY (PROFILE, LABELS)
1059
1060 MH3 *h = NULL;
1061
1062 if (fData[0] == NULL)
1063 h=new MH3(fDimension);
1064 else
1065 switch (fDimension)
1066 {
1067 case 1:
1068 h=new MH3(fData[0]->GetRule());
1069 break;
1070 case 2:
1071 h=new MH3(fData[0]->GetRule(), fData[1]->GetRule());
1072 break;
1073 case -1:
1074 h=new MH3(fData[0]->GetRule(), fData[1]->GetRule(), MH3::kProfile);
1075 break;
1076 case 3:
1077 h=new MH3(fData[0]->GetRule(), fData[1]->GetRule(), fData[2]->GetRule());
1078 break;
1079 case -2:
1080 h=new MH3(fData[0]->GetRule(), fData[1]->GetRule(), fData[2]->GetRule(), MH3::kProfile);
1081 break;
1082 }
1083
1084 switch (fDimension)
1085 {
1086 case -2:
1087 case 3:
1088 h->SetScaleZ(fScale[2]);
1089 case 2:
1090 case -1:
1091 h->SetScaleY(fScale[1]);
1092 case 1:
1093 h->SetScaleX(fScale[0]);
1094 }
1095
1096 if (fData[3])
1097 h->SetWeight(fData[3]->GetRule());
1098
1099 return h;
1100}
1101
1102// --------------------------------------------------------------------------
1103//
1104// FIXME
1105//
1106TString MH3::GetRule(const Char_t axis) const
1107{
1108 switch (tolower(axis))
1109 {
1110 case 'x':
1111 case 'X':
1112 return fData[0] ? fData[0]->GetRule() : TString("");
1113 case 'y':
1114 case 'Y':
1115 return fData[1] ? fData[1]->GetRule() : TString("");
1116 case 'z':
1117 case 'Z':
1118 return fData[2] ? fData[2]->GetRule() : TString("");
1119 case 'w':
1120 case 'W':
1121 return fData[3] ? fData[3]->GetRule() : TString("");
1122 default:
1123 return "<n/a>";
1124 }
1125}
1126
1127// --------------------------------------------------------------------------
1128//
1129// Returns the total number of bins in a histogram (excluding under- and
1130// overflow bins)
1131//
1132Int_t MH3::GetNbins() const
1133{
1134 Int_t num = 1;
1135
1136 switch (TMath::Abs(fDimension))
1137 {
1138 case 3:
1139 num *= fHist->GetNbinsZ()+2;
1140 case 2:
1141 num *= fHist->GetNbinsY()+2;
1142 case 1:
1143 num *= fHist->GetNbinsX()+2;
1144 }
1145
1146 return num;
1147}
1148
1149// --------------------------------------------------------------------------
1150//
1151// Returns the total number of bins in a histogram (excluding under- and
1152// overflow bins) Return -1 if bin is underflow or overflow
1153//
1154Int_t MH3::FindFixBin(Double_t x, Double_t y, Double_t z) const
1155{
1156 const TAxis &axex = *fHist->GetXaxis();
1157 const TAxis &axey = *fHist->GetYaxis();
1158 const TAxis &axez = *fHist->GetZaxis();
1159
1160 Int_t binz = 0;
1161 Int_t biny = 0;
1162 Int_t binx = 0;
1163
1164 switch (fDimension)
1165 {
1166 case 3:
1167 case -2:
1168 binz = axez.FindFixBin(z);
1169 if (binz>axez.GetLast() || binz<axez.GetFirst())
1170 return -1;
1171 case 2:
1172 case -1:
1173 biny = axey.FindFixBin(y);
1174 if (biny>axey.GetLast() || biny<axey.GetFirst())
1175 return -1;
1176 case 1:
1177 binx = axex.FindFixBin(x);
1178 if (binx<axex.GetFirst() || binx>axex.GetLast())
1179 return -1;
1180 }
1181
1182 const Int_t nx = fHist->GetNbinsX()+2;
1183 const Int_t ny = fHist->GetNbinsY()+2;
1184
1185 return binx + nx*(biny +ny*binz);
1186}
1187
1188// --------------------------------------------------------------------------
1189//
1190// Return the MObjLookup corresponding to the axis/character.
1191// Note that only lower-case charecters (x, y, z) are supported.
1192// If for the axis no labels were set, the corresponding
1193// InitLabels is called.
1194//
1195MObjLookup *MH3::GetLabels(char axe)
1196{
1197 if (!fHist)
1198 return 0;
1199
1200 TAxis *x = 0;
1201
1202 switch (axe)
1203 {
1204 case 'x':
1205 x = fHist->GetXaxis();
1206 break;
1207 case 'y':
1208 x = fHist->GetYaxis();
1209 break;
1210 case 'z':
1211 x = fHist->GetZaxis();
1212 break;
1213 }
1214
1215 if (!x)
1216 return 0;
1217
1218 const Int_t idx = axe-'x';
1219
1220 if (!x->GetLabels())
1221 switch (idx)
1222 {
1223 case 0:
1224 InitLabels(kLabelsX);
1225 break;
1226 case 1:
1227 InitLabels(kLabelsY);
1228 break;
1229 case 2:
1230 InitLabels(kLabelsZ);
1231 break;
1232 }
1233
1234 return &fLabels[idx];
1235}
1236
1237// --------------------------------------------------------------------------
1238//
1239// Set a default label which is used if no other is found in the list
1240// of labels. if a default was set already it is overwritten. If the
1241// axis has not yet been initialized to use labels it it now.
1242//
1243void MH3::DefaultLabel(char axe, const char *name)
1244{
1245 MObjLookup *arr = GetLabels(axe);
1246 if (!arr)
1247 return;
1248
1249 if (arr->GetDefault())
1250 {
1251 delete arr->GetDefault();
1252 arr->SetDefault(0);
1253 }
1254
1255 if (name)
1256 arr->SetDefault(new TObjString(name));
1257}
1258
1259// --------------------------------------------------------------------------
1260//
1261// Define a name for a label. More than one label can have the same
1262// name. If the axis has not yet been initialized to use labels
1263// it it now.
1264//
1265void MH3::DefineLabel(char axe, Int_t label, const char *name)
1266{
1267 MObjLookup *arr = GetLabels(axe);
1268
1269 if (!arr || !name)
1270 return;
1271
1272 if (arr->GetObj(label)!=arr->GetDefault())
1273 return;
1274
1275 arr->Add(label, name);
1276}
1277
1278// --------------------------------------------------------------------------
1279//
1280// Define names for labels, like
1281// 1=Trig;2=Cal;4=Ped;8=Lvl2
1282// More than one label can have the same name. If the axis has not
1283// yet been initialized to use labels it it now.
1284//
1285// A default cannot be set here. Use DefaultLabel instead.
1286//
1287void MH3::DefineLabels(char axe, const TString &labels)
1288{
1289 TObjArray *arr = labels.Tokenize(';');
1290
1291 for (int i=0; i<arr->GetEntries(); i++)
1292 {
1293 const char *s = (*arr)[0]->GetName();
1294 const char *v = strchr(s, '=');
1295
1296 if (v)
1297 DefineLabel(axe, atoi(s), v+1);
1298 }
1299
1300 delete arr;
1301}
1302
1303void MH3::RecursiveRemove(TObject *obj)
1304{
1305 if (obj==fHist)
1306 fHist = 0;
1307}
Note: See TracBrowser for help on using the repository browser.