1 /*************************************************************************
2 * Copyright(c) 1998-2009, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
17 // Generic Histogram container with support for groups and filling of groups by passing
21 // Jens Wiechula <Jens.Wiechula@cern.ch>
22 // Julian Book <Julian.Book@cern.ch>
30 #include <TProfile2D.h>
31 #include <TProfile3D.h>
32 #include <TCollection.h>
33 #include <THashList.h>
35 #include <TObjString.h>
36 #include <TObjArray.h>
45 #include <TVirtualPS.h>
48 #include "AliDielectronHelper.h"
49 #include "AliDielectronHistos.h"
52 ClassImp(AliDielectronHistos)
55 AliDielectronHistos::AliDielectronHistos() :
57 TNamed("AliDielectronHistos","Dielectron Histogram Container"),
60 fReservedWords(new TString)
63 // Default constructor
65 fHistoList.SetOwner(kTRUE);
66 fHistoList.SetName("Dielectron_Histos");
69 //_____________________________________________________________________________
70 AliDielectronHistos::AliDielectronHistos(const char* name, const char* title) :
75 fReservedWords(new TString)
80 fHistoList.SetOwner(kTRUE);
81 fHistoList.SetName(name);
84 //_____________________________________________________________________________
85 AliDielectronHistos::~AliDielectronHistos()
91 if (fList) fList->Clear();
92 delete fReservedWords;
95 //_____________________________________________________________________________
96 void AliDielectronHistos::UserProfile(const char* histClass,const char *name, const char* title,
98 Int_t nbinsX, Double_t xmin, Double_t xmax,
99 UInt_t valTypeX, Bool_t logBinX)
102 // Default histogram creation 1D case
105 TVectorD *binLimX=0x0;
108 binLimX=AliDielectronHelper::MakeLogBinning(nbinsX, xmin, xmax);
110 binLimX=AliDielectronHelper::MakeLinBinning(nbinsX, xmin, xmax);
112 UserProfile(histClass,name,title,valTypeP,binLimX,valTypeX);
115 //_____________________________________________________________________________
116 void AliDielectronHistos::UserProfile(const char* histClass,const char *name, const char* title,
118 Int_t nbinsX, Double_t xmin, Double_t xmax,
119 Int_t nbinsY, Double_t ymin, Double_t ymax,
120 UInt_t valTypeX, UInt_t valTypeY,
121 Bool_t logBinX, Bool_t logBinY)
124 // Default histogram creation 2D case
126 if (!IsHistogramOk(histClass,name)) return;
128 TVectorD *binLimX=0x0;
129 TVectorD *binLimY=0x0;
132 binLimX=AliDielectronHelper::MakeLogBinning(nbinsX, xmin, xmax);
134 binLimX=AliDielectronHelper::MakeLinBinning(nbinsX, xmin, xmax);
137 binLimY=AliDielectronHelper::MakeLogBinning(nbinsY, ymin, ymax);
139 binLimY=AliDielectronHelper::MakeLinBinning(nbinsY, ymin, ymax);
141 UserProfile(histClass,name,title,valTypeP,binLimX,binLimY,valTypeX,valTypeY);
145 //_____________________________________________________________________________
146 void AliDielectronHistos::UserProfile(const char* histClass,const char *name, const char* title,
148 Int_t nbinsX, Double_t xmin, Double_t xmax,
149 Int_t nbinsY, Double_t ymin, Double_t ymax,
150 Int_t nbinsZ, Double_t zmin, Double_t zmax,
151 UInt_t valTypeX, UInt_t valTypeY, UInt_t valTypeZ,
152 Bool_t logBinX, Bool_t logBinY, Bool_t logBinZ)
155 // Default histogram creation 3D case
157 if (!IsHistogramOk(histClass,name)) return;
159 TVectorD *binLimX=0x0;
160 TVectorD *binLimY=0x0;
161 TVectorD *binLimZ=0x0;
164 binLimX=AliDielectronHelper::MakeLogBinning(nbinsX, xmin, xmax);
166 binLimX=AliDielectronHelper::MakeLinBinning(nbinsX, xmin, xmax);
170 binLimY=AliDielectronHelper::MakeLogBinning(nbinsY, ymin, ymax);
172 binLimY=AliDielectronHelper::MakeLinBinning(nbinsY, ymin, ymax);
176 binLimZ=AliDielectronHelper::MakeLogBinning(nbinsZ, zmin, zmax);
178 binLimZ=AliDielectronHelper::MakeLinBinning(nbinsZ, zmin, zmax);
181 UserProfile(histClass,name,title,valTypeP,binLimX,binLimY,binLimZ,valTypeX,valTypeY,valTypeZ);
184 //_____________________________________________________________________________
185 void AliDielectronHistos::UserProfile(const char* histClass,const char *name, const char* title,
191 // Histogram creation 1D case with arbitraty binning
194 TVectorD *binLimX=AliDielectronHelper::MakeArbitraryBinning(binning);
195 UserProfile(histClass,name,title,valTypeP,binLimX,valTypeX);
198 //_____________________________________________________________________________
199 void AliDielectronHistos::UserProfile(const char* histClass,const char *name, const char* title,
201 const TVectorD * const binsX,
202 UInt_t valTypeX/*=kNoAutoFill*/)
205 // Histogram creation 1D case with arbitraty binning X
206 // the TVectorD is assumed to be surplus after the creation and will be deleted!!!
210 isOk&=IsHistogramOk(histClass,name);
216 hist=new TH1F(name,title,binsX->GetNrows()-1,binsX->GetMatrixArray());
218 hist=new TProfile(name,title,binsX->GetNrows()-1,binsX->GetMatrixArray());
221 // store var for profile in fBits
222 StoreVarForProfile(hist,valTypeP);
224 Bool_t isReserved=fReservedWords->Contains(histClass);
225 UInt_t uniqueID = valTypeX;
226 if(valTypeP!=999) uniqueID |= 0x80000000; // set last bit to 1 (for profiles only)
228 UserHistogramReservedWords(histClass, hist, uniqueID);
230 UserHistogram(histClass, hist, uniqueID);
236 //_____________________________________________________________________________
237 void AliDielectronHistos::UserProfile(const char* histClass,const char *name, const char* title,
239 const TVectorD * const binsX, const TVectorD * const binsY,
240 UInt_t valTypeX/*=kNoAutoFill*/, UInt_t valTypeY/*=0*/)
243 // Histogram creation 2D case with arbitraty binning X
244 // the TVectorD is assumed to be surplus after the creation and will be deleted!!!
248 isOk&=IsHistogramOk(histClass,name);
255 hist=new TH2F(name,title,
256 binsX->GetNrows()-1,binsX->GetMatrixArray(),
257 binsY->GetNrows()-1,binsY->GetMatrixArray());
260 hist=new TProfile2D(name,title,
261 binsX->GetNrows()-1,binsX->GetMatrixArray(),
262 binsY->GetNrows()-1,binsY->GetMatrixArray());
264 // store var for profile in fBits
265 StoreVarForProfile(hist,valTypeP);
267 Bool_t isReserved=fReservedWords->Contains(histClass);
268 // switched from 2 digits encoding to 3 digits
269 UInt_t uniqueID = valTypeX+1000*valTypeY;
270 if(valTypeP!=999) uniqueID |= 0x80000000; // set last bit to 1 (for profiles only)
273 UserHistogramReservedWords(histClass, hist, uniqueID);
275 UserHistogram(histClass, hist, uniqueID);
283 //_____________________________________________________________________________
284 void AliDielectronHistos::UserProfile(const char* histClass,const char *name, const char* title,
286 const TVectorD * const binsX, const TVectorD * const binsY, const TVectorD * const binsZ,
287 UInt_t valTypeX/*=kNoAutoFill*/, UInt_t valTypeY/*=0*/, UInt_t valTypeZ/*=0*/)
290 // Histogram creation 3D case with arbitraty binning X
291 // the TVectorD is assumed to be surplus after the creation and will be deleted!!!
295 isOk&=IsHistogramOk(histClass,name);
303 hist=new TH3F(name,title,
304 binsX->GetNrows()-1,binsX->GetMatrixArray(),
305 binsY->GetNrows()-1,binsY->GetMatrixArray(),
306 binsZ->GetNrows()-1,binsZ->GetMatrixArray());
308 hist=new TProfile3D(name,title,
309 binsX->GetNrows()-1,binsX->GetMatrixArray(),
310 binsY->GetNrows()-1,binsY->GetMatrixArray(),
311 binsZ->GetNrows()-1,binsZ->GetMatrixArray());
313 // store var for profile in fBits
314 StoreVarForProfile(hist,valTypeP);
316 Bool_t isReserved=fReservedWords->Contains(histClass);
317 // switched from 2 digits encoding to 3 digits
318 UInt_t uniqueID = valTypeX+1000*valTypeY+1000000*valTypeZ;
319 if(valTypeP!=999) uniqueID |= 0x80000000; // set last bit to 1 (for profiles only)
322 UserHistogramReservedWords(histClass, hist, uniqueID);
324 UserHistogram(histClass, hist, uniqueID);
332 //_____________________________________________________________________________
333 void AliDielectronHistos::UserHistogram(const char* histClass, TH1* hist, UInt_t valTypes)
336 // Add any type of user histogram
339 //special case for the calss Pair. where histograms will be created for all pair classes
340 Bool_t isReserved=fReservedWords->Contains(histClass);
342 UserHistogramReservedWords(histClass, hist, valTypes);
346 if (!IsHistogramOk(histClass,hist->GetName())) return;
348 THashList *classTable=(THashList*)fHistoList.FindObject(histClass);
349 hist->SetDirectory(0);
350 hist->SetUniqueID(valTypes);
352 classTable->Add(hist);
355 //_____________________________________________________________________________
356 void AliDielectronHistos::AddClass(const char* histClass)
359 // Add a class of histograms
360 // Several classes can be added by separating them by a ';' e.g. 'class1;class2;class3'
362 TString hists(histClass);
363 TObjArray *arr=hists.Tokenize(";");
366 while ( (o=next()) ){
367 if (fHistoList.FindObject(o->GetName())){
368 Warning("AddClass","Cannot create class '%s' it already exists.",histClass);
371 if (fReservedWords->Contains(o->GetName())){
372 Error("AddClass","Pair is a reserved word, please use another name");
375 THashList *table=new THashList;
376 table->SetOwner(kTRUE);
377 table->SetName(o->GetName());
378 fHistoList.Add(table);
383 //_____________________________________________________________________________
384 void AliDielectronHistos::Fill(const char* histClass, const char* name, Double_t xval)
387 // Fill function 1D case
389 THashList *classTable=(THashList*)fHistoList.FindObject(histClass);
391 if (!classTable || !(hist=(TH1*)classTable->FindObject(name)) ){
392 Warning("Fill","Cannot fill histogram. Either class '%s' or histogram '%s' not existing.",histClass,name);
398 //_____________________________________________________________________________
399 void AliDielectronHistos::Fill(const char* histClass, const char* name, Double_t xval, Double_t yval)
402 // Fill function 2D case
404 THashList *classTable=(THashList*)fHistoList.FindObject(histClass);
406 if (!classTable || !(hist=(TH2*)classTable->FindObject(name)) ){
407 Warning("UserHistogram","Cannot fill histogram. Either class '%s' or histogram '%s' not existing.",histClass,name);
410 hist->Fill(xval,yval);
413 //_____________________________________________________________________________
414 void AliDielectronHistos::Fill(const char* histClass, const char* name, Double_t xval, Double_t yval, Double_t zval)
417 // Fill function 3D case
419 THashList *classTable=(THashList*)fHistoList.FindObject(histClass);
421 if (!classTable || !(hist=(TH3*)classTable->FindObject(name)) ){
422 Warning("UserHistogram","Cannot fill histogram. Either class '%s' or histogram '%s' not existing.",histClass,name);
425 hist->Fill(xval,yval,zval);
428 //_____________________________________________________________________________
429 void AliDielectronHistos::FillClass(const char* histClass, Int_t nValues, const Double_t *values)
432 // Fill class 'histClass' (by name)
435 THashList *classTable=(THashList*)fHistoList.FindObject(histClass);
437 Warning("FillClass","Cannot fill class '%s' its not defined.",histClass);
441 TIter nextHist(classTable);
443 while ( (hist=(TH1*)nextHist()) ){
444 UInt_t value4=hist->TestBits(0x00ffffff)>>14; // get profile var stored in fBits
445 UInt_t valueTypes=hist->GetUniqueID()&0x7fffffff; // ignore "boolean" bit for profiles
447 if (valueTypes==(UInt_t)kNoAutoFill) continue;
448 UInt_t value1=valueTypes%1000; //last three digits
449 UInt_t value2=valueTypes/1000%1000; //second last three digits
450 UInt_t value3=valueTypes/1000000%1000; //third last three digits
452 if (value1>=(UInt_t)nValues||value2>=(UInt_t)nValues||value3>=(UInt_t)nValues||(value4>=(UInt_t)nValues && value4!=999)) {
453 Warning("FillClass","One of the values is out of range. Not filling Histogram '%s/%s'.", histClass, hist->GetName());
456 switch (hist->GetDimension()){
458 if(value4==999) hist->Fill(values[value1]); // histograms
459 else ((TProfile*)hist)->Fill(values[value1],values[value4]); // profiles
462 if(value4==999) ((TH2*)hist)->Fill(values[value1],values[value2]);
463 else ((TProfile2D*)hist)->Fill(values[value1],values[value2],values[value4]);
466 if(value4==999) ((TH3*)hist)->Fill(values[value1],values[value2],values[value3]);
467 else ((TProfile3D*)hist)->Fill(values[value1],values[value2],values[value3],values[value4]);
473 //_____________________________________________________________________________
474 // void AliDielectronHistos::FillClass(const char* histClass, const TVectorD &vals)
479 // FillClass(histClass, vals.GetNrows(), vals.GetMatrixArray());
482 //_____________________________________________________________________________
483 void AliDielectronHistos::UserHistogramReservedWords(const char* histClass, const TH1 *hist, UInt_t valTypes)
486 // Creation of histogram for all pair types
488 TString title(hist->GetTitle());
489 // Same Event Like Sign
490 TIter nextClass(&fHistoList);
492 while ( (l=static_cast<THashList*>(nextClass())) ){
493 TString name(l->GetName());
494 if (name.Contains(histClass)){
495 TH1 *h=static_cast<TH1*>(hist->Clone());
497 h->SetTitle(Form("%s %s",title.Data(),l->GetName()));
499 UserHistogram(l->GetName(),h,valTypes);
505 //_____________________________________________________________________________
506 void AliDielectronHistos::DumpToFile(const char* file)
509 // Dump the histogram list to a newly created root file
511 TFile f(file,"recreate");
512 fHistoList.Write(fHistoList.GetName(),TObject::kSingleKey);
516 //_____________________________________________________________________________
517 TH1* AliDielectronHistos::GetHistogram(const char* histClass, const char* name) const
520 // return histogram 'name' in 'histClass'
522 THashList *classTable=(THashList*)fHistoList.FindObject(histClass);
523 if (!classTable) return 0x0;
524 return (TH1*)classTable->FindObject(name);
527 //_____________________________________________________________________________
528 TH1* AliDielectronHistos::GetHistogram(const char* cutClass, const char* histClass, const char* name) const
531 // return histogram from list of list of histograms
532 // this function is thought for retrieving histograms if a list of AliDielectronHistos is set
535 if (!fList) return 0x0;
536 THashList *h=dynamic_cast<THashList*>(fList->FindObject(cutClass));
538 THashList *classTable=dynamic_cast<THashList*>(h->FindObject(histClass));
539 if (!classTable) return 0x0;
540 return (TH1*)classTable->FindObject(name);
543 //_____________________________________________________________________________
544 void AliDielectronHistos::Draw(const Option_t* option)
550 TString drawStr(option);
551 TObjArray *arr=drawStr.Tokenize(";");
556 TObjString *ostr=0x0;
560 while ( (ostr=(TObjString*)nextOpt()) ){
561 currentOpt=ostr->GetString();
562 currentOpt.Remove(TString::kBoth,'\t');
563 currentOpt.Remove(TString::kBoth,' ');
566 if ( currentOpt.Contains(testOpt.Data()) ){
567 drawClasses=currentOpt(testOpt.Length(),currentOpt.Length());
574 // Bool_t same=drawOpt.Contains("same"); //FIXME not yet implemented
579 Error("Draw","When writing to a file you have to create a canvas before opening the file!!!");
587 TIter nextClass(&fHistoList);
588 THashList *classTable=0;
589 // Bool_t first=kTRUE;
590 while ( (classTable=(THashList*)nextClass()) ){
591 //test classes option
592 if (!drawClasses.IsNull() && !drawClasses.Contains(classTable->GetName())) continue;
594 Int_t nPads = classTable->GetEntries();
595 Int_t nCols = (Int_t)TMath::Ceil( TMath::Sqrt(nPads) );
596 Int_t nRows = (Int_t)TMath::Ceil( (Double_t)nPads/(Double_t)nCols );
601 canvasName.Form("c%s_%s",GetName(),classTable->GetName());
602 c=(TCanvas*)gROOT->FindObject(canvasName.Data());
603 if (!c) c=new TCanvas(canvasName.Data(),Form("%s: %s",GetName(),classTable->GetName()));
608 // if (nPads>1) gVirtualPS->NewPage();
610 if (nPads>1) c->Clear();
613 if (nCols>1||nRows>1) c->Divide(nCols,nRows);
615 //loop over histograms and draw them
616 TIter nextHist(classTable);
619 while ( (h=(TH1*)nextHist()) ){
621 if ( (h->InheritsFrom(TH2::Class())) ) drawOpt="colz";
622 if (nCols>1||nRows>1) c->cd(++iPad);
623 if ( TMath::Abs(h->GetXaxis()->GetBinWidth(1)-h->GetXaxis()->GetBinWidth(2))>1e-10 ) gPad->SetLogx();
624 if ( TMath::Abs(h->GetYaxis()->GetBinWidth(1)-h->GetYaxis()->GetBinWidth(2))>1e-10 ) gPad->SetLogy();
625 if ( TMath::Abs(h->GetZaxis()->GetBinWidth(1)-h->GetZaxis()->GetBinWidth(2))>1e-10 ) gPad->SetLogz();
626 TString histOpt=h->GetOption();
628 if (histOpt.Contains("logx")) gPad->SetLogx();
629 if (histOpt.Contains("logy")) gPad->SetLogy();
630 if (histOpt.Contains("logz")) gPad->SetLogz();
631 histOpt.ReplaceAll("logx","");
632 histOpt.ReplaceAll("logy","");
633 histOpt.ReplaceAll("logz","");
634 h->Draw(drawOpt.Data());
641 // if (gVirtualPS) delete c;
644 //_____________________________________________________________________________
645 void AliDielectronHistos::Print(const Option_t* option) const
648 // Print classes and histograms
650 TString optString(option);
652 if (optString.IsNull()) PrintStructure();
658 //_____________________________________________________________________________
659 void AliDielectronHistos::PrintStructure() const
662 // Print classes and histograms in the class to stdout
665 TIter nextClass(&fHistoList);
666 THashList *classTable=0;
667 while ( (classTable=(THashList*)nextClass()) ){
668 TIter nextHist(classTable);
670 printf("+ %s\n",classTable->GetName());
671 while ( (o=nextHist()) )
672 printf("| ->%s\n",o->GetName());
675 TIter nextCutClass(fList);
676 THashList *cutClass=0x0;
677 while ( (cutClass=(THashList*)nextCutClass()) ) {
678 printf("+ %s\n",cutClass->GetName());
679 TIter nextClass(cutClass);
680 THashList *classTable=0;
681 while ( (classTable=(THashList*)nextClass()) ){
682 TIter nextHist(classTable);
684 printf("| + %s\n",classTable->GetName());
685 while ( (o=nextHist()) )
686 printf("| | ->%s\n",o->GetName());
693 //_____________________________________________________________________________
694 void AliDielectronHistos::SetHistogramList(THashList &list, Bool_t setOwner/*=kTRUE*/)
697 // set histogram classes and histograms to this instance. It will take onwnership!
699 ResetHistogramList();
700 TString name(GetName());
701 if (name == "AliDielectronHistos") SetName(list.GetName());
704 while ( (o=next()) ){
708 list.SetOwner(kFALSE);
709 fHistoList.SetOwner(kTRUE);
711 fHistoList.SetOwner(kFALSE);
715 //_____________________________________________________________________________
716 Bool_t AliDielectronHistos::SetCutClass(const char* cutClass)
719 // Assign histogram list according to cutClass
722 if (!fList) return kFALSE;
723 ResetHistogramList();
724 THashList *h=dynamic_cast<THashList*>(fList->FindObject(cutClass));
726 Warning("SetCutClass","cutClass '%s' not found", cutClass);
729 SetHistogramList(*h,kFALSE);
733 //_____________________________________________________________________________
734 Bool_t AliDielectronHistos::IsHistogramOk(const char* histClass, const char* name)
737 // check whether the histogram class exists and the histogram itself does not exist yet
739 Bool_t isReserved=fReservedWords->Contains(histClass);
740 if (!fHistoList.FindObject(histClass)&&!isReserved){
741 Warning("IsHistogramOk","Cannot create histogram. Class '%s' not defined. Please create it using AddClass before.",histClass);
744 if (GetHistogram(histClass,name)){
745 Warning("IsHistogramOk","Cannot create histogram '%s' in class '%s': It already exists!",name,histClass);
751 // //_____________________________________________________________________________
752 // TIterator* AliDielectronHistos::MakeIterator(Bool_t dir) const
757 // return new TListIter(&fHistoList, dir);
760 //_____________________________________________________________________________
761 void AliDielectronHistos::ReadFromFile(const char* file)
764 // Read histos from file
767 TIter nextKey(f.GetListOfKeys());
769 while ( (key=(TKey*)nextKey()) ){
770 TObject *o=f.Get(key->GetName());
771 THashList *list=dynamic_cast<THashList*>(o);
773 SetHistogramList(*list);
779 //_____________________________________________________________________________
780 void AliDielectronHistos::DrawSame(const char* histName, const Option_t *opt)
783 // Draw all histograms with the same name into one canvas
784 // if option contains 'leg' a legend will be created with the class name as caption
785 // if option contains 'can' a new canvas is created
788 TString optString(opt);
790 Bool_t optLeg=optString.Contains("leg");
791 Bool_t optCan=optString.Contains("can");
796 c=(TCanvas*)gROOT->FindObject(Form("c%s",histName));
797 if (!c) c=new TCanvas(Form("c%s",histName),Form("All '%s' histograms",histName));
802 if (optLeg) leg=new TLegend(.8,.3,.99,.9);
805 TIter next(&fHistoList);
806 THashList *classTable=0;
809 while ( (classTable=(THashList*)next()) ){
810 if ( TH1 *h=(TH1*)classTable->FindObject(histName) ){
812 h->SetLineColor(i+1);
813 h->SetMarkerColor(i+1);
814 h->Draw(i>0?"same":"");
815 if (leg) leg->AddEntry(h,classTable->GetName(),"lp");
817 max=TMath::Max(max,h->GetMaximum());
821 leg->SetFillColor(10);
822 leg->SetY1(.9-i*.05);
825 if (hFirst&&(hFirst->GetYaxis()->GetXmax()<max)){
826 hFirst->SetMaximum(max);
830 //_____________________________________________________________________________
831 void AliDielectronHistos::SetReservedWords(const char* words)
834 // set reserved words
837 (*fReservedWords)=words;
840 //_____________________________________________________________________________
841 void AliDielectronHistos::StoreVarForProfile(TObject *obj, UInt_t valType)
844 // store var for TProfiles in TObject::fBits [14-23]
847 for(UInt_t i=14; i < sizeof(UInt_t)*8; i++) {
848 if(TESTBIT(valType,i-14)) {
849 obj->SetBit(1<<i,kTRUE);