1 /**************************************************************************
2 * Copyright(c) 1998-2002, 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 **************************************************************************/
18 //_________________________________________________________________________
19 // This is a set of histogram
20 // utilities for the EMCAL
21 // to make some common
24 //*-- Authors: J.L. Klay (LLNL) & Aleksei Pavlinov (WSU)
26 #include "AliEMCALHistoUtilities.h"
41 #include <TObjArray.h>
42 #include <TObjString.h>
45 #include <TLorentzVector.h>
46 #include <Gtypes.h> // color, line style and so on
48 #include "AliESDCaloCluster.h"
49 #include "AliEMCALRecPoint.h"
53 ClassImp(AliEMCALHistoUtilities)
55 AliEMCALHistoUtilities::AliEMCALHistoUtilities(const char *name, const char *tit) : TNamed(name,tit)
60 AliEMCALHistoUtilities::~AliEMCALHistoUtilities()
65 TList* AliEMCALHistoUtilities::MoveHistsToList(const char* name, Bool_t putToBrowser)
69 TIter nextHist(gDirectory->GetList());
70 TList *list = new TList;
73 while((objHist=nextHist())){
74 if (!objHist->InheritsFrom("TH1")) continue;
75 ((TH1*)objHist)->SetDirectory(0); // Remove from gROOT
77 gDirectory->GetList()->Remove(objHist);
79 if(putToBrowser) gROOT->GetListOfBrowsables()->Add((TObject*)list);
83 void AliEMCALHistoUtilities::FillH1(TList *l, Int_t ind, Double_t x, Double_t w)
88 if(ind < l->GetSize()){
89 hid = (TH1*)l->At(ind);
94 void AliEMCALHistoUtilities::FillH2(TList *l, Int_t ind, Double_t x, Double_t y, Double_t w)
99 if(ind < l->GetSize()){
100 hid = (TH2*)l->At(ind);
105 int AliEMCALHistoUtilities::SaveListOfHists(TList *mylist,const char* name,Bool_t kSingleKey,const char* opt)
107 //write histograms to file
108 printf(" Name of out file |%s|\n", name);
110 if(mylist && mylist->GetSize() && strlen(name)){
112 if(nf.Contains(".root") == kFALSE) nf += ".root";
113 TFile file(nf.Data(),opt);
114 TIter nextHist(mylist);
119 mylist->Write(mylist->GetName(),TObject::kSingleKey);
123 while((objHist=nextHist())) { // loop over list
124 if(objHist->InheritsFrom("TH1")) {
125 TH1* hid = (TH1*)objHist;
129 printf("Save hist. %s \n",hid ->GetName());
132 printf("%i hists. save to file -> %s\n", nh,file.GetName());
137 printf("AliEMCALHistoUtilities::SaveListOfHists : N O S A V I N G \n");
142 void AliEMCALHistoUtilities::AddToNameAndTitle(TH1 *h, const char *name, const char *title)
145 if(name && strlen(name)) h->SetName(Form("%s%s",h->GetName(),name));
146 if(title && strlen(title)) h->SetTitle(Form("%s%s",h->GetTitle(),title));
149 void AliEMCALHistoUtilities::AddToNameAndTitleToList(TList *l, const char *name, const char *title)
153 for(int i=0; i<l->GetSize(); i++) {
154 TObject *o = l->At(i);
155 if(o->InheritsFrom("TH1")) {
157 AddToNameAndTitle(h, name, title);
163 TLatex *AliEMCALHistoUtilities::lat(const char *text, Float_t x,Float_t y, Int_t align, Float_t tsize, short tcolor)
166 TLatex *latex = new TLatex;
167 latex->SetTextAlign(align);
168 latex->SetTextSize(tsize);
169 latex->SetTextColor(tcolor);
170 latex->DrawLatex(x, y1, text);
171 printf("<I> AliEMCALHistoUtilities::lat() -> %s gPad->GetLogy() %i\n",
172 text, gPad->GetLogy());
176 void AliEMCALHistoUtilities::InitChain(TChain *chain, const char* nameListOfFiles, Int_t nFileMax)
178 // Read name of files from text file with nameListOfFiles and added to the chain
179 if(chain==0 || nameListOfFiles==0) return;
182 in.open(nameListOfFiles);
184 cout << "Input file "<<nameListOfFiles<<" cannot be opened.\n";
188 Int_t nfiles = 0; // number of files in chain
189 char nf[200]; // name of current file
190 while (!in.getline(nf,200).eof()) {
191 if(!in.good()) break;
194 cout<<nfiles<<" "<<nf<<endl;
195 if(nFileMax && nfiles>=nFileMax) break;
197 cout << " \n ********** <I> Accepted file "<< nfiles << "********* \n"<<endl;
198 // chainEsd->Print();
202 int AliEMCALHistoUtilities::ParseString(const TString &topt, TObjArray &Opt)
204 // Moved from AliEMCALGeometry
206 Ssiz_t begin, index, end, end2;
207 begin = index = end = end2 = 0;
208 TRegexp separator("[^ ;,\\t\\s/]+");
209 while ( (begin < topt.Length()) && (index != kNPOS) ) {
210 // loop over given options
211 index = topt.Index(separator,&end,begin);
212 if (index >= 0 && end >= 1) {
213 TString substring(topt(index,end));
214 Opt.Add(new TObjString(substring.Data()));
218 return Opt.GetEntries();
221 Bool_t AliEMCALHistoUtilities::GetLorentzVectorFromESDCluster(TLorentzVector &v, const AliESDCaloCluster* cl)
224 static Double_t e=0.0;
225 static Float_t pos[3];
226 static TVector3 gpos;
227 if(cl==0) return kFALSE;
229 e = Double_t(cl->E());
231 printf(" negative cluster energy : %f \n", e);
234 cl->GetPosition(pos);
235 gpos.SetXYZ(Double_t(pos[0]), Double_t(pos[1]), Double_t(pos[2]));
237 v.SetVectM(gpos, 0.0);
242 Bool_t AliEMCALHistoUtilities::GetLorentzVectorFromRecPoint(TLorentzVector &v, const AliEMCALRecPoint *rp)
245 static Double_t e=0.0;
246 static TVector3 gpos;
247 if(rp==0) return kFALSE;
249 e = Double_t(rp->GetPointEnergy());
251 printf(" negative rec.point energy : %f \n", e);
254 rp->GetGlobalPosition(gpos);
256 v.SetVectM(gpos, 0.0);
263 void AliEMCALHistoUtilities::DrawHist(TH1* hid,int lineWidth,int lineColor,const char* opt, int lineStyle)
268 printf(" Hist. %s : option |%s| \n", hid->GetName(), opt);
269 if(hid && hid->GetEntries()>=1.) {
270 if(lineWidth) hid->SetLineWidth(lineWidth);
271 if(lineColor) hid->SetLineColor(lineColor);
272 if(lineStyle) hid->SetLineStyle(lineStyle);
273 if(OPT.Contains("stat",TString::kIgnoreCase)) hid->SetStats(kTRUE);
276 if (strcmp(opt,"empty")==0) hid->Draw();
277 else printf(" has fewer entries %i or hid is zero\n", (int) hid->GetEntries());
284 TF1* AliEMCALHistoUtilities::Gausi(const char *addName,double xmi,double xma,double N,double mean,double sig,double width)
285 { // Fit by gaus where first parameter is the number of events under ga
288 TF1 *F = new TF1(name.Data(), Gi, xmi, xma, 4);
289 F->SetParNames("INTEGRAL","MEAN","SIGMA","WIDTH");
291 F->SetParameter(0,N);
292 F->SetParameter(1,mean);
293 F->SetParameter(2,sig);
295 F->FixParameter(3,width); // width of histogramm bin
299 TF1* AliEMCALHistoUtilities::Gausi(const char *addName, double xmi, double xma, TH1 *h)
301 // You can change parameters after that if you don't like this assumption
302 if(h) return Gausi(addName, xmi, xma, h->Integral(), h->GetMean(),h->GetRMS(), h->GetBinWidth(1));
306 TF1* AliEMCALHistoUtilities::GausiPol2(const char *addName,double xmi,double xma, TF1 *g, TF1* bg)
307 { // Fit by gaus where first parameter is the number of events under ga
308 TString name("giPol2");
310 TF1 *F = new TF1(name.Data(), GiPol2, xmi, xma, 7);
311 F->SetParNames("INTEGRAL","MEAN","SIGMA","WIDTH","a0","a1","a2");
314 for(int i=0; i<4; i++) F->SetParameter(i, g->GetParameter(i));
315 F->FixParameter(3,g->GetParameter(3));
319 for(int i=4; i<7; i++) F->SetParameter(i, bg->GetParameter(i+4));
321 F->SetLineColor(kRed);
325 Double_t AliEMCALHistoUtilities::Gi(Double_t *x, Double_t *par)
327 // First parameter is integral (number events under gaus)
328 // Forth parameter (par[3]) is width of histogram bin
329 // gaus(0) is a substitute for [0]*exp(-0.5*((x-[1])/[2])**2)
331 static Double_t C = TMath::Sqrt(2.*TMath::Pi()), y=0.0, f=0.0; // sqrt(2.*pi)
333 y = (x[0]-par[1])/par[2];
334 f = par[0]*par[3]/(par[2]*C) * TMath::Exp(-0.5*y*y);
339 Double_t AliEMCALHistoUtilities::GiPol2(Double_t *x, Double_t *par)
341 // First parameter is integral (number events under gaus)
342 // Forth parameter (par[3]) is width of histogram bin
343 // gaus(0) is a substitute for [0]*exp(-0.5*((x-[1])/[2])**2)
344 // + pol2 -> 7 parameters
345 static Double_t C = TMath::Sqrt(2.*TMath::Pi()), y=0.0, f=0.0; // sqrt(2.*pi)
347 y = (x[0]-par[1])/par[2];
348 f = par[0]*par[3]/(par[2]*C) * TMath::Exp(-0.5*y*y);
350 f += par[4] + par[5]*x[0] + par[6]*x[0]*x[0];