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>
28 #include <TCollection.h>
29 #include <THashList.h>
31 #include <TObjArray.h>
39 // #include <TVectorD.h>
41 #include "AliDielectronHistos.h"
44 ClassImp(AliDielectronHistos)
47 AliDielectronHistos::AliDielectronHistos() :
49 TNamed("AliDielectronHistos","Dielectron Histogram Container"),
51 fReservedWords(new TString)
54 // Default constructor
56 fHistoList.SetOwner(kTRUE);
57 fHistoList.SetName("Dielectron_Histos");
60 //_____________________________________________________________________________
61 AliDielectronHistos::AliDielectronHistos(const char* name, const char* title) :
65 fReservedWords(new TString)
70 fHistoList.SetOwner(kTRUE);
71 fHistoList.SetName(name);
74 //_____________________________________________________________________________
75 AliDielectronHistos::~AliDielectronHistos()
81 delete fReservedWords;
84 //_____________________________________________________________________________
85 void AliDielectronHistos::UserHistogram(const char* histClass,const char *name, const char* title,
86 Int_t nbinsX, Double_t xmin, Double_t xmax,
90 // Default histogram creation 1D case
92 if (!IsHistogramOk(histClass,name)) return;
94 TH1* hist=new TH1F(name,title,nbinsX,xmin,xmax);
95 Bool_t isReserved=fReservedWords->Contains(histClass);
97 UserHistogramReservedWords(histClass, hist, valTypeX);
99 UserHistogram(histClass, hist, valTypeX);
102 //_____________________________________________________________________________
103 void AliDielectronHistos::UserHistogram(const char* histClass,const char *name, const char* title,
104 Int_t nbinsX, Double_t xmin, Double_t xmax,
105 Int_t nbinsY, Double_t ymin, Double_t ymax,
106 UInt_t valTypeX, UInt_t valTypeY)
109 // Default histogram creation 2D case
111 if (!IsHistogramOk(histClass,name)) return;
113 TH1* hist=new TH2F(name,title,nbinsX,xmin,xmax,nbinsY,ymin,ymax);
114 Bool_t isReserved=fReservedWords->Contains(histClass);
116 UserHistogramReservedWords(histClass, hist, valTypeX+100*valTypeY);
118 UserHistogram(histClass, hist, valTypeX+100*valTypeY);
122 //_____________________________________________________________________________
123 void AliDielectronHistos::UserHistogram(const char* histClass,const char *name, const char* title,
124 Int_t nbinsX, Double_t xmin, Double_t xmax,
125 Int_t nbinsY, Double_t ymin, Double_t ymax,
126 Int_t nbinsZ, Double_t zmin, Double_t zmax,
127 UInt_t valTypeX, UInt_t valTypeY, UInt_t valTypeZ)
130 // Default histogram creation 3D case
132 if (!IsHistogramOk(histClass,name)) return;
134 TH1* hist=new TH3F(name,title,nbinsX,xmin,xmax,nbinsY,ymin,ymax,nbinsZ,zmin,zmax);
135 Bool_t isReserved=fReservedWords->Contains(histClass);
137 UserHistogramReservedWords(histClass, hist, valTypeX+100*valTypeY+10000*valTypeZ);
139 UserHistogram(histClass, hist, valTypeX+100*valTypeY+10000*valTypeZ);
142 //_____________________________________________________________________________
143 void AliDielectronHistos::UserHistogram(const char* histClass, TH1* hist, UInt_t valTypes)
146 // Add any type of user histogram
149 //special case for the calss Pair. where histograms will be created for all pair classes
150 Bool_t isReserved=fReservedWords->Contains(histClass);
152 UserHistogramReservedWords(histClass, hist, valTypes);
156 if (!IsHistogramOk(histClass,hist->GetName())) return;
158 THashList *classTable=(THashList*)fHistoList.FindObject(histClass);
159 hist->SetDirectory(0);
160 hist->SetUniqueID(valTypes);
161 classTable->Add(hist);
164 //_____________________________________________________________________________
165 void AliDielectronHistos::AddClass(const char* histClass)
168 // Add a class of histograms
169 // Several classes can be added by separating them by a ';' e.g. 'class1;class2;class3'
171 TString hists(histClass);
172 TObjArray *arr=hists.Tokenize(";");
175 while ( (o=next()) ){
176 if (fHistoList.FindObject(o->GetName())){
177 Warning("AddClass","Cannot create class '%s' it already exists.",histClass);
180 if (fReservedWords->Contains(o->GetName())){
181 Error("AddClass","Pair is a reserved word, please use another name");
184 THashList *table=new THashList;
185 table->SetOwner(kTRUE);
186 table->SetName(o->GetName());
187 fHistoList.Add(table);
192 //_____________________________________________________________________________
193 void AliDielectronHistos::Fill(const char* histClass, const char* name, Double_t xval)
196 // Fill function 1D case
198 THashList *classTable=(THashList*)fHistoList.FindObject(histClass);
200 if (!classTable || !(hist=(TH1*)classTable->FindObject(name)) ){
201 Warning("Fill","Cannot fill histogram. Either class '%s' or histogram '%s' not existing.",histClass,name);
207 //_____________________________________________________________________________
208 void AliDielectronHistos::Fill(const char* histClass, const char* name, Double_t xval, Double_t yval)
211 // Fill function 2D case
213 THashList *classTable=(THashList*)fHistoList.FindObject(histClass);
215 if (!classTable || !(hist=(TH2*)classTable->FindObject(name)) ){
216 Warning("UserHistogram","Cannot fill histogram. Either class '%s' or histogram '%s' not existing.",histClass,name);
219 hist->Fill(xval,yval);
222 //_____________________________________________________________________________
223 void AliDielectronHistos::Fill(const char* histClass, const char* name, Double_t xval, Double_t yval, Double_t zval)
226 // Fill function 3D case
228 THashList *classTable=(THashList*)fHistoList.FindObject(histClass);
230 if (!classTable || !(hist=(TH3*)classTable->FindObject(name)) ){
231 Warning("UserHistogram","Cannot fill histogram. Either class '%s' or histogram '%s' not existing.",histClass,name);
234 hist->Fill(xval,yval,zval);
237 //_____________________________________________________________________________
238 void AliDielectronHistos::FillClass(const char* histClass, Int_t nValues, const Double_t *values)
241 // Fill class 'histClass' (by name)
244 THashList *classTable=(THashList*)fHistoList.FindObject(histClass);
246 Warning("FillClass","Cannot fill class '%s' its not defined.",histClass);
250 TIter nextHist(classTable);
252 while ( (hist=(TH1*)nextHist()) ){
253 UInt_t valueTypes=hist->GetUniqueID();
254 if (valueTypes==(UInt_t)kNoAutoFill) continue;
255 UInt_t value1=valueTypes%100; //last two digits
256 UInt_t value2=valueTypes/100%100; //second last two digits
257 UInt_t value3=valueTypes/10000%100; //third last two digits
258 if (value1>=(UInt_t)nValues||value2>=(UInt_t)nValues||value3>=(UInt_t)nValues) {
259 Warning("FillClass","One of the values is out of range. Not filling histogram '%s/%s'.", histClass, hist->GetName());
262 switch (hist->GetDimension()){
264 hist->Fill(values[value1]);
267 ((TH2*)hist)->Fill(values[value1],values[value2]);
270 ((TH3*)hist)->Fill(values[value1],values[value2],values[value3]);
276 //_____________________________________________________________________________
277 // void AliDielectronHistos::FillClass(const char* histClass, const TVectorD &vals)
282 // FillClass(histClass, vals.GetNrows(), vals.GetMatrixArray());
285 //_____________________________________________________________________________
286 void AliDielectronHistos::UserHistogramReservedWords(const char* histClass, TH1 *hist, UInt_t valTypes)
289 // Creation of histogram for all pair types
291 TString title(hist->GetTitle());
292 // Same Event Like Sign
293 TIter nextClass(&fHistoList);
295 while ( (l=static_cast<THashList*>(nextClass())) ){
296 TString name(l->GetName());
297 if (name.Contains(histClass)){
298 TH1 *h=static_cast<TH1*>(hist->Clone());
300 h->SetTitle(Form("%s %s",title.Data(),l->GetName()));
301 UserHistogram(l->GetName(),h,valTypes);
307 //_____________________________________________________________________________
308 void AliDielectronHistos::DumpToFile(const char* file)
311 // Dump the histogram list to a newly created root file
313 TFile f(file,"recreate");
314 fHistoList.Write(fHistoList.GetName(),TObject::kSingleKey);
318 //_____________________________________________________________________________
319 TH1* AliDielectronHistos::GetHistogram(const char* histClass, const char* name) const
322 // return histogram 'name' in 'histClass'
324 THashList *classTable=(THashList*)fHistoList.FindObject(histClass);
325 if (!classTable) return 0x0;
326 return (TH1*)classTable->FindObject(name);
329 //_____________________________________________________________________________
330 void AliDielectronHistos::Draw(const Option_t* option)
336 TString drawOpt(option);
339 // Bool_t same=drawOpt.Contains("same"); //FIXME not yet implemented
341 TIter nextClass(&fHistoList);
342 THashList *classTable=0;
343 while ( (classTable=(THashList*)nextClass()) ){
345 Int_t nPads = classTable->GetEntries();
346 Int_t nCols = (Int_t)TMath::Ceil( TMath::Sqrt(nPads) );
347 Int_t nRows = (Int_t)TMath::Ceil( (Double_t)nPads/(Double_t)nCols );
351 canvasName.Form("c%s",classTable->GetName());
352 TCanvas *c=(TCanvas*)gROOT->FindObject(canvasName.Data());
353 if (!c) c=new TCanvas(canvasName.Data(),classTable->GetName());
355 c->Divide(nCols,nRows);
357 //loop over histograms and draw them
358 TIter nextHist(classTable);
361 while ( (h=(TH1*)nextHist()) ){
363 if ( (h->InheritsFrom(TH2::Class())) ) drawOpt="colz";
365 h->Draw(drawOpt.Data());
370 //_____________________________________________________________________________
371 void AliDielectronHistos::Print(const Option_t* option) const
374 // Print classes and histograms
376 TString optString(option);
378 if (optString.IsNull()) PrintStructure();
384 //_____________________________________________________________________________
385 void AliDielectronHistos::PrintStructure() const
388 // Print classes and histograms in the class to stdout
390 TIter nextClass(&fHistoList);
391 THashList *classTable=0;
392 while ( (classTable=(THashList*)nextClass()) ){
393 TIter nextHist(classTable);
395 printf("+ %s\n",classTable->GetName());
396 while ( (o=nextHist()) )
397 printf("| ->%s\n",o->GetName());
402 //_____________________________________________________________________________
403 void AliDielectronHistos::SetHistogramList(THashList &list)
406 // set histogram classes and histograms to this instance. It will take onwnership!
410 while ( (o=next()) ){
413 list.SetOwner(kFALSE);
414 fHistoList.SetOwner(kTRUE);
417 //_____________________________________________________________________________
418 Bool_t AliDielectronHistos::IsHistogramOk(const char* histClass, const char* name)
421 // check whether the histogram class exists and the histogram itself does not exist yet
423 Bool_t isReserved=fReservedWords->Contains(histClass);
424 if (!fHistoList.FindObject(histClass)&&!isReserved){
425 Warning("IsHistogramOk","Cannot create histogram. Class '%s' not defined. Please create it using AddClass before.",histClass);
428 if (GetHistogram(histClass,name)){
429 Warning("IsHistogramOk","Cannot create histogram '%s' in class '%s': It already exists!",name,histClass);
435 // //_____________________________________________________________________________
436 // TIterator* AliDielectronHistos::MakeIterator(Bool_t dir) const
441 // return new TListIter(&fHistoList, dir);
444 //_____________________________________________________________________________
445 void AliDielectronHistos::ReadFromFile(const char* file)
448 // Read histos from file
451 TIter nextKey(f.GetListOfKeys());
453 while ( (key=(TKey*)nextKey()) ){
454 TObject *o=f.Get(key->GetName());
455 THashList *list=dynamic_cast<THashList*>(o);
457 SetHistogramList(*list);
463 void AliDielectronHistos::DrawSame(const char* histName, const Option_t *opt)
466 // Draw all histograms with the same name into one canvas
467 // if option contains 'leg' a legend will be created with the class name as caption
468 // if option contains 'can' a new canvas is created
471 TString optString(opt);
473 Bool_t optLeg=optString.Contains("leg");
474 Bool_t optCan=optString.Contains("can");
479 c=(TCanvas*)gROOT->FindObject(Form("c%s",histName));
480 if (!c) c=new TCanvas(Form("c%s",histName),Form("All '%s' histograms",histName));
485 if (optLeg) leg=new TLegend(.8,.3,.99,.9);
488 TIter next(&fHistoList);
489 THashList *classTable=0;
492 while ( (classTable=(THashList*)next()) ){
493 if ( TH1 *h=(TH1*)classTable->FindObject(histName) ){
495 h->SetLineColor(i+1);
496 h->SetMarkerColor(i+1);
497 h->Draw(i>0?"same":"");
498 if (leg) leg->AddEntry(h,classTable->GetName(),"lp");
500 max=TMath::Max(max,h->GetMaximum());
504 leg->SetFillColor(10);
505 leg->SetY1(.9-i*.05);
508 if (hFirst&&(hFirst->GetYaxis()->GetXmax()<max)){
509 hFirst->SetMaximum(max);
513 //_____________________________________________________________________________
514 void AliDielectronHistos::SetReservedWords(const char* words)
517 // set reserved words
520 (*fReservedWords)=words;