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

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