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

Last change on this file since 9194 was 9153, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 35.5 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 (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 '" << 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//
676Int_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 (GetLabels())
935 {
936 if (str.IsNull() && fDimension==2)
937 str = "colz";
938
939 if (str.Contains("box", TString::kIgnoreCase) && fDimension==2)
940 fHist->SetLineColor(kBlue);
941 }
942
943 const Bool_t only = str.Contains("only") && TMath::Abs(fDimension)==2;
944 const Bool_t same = str.Contains("same") && TMath::Abs(fDimension)<3;
945 const Bool_t blue = str.Contains("blue") && TMath::Abs(fDimension)==2;
946 const Bool_t profx = str.Contains("profx") && TMath::Abs(fDimension)==2;
947 const Bool_t profy = str.Contains("profy") && TMath::Abs(fDimension)==2;
948
949 str.ReplaceAll("only", "");
950 str.ReplaceAll("blue", "");
951 str.ReplaceAll("profx", "");
952 str.ReplaceAll("profy", "");
953 str.ReplaceAll(" ", "");
954
955 if (same && TMath::Abs(fDimension)==1)
956 {
957 fHist->SetLineColor(kBlue);
958 fHist->SetMarkerColor(kBlue);
959 }
960
961 // FIXME: We may have to remove all our own options from str!
962 if (!only)
963 fHist->Draw(str);
964
965 AppendPad();
966
967 TProfile *p=0;
968 if (profx)
969 {
970 const TString pfx(Form("%sProfX", fHist->GetName()));
971
972 if (same && (p=dynamic_cast<TProfile*>(gPad->FindObject(pfx))))
973 *fLog << warn << "TProfile " << pfx << " already in pad." << endl;
974
975 p = ((TH2*)fHist)->ProfileX(pfx, -1, -1, "s");
976 p->UseCurrentStyle();
977 p->SetLineColor(blue ? kBlue : fHist->GetLineColor());
978 p->SetBit(kCanDelete);
979 p->SetDirectory(NULL);
980 p->SetXTitle(fHist->GetXaxis()->GetTitle());
981 p->SetYTitle(fHist->GetYaxis()->GetTitle());
982 p->Draw(only&&!same?"":"same");
983 }
984 if (profy)
985 {
986 const TString pfy(Form("%sProfY", fHist->GetName()));
987
988 if (same && (p=dynamic_cast<TProfile*>(gPad->FindObject(pfy))))
989 *fLog << warn << "TProfile " << pfy << " already in pad." << endl;
990
991 p = ((TH2*)fHist)->ProfileY(pfy, -1, -1, "s");
992 p->UseCurrentStyle();
993 p->SetLineColor(blue ? kBlue : fHist->GetLineColor());
994 p->SetBit(kCanDelete);
995 p->SetDirectory(NULL);
996 p->SetYTitle(fHist->GetXaxis()->GetTitle());
997 p->SetXTitle(fHist->GetYaxis()->GetTitle());
998 p->Draw(only&&!same?"":"same");
999 }
1000
1001 //AppendPad("log");
1002}
1003
1004// --------------------------------------------------------------------------
1005//
1006// Implementation of SavePrimitive. Used to write the call to a constructor
1007// to a macro. In the original root implementation it is used to write
1008// gui elements to a macro-file.
1009//
1010void MH3::StreamPrimitive(ostream &out) const
1011{
1012 TString name = GetUniqueName();
1013
1014 out << " MH3 " << name << "(\"";
1015 out << fData[0]->GetRule() << "\"";
1016 if (fDimension>1 || fDimension<0)
1017 out << ", \"" << fData[1]->GetRule() << "\"";
1018 if (fDimension>2 || fDimension<-1)
1019 out << ", \"" << fData[2]->GetRule() << "\"";
1020
1021 out << ");" << endl;
1022
1023 if (fName!=gsDefName)
1024 out << " " << name << ".SetName(\"" << fName << "\");" << endl;
1025
1026 if (fTitle!=gsDefTitle)
1027 out << " " << name << ".SetTitle(\"" << fTitle << "\");" << endl;
1028
1029 if (fData[3])
1030 out << " " << name << ".SetWeight(\"" << fData[3]->GetRule() << "\");" << endl;
1031
1032 switch (fDimension)
1033 {
1034 case -2:
1035 case 3:
1036 if (fScale[2]!=1)
1037 out << " " << name << ".SetScaleZ(" << fScale[2] << ");" << endl;
1038 case -1:
1039 case 2:
1040 if (fScale[1]!=1)
1041 out << " " << name << ".SetScaleY(" << fScale[1] << ");" << endl;
1042 case 1:
1043 if (fScale[0]!=1)
1044 out << " " << name << ".SetScaleX(" << fScale[0] << ");" << endl;
1045 }
1046}
1047
1048// --------------------------------------------------------------------------
1049//
1050// Used to rebuild a MH3 object of the same type (data members,
1051// dimension, ...)
1052//
1053MParContainer *MH3::New() const
1054{
1055 // FIXME: TREAT THE NEW OPTIONS CORRECTLY (PROFILE, LABELS)
1056
1057 MH3 *h = NULL;
1058
1059 if (fData[0] == NULL)
1060 h=new MH3(fDimension);
1061 else
1062 switch (fDimension)
1063 {
1064 case 1:
1065 h=new MH3(fData[0]->GetRule());
1066 break;
1067 case 2:
1068 h=new MH3(fData[0]->GetRule(), fData[1]->GetRule());
1069 break;
1070 case -1:
1071 h=new MH3(fData[0]->GetRule(), fData[1]->GetRule(), MH3::kProfile);
1072 break;
1073 case 3:
1074 h=new MH3(fData[0]->GetRule(), fData[1]->GetRule(), fData[2]->GetRule());
1075 break;
1076 case -2:
1077 h=new MH3(fData[0]->GetRule(), fData[1]->GetRule(), fData[2]->GetRule(), MH3::kProfile);
1078 break;
1079 }
1080
1081 switch (fDimension)
1082 {
1083 case -2:
1084 case 3:
1085 h->SetScaleZ(fScale[2]);
1086 case 2:
1087 case -1:
1088 h->SetScaleY(fScale[1]);
1089 case 1:
1090 h->SetScaleX(fScale[0]);
1091 }
1092
1093 if (fData[3])
1094 h->SetWeight(fData[3]->GetRule());
1095
1096 return h;
1097}
1098
1099// --------------------------------------------------------------------------
1100//
1101// FIXME
1102//
1103TString MH3::GetRule(const Char_t axis) const
1104{
1105 switch (tolower(axis))
1106 {
1107 case 'x':
1108 case 'X':
1109 return fData[0] ? fData[0]->GetRule() : TString("");
1110 case 'y':
1111 case 'Y':
1112 return fData[1] ? fData[1]->GetRule() : TString("");
1113 case 'z':
1114 case 'Z':
1115 return fData[2] ? fData[2]->GetRule() : TString("");
1116 case 'w':
1117 case 'W':
1118 return fData[3] ? fData[3]->GetRule() : TString("");
1119 default:
1120 return "<n/a>";
1121 }
1122}
1123
1124// --------------------------------------------------------------------------
1125//
1126// Returns the total number of bins in a histogram (excluding under- and
1127// overflow bins)
1128//
1129Int_t MH3::GetNbins() const
1130{
1131 Int_t num = 1;
1132
1133 switch (TMath::Abs(fDimension))
1134 {
1135 case 3:
1136 num *= fHist->GetNbinsZ()+2;
1137 case 2:
1138 num *= fHist->GetNbinsY()+2;
1139 case 1:
1140 num *= fHist->GetNbinsX()+2;
1141 }
1142
1143 return num;
1144}
1145
1146// --------------------------------------------------------------------------
1147//
1148// Returns the total number of bins in a histogram (excluding under- and
1149// overflow bins) Return -1 if bin is underflow or overflow
1150//
1151Int_t MH3::FindFixBin(Double_t x, Double_t y, Double_t z) const
1152{
1153 const TAxis &axex = *fHist->GetXaxis();
1154 const TAxis &axey = *fHist->GetYaxis();
1155 const TAxis &axez = *fHist->GetZaxis();
1156
1157 Int_t binz = 0;
1158 Int_t biny = 0;
1159 Int_t binx = 0;
1160
1161 switch (fDimension)
1162 {
1163 case 3:
1164 case -2:
1165 binz = axez.FindFixBin(z);
1166 if (binz>axez.GetLast() || binz<axez.GetFirst())
1167 return -1;
1168 case 2:
1169 case -1:
1170 biny = axey.FindFixBin(y);
1171 if (biny>axey.GetLast() || biny<axey.GetFirst())
1172 return -1;
1173 case 1:
1174 binx = axex.FindFixBin(x);
1175 if (binx<axex.GetFirst() || binx>axex.GetLast())
1176 return -1;
1177 }
1178
1179 const Int_t nx = fHist->GetNbinsX()+2;
1180 const Int_t ny = fHist->GetNbinsY()+2;
1181
1182 return binx + nx*(biny +ny*binz);
1183}
1184
1185// --------------------------------------------------------------------------
1186//
1187// Return the MObjLookup corresponding to the axis/character.
1188// Note that only lower-case charecters (x, y, z) are supported.
1189// If for the axis no labels were set, the corresponding
1190// InitLabels is called.
1191//
1192MObjLookup *MH3::GetLabels(char axe)
1193{
1194 if (!fHist)
1195 return 0;
1196
1197 TAxis *x = 0;
1198
1199 switch (axe)
1200 {
1201 case 'x':
1202 x = fHist->GetXaxis();
1203 break;
1204 case 'y':
1205 x = fHist->GetYaxis();
1206 break;
1207 case 'z':
1208 x = fHist->GetZaxis();
1209 break;
1210 }
1211
1212 if (!x)
1213 return 0;
1214
1215 const Int_t idx = axe-'x';
1216
1217 if (!x->GetLabels())
1218 switch (idx)
1219 {
1220 case 0:
1221 InitLabels(kLabelsX);
1222 break;
1223 case 1:
1224 InitLabels(kLabelsY);
1225 break;
1226 case 2:
1227 InitLabels(kLabelsZ);
1228 break;
1229 }
1230
1231 return &fLabels[idx];
1232}
1233
1234// --------------------------------------------------------------------------
1235//
1236// Set a default label which is used if no other is found in the list
1237// of labels. if a default was set already it is overwritten. If the
1238// axis has not yet been initialized to use labels it it now.
1239//
1240void MH3::DefaultLabel(char axe, const char *name)
1241{
1242 MObjLookup *arr = GetLabels(axe);
1243 if (!arr)
1244 return;
1245
1246 if (arr->GetDefault())
1247 {
1248 delete arr->GetDefault();
1249 arr->SetDefault(0);
1250 }
1251
1252 if (name)
1253 arr->SetDefault(new TObjString(name));
1254}
1255
1256// --------------------------------------------------------------------------
1257//
1258// Define a name for a label. More than one label can have the same
1259// name. If the axis has not yet been initialized to use labels
1260// it it now.
1261//
1262void MH3::DefineLabel(char axe, Int_t label, const char *name)
1263{
1264 MObjLookup *arr = GetLabels(axe);
1265
1266 if (!arr || !name)
1267 return;
1268
1269 if (arr->GetObj(label)!=arr->GetDefault())
1270 return;
1271
1272 arr->Add(label, name);
1273}
1274
1275// --------------------------------------------------------------------------
1276//
1277// Define names for labels, like
1278// 1=Trig;2=Cal;4=Ped;8=Lvl2
1279// More than one label can have the same name. If the axis has not
1280// yet been initialized to use labels it it now.
1281//
1282// A default cannot be set here. Use DefaultLabel instead.
1283//
1284void MH3::DefineLabels(char axe, const TString &labels)
1285{
1286 TObjArray *arr = labels.Tokenize(';');
1287
1288 for (int i=0; i<arr->GetEntries(); i++)
1289 {
1290 const char *s = (*arr)[0]->GetName();
1291 const char *v = strchr(s, '=');
1292
1293 if (v)
1294 DefineLabel(axe, atoi(s), v+1);
1295 }
1296
1297 delete arr;
1298}
Note: See TracBrowser for help on using the repository browser.