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

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