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

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