New method SetNCarlos in order to set the number of CARLOS chips connected to the...
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALHistoUtilities.cxx
CommitLineData
315d1c64 1/**************************************************************************
2 * Copyright(c) 1998-2002, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
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 **************************************************************************/
15
9edefa04 16/* $Id$ */
315d1c64 17
d434833b 18//_________________________________________________________________________
14ce0a6e 19// This is a set of histogram
20// utilities for the EMCAL
21// to make some common
22// functions easier
d434833b 23//
315d1c64 24//*-- Authors: J.L. Klay (LLNL) & Aleksei Pavlinov (WSU)
25
d434833b 26#include "AliEMCALHistoUtilities.h"
315d1c64 27
af303720 28#include <iostream>
29#include <iomanip>
30#include <fstream>
31
32#include <TROOT.h>
33#include <TPad.h>
315d1c64 34#include <TFile.h>
315d1c64 35#include <TH1.h>
36#include <TH2.h>
af303720 37#include <TF1.h>
38#include <TLatex.h>
39#include <TChain.h>
9edefa04 40#include <TList.h>
41#include <TObjArray.h>
d434833b 42#include <TObjString.h>
43#include <TRegexp.h>
9edefa04 44#include <TString.h>
af303720 45#include <TLorentzVector.h>
46#include <Gtypes.h> // color, line style and so on
47
48#include "AliESDCaloCluster.h"
49#include "AliEMCALRecPoint.h"
50
51using namespace std;
315d1c64 52
53ClassImp(AliEMCALHistoUtilities)
54
55AliEMCALHistoUtilities::AliEMCALHistoUtilities(const char *name, const char *tit) : TNamed(name,tit)
56{
d434833b 57 // constructor
315d1c64 58}
59
60AliEMCALHistoUtilities::~AliEMCALHistoUtilities()
61{
62 //destructor
63}
64
315d1c64 65TList* AliEMCALHistoUtilities::MoveHistsToList(const char* name, Bool_t putToBrowser)
66{
67 // Move HIST to list
68 gROOT->cd();
69 TIter nextHist(gDirectory->GetList());
70 TList *list = new TList;
71 list->SetName(name);
72 TObject *objHist;
73 while((objHist=nextHist())){
74 if (!objHist->InheritsFrom("TH1")) continue;
75 ((TH1*)objHist)->SetDirectory(0); // Remove from gROOT
76 list->Add(objHist);
af303720 77 gDirectory->GetList()->Remove(objHist);
315d1c64 78 }
79 if(putToBrowser) gROOT->GetListOfBrowsables()->Add((TObject*)list);
80 return list;
81}
82
83void AliEMCALHistoUtilities::FillH1(TList *l, Int_t ind, Double_t x, Double_t w)
84{
14ce0a6e 85 //fill 1d histogram
315d1c64 86 static TH1* hid=0;
87 if(l == 0) return;
88 if(ind < l->GetSize()){
89 hid = (TH1*)l->At(ind);
90 hid->Fill(x,w);
91 }
92}
93
94void AliEMCALHistoUtilities::FillH2(TList *l, Int_t ind, Double_t x, Double_t y, Double_t w)
95{
14ce0a6e 96 //fill 2d histogram
315d1c64 97 static TH2* hid=0;
98 if(l == 0) return;
99 if(ind < l->GetSize()){
100 hid = (TH2*)l->At(ind);
101 hid->Fill(x,y,w);
102 }
103}
104
4800667c 105int AliEMCALHistoUtilities::SaveListOfHists(TList *mylist,const char* name,Bool_t kSingleKey,const char* opt)
315d1c64 106{
14ce0a6e 107 //write histograms to file
315d1c64 108 printf(" Name of out file |%s|\n", name);
109 int save = 0;
4800667c 110 if(mylist && mylist->GetSize() && strlen(name)){
315d1c64 111 TString nf(name);
112 if(nf.Contains(".root") == kFALSE) nf += ".root";
113 TFile file(nf.Data(),opt);
4800667c 114 TIter nextHist(mylist);
315d1c64 115 TObject* objHist=0;
116 int nh=0;
117 if(kSingleKey) {
118 file.cd();
4800667c 119 mylist->Write(mylist->GetName(),TObject::kSingleKey);
120 mylist->ls();
315d1c64 121 save = 1;
122 } else {
123 while((objHist=nextHist())) { // loop over list
124 if(objHist->InheritsFrom("TH1")) {
125 TH1* hid = (TH1*)objHist;
126 file.cd();
127 hid->Write();
128 nh++;
129 printf("Save hist. %s \n",hid ->GetName());
130 }
131 }
132 printf("%i hists. save to file -> %s\n", nh,file.GetName());
133 if(nh>0) save = 1;
134 }
135 file.Close();
136 } else {
137 printf("AliEMCALHistoUtilities::SaveListOfHists : N O S A V I N G \n");
315d1c64 138 }
139 return save;
140}
d434833b 141
c2d4c7af 142void AliEMCALHistoUtilities::AddToNameAndTitle(TH1 *h, const char *name, const char *title)
143{
144 if(h==0) return;
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));
147}
148
149void AliEMCALHistoUtilities::AddToNameAndTitleToList(TList *l, const char *name, const char *title)
150{
151 if(l==0) return;
152 if(name || title) {
153 for(int i=0; i<l->GetSize(); i++) {
154 TObject *o = l->At(i);
155 if(o->InheritsFrom("TH1")) {
156 TH1 *h = (TH1*)o;
157 AddToNameAndTitle(h, name, title);
158 }
159 }
160 }
161}
162
af303720 163TLatex *AliEMCALHistoUtilities::lat(const char *text, Float_t x,Float_t y, Int_t align, Float_t tsize, short tcolor)
164{
165 double y1=y;
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());
173 return latex;
174}
175
176void AliEMCALHistoUtilities::InitChain(TChain *chain, const char* nameListOfFiles, Int_t nFileMax)
177{
178 // Read name of files from text file with nameListOfFiles and added to the chain
179 if(chain==0 || nameListOfFiles==0) return;
180
181 ifstream in;
182 in.open(nameListOfFiles);
183 if (!in) {
184 cout << "Input file "<<nameListOfFiles<<" cannot be opened.\n";
185 return;
186 }
187
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;
192 chain->Add(nf);
193 nfiles++;
194 cout<<nfiles<<" "<<nf<<endl;
195 if(nFileMax && nfiles>=nFileMax) break;
196 }
197 cout << " \n ********** <I> Accepted file "<< nfiles << "********* \n"<<endl;
198 // chainEsd->Print();
199 // chain->Lookup();
200}
201
d434833b 202int AliEMCALHistoUtilities::ParseString(const TString &topt, TObjArray &Opt)
14ce0a6e 203{
204 // Moved from AliEMCALGeometry
205 // Feb 06, 2006
d434833b 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()));
215 }
216 begin += end+1;
217 }
218 return Opt.GetEntries();
219}
af303720 220// Analysis utilites
221Bool_t AliEMCALHistoUtilities::GetLorentzVectorFromESDCluster(TLorentzVector &v, const AliESDCaloCluster* cl)
222{
223 // May 8, 2007
224 static Double_t e=0.0;
225 static Float_t pos[3];
226 static TVector3 gpos;
227 if(cl==0) return kFALSE;
228
229 e = Double_t(cl->E());
230 if(e<=0.0) {
231 printf(" negative cluster energy : %f \n", e);
232 return kFALSE;
233 }
234 cl->GetPosition(pos);
235 gpos.SetXYZ(Double_t(pos[0]), Double_t(pos[1]), Double_t(pos[2]));
236 gpos.SetMag(e);
237 v.SetVectM(gpos, 0.0);
238
239 return kTRUE;
240}
241
242Bool_t AliEMCALHistoUtilities::GetLorentzVectorFromRecPoint(TLorentzVector &v, const AliEMCALRecPoint *rp)
243{
244 // Jun 20, 2007
245 static Double_t e=0.0;
246 static TVector3 gpos;
247 if(rp==0) return kFALSE;
248
249 e = Double_t(rp->GetPointEnergy());
250 if(e<=0.0) {
251 printf(" negative rec.point energy : %f \n", e);
252 return kFALSE;
253 }
254 rp->GetGlobalPosition(gpos);
255 gpos.SetMag(e);
256 v.SetVectM(gpos, 0.0);
257
258 return kTRUE;
259}
260//
261//// Drawing:
262//
263void AliEMCALHistoUtilities::DrawHist(TH1* hid,int lineWidth,int lineColor,const char* opt, int lineStyle)
264{
265 TString OPT;
266 OPT = opt;
267 if(!hid) return;
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);
274 hid->Draw(opt);
275 } else {
276 if (strcmp(opt,"empty")==0) hid->Draw();
277 else printf(" has fewer entries %i or hid is zero\n", (int) hid->GetEntries());
278 }
279}
280
281//
282//// Fitting:
283//
284TF1* 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
286 TString name("gi");
287 name += addName;
288 TF1 *F = new TF1(name.Data(), Gi, xmi, xma, 4);
289 F->SetParNames("INTEGRAL","MEAN","SIGMA","WIDTH");
290
291 F->SetParameter(0,N);
292 F->SetParameter(1,mean);
293 F->SetParameter(2,sig);
294
295 F->FixParameter(3,width); // width of histogramm bin
296 return F;
297}
298
299TF1* AliEMCALHistoUtilities::Gausi(const char *addName, double xmi, double xma, TH1 *h)
300{
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));
303 else return 0;
304}
305
306TF1* 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");
309 name += addName;
310 TF1 *F = new TF1(name.Data(), GiPol2, xmi, xma, 7);
311 F->SetParNames("INTEGRAL","MEAN","SIGMA","WIDTH","a0","a1","a2");
312
313 if(g) {
314 for(int i=0; i<4; i++) F->SetParameter(i, g->GetParameter(i));
315 F->FixParameter(3,g->GetParameter(3));
316 }
317
318 if(bg) {
319 for(int i=4; i<7; i++) F->SetParameter(i, bg->GetParameter(i+4));
320 }
321 F->SetLineColor(kRed);
322 return F;
323}
324
325Double_t AliEMCALHistoUtilities::Gi(Double_t *x, Double_t *par)
326{
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)
330
331 static Double_t C = TMath::Sqrt(2.*TMath::Pi()), y=0.0, f=0.0; // sqrt(2.*pi)
332
333 y = (x[0]-par[1])/par[2];
334 f = par[0]*par[3]/(par[2]*C) * TMath::Exp(-0.5*y*y);
335
336 return f;
337}
338
339Double_t AliEMCALHistoUtilities::GiPol2(Double_t *x, Double_t *par)
340{
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)
346
347 y = (x[0]-par[1])/par[2];
348 f = par[0]*par[3]/(par[2]*C) * TMath::Exp(-0.5*y*y);
349
350 f += par[4] + par[5]*x[0] + par[6]*x[0]*x[0];
351
352 return f;
353}
354
355