]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EMCAL/AliEMCALHistoUtilities.cxx
Calibration class
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALHistoUtilities.cxx
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
16 /* $Id$ */
17
18 //_________________________________________________________________________
19 // This is a set of histogram
20 // utilities for the EMCAL
21 // to make some common
22 // functions easier
23 //
24 //*-- Authors: J.L. Klay (LLNL) & Aleksei Pavlinov (WSU) 
25
26 #include "AliEMCALHistoUtilities.h"
27
28 #include <TFile.h>
29 #include <TH1.h>
30 #include <TH2.h>
31 #include <TList.h>
32 #include <TObjArray.h>
33 #include <TObjString.h>
34 #include <TROOT.h>
35 #include <TRegexp.h>
36 #include <TString.h>
37
38 ClassImp(AliEMCALHistoUtilities)
39
40 AliEMCALHistoUtilities::AliEMCALHistoUtilities(const char *name, const char *tit) : TNamed(name,tit)
41 {
42   // constructor
43 }
44
45 AliEMCALHistoUtilities::~AliEMCALHistoUtilities()
46 {
47         //destructor
48 }  
49
50 TList* AliEMCALHistoUtilities::MoveHistsToList(const char* name, Bool_t putToBrowser)
51 {
52   // Move HIST to list
53   gROOT->cd();
54   TIter nextHist(gDirectory->GetList());
55   TList *list = new TList;
56   list->SetName(name);
57   TObject *objHist;
58   while((objHist=nextHist())){
59     if (!objHist->InheritsFrom("TH1")) continue;
60     ((TH1*)objHist)->SetDirectory(0); // Remove from gROOT
61     list->Add(objHist);
62   }
63   if(putToBrowser) gROOT->GetListOfBrowsables()->Add((TObject*)list);
64   return list;
65 }
66
67 void AliEMCALHistoUtilities::FillH1(TList *l, Int_t ind, Double_t x, Double_t w)
68 {
69   //fill 1d histogram
70   static TH1* hid=0;
71   if(l == 0) return;
72   if(ind < l->GetSize()){
73     hid = (TH1*)l->At(ind);
74     hid->Fill(x,w);
75   }
76 }
77
78 void AliEMCALHistoUtilities::FillH2(TList *l, Int_t ind, Double_t x, Double_t y, Double_t w)
79 {
80   //fill 2d histogram
81   static TH2* hid=0;
82   if(l == 0) return;
83   if(ind < l->GetSize()){
84     hid = (TH2*)l->At(ind);
85     hid->Fill(x,y,w);
86   }
87 }
88
89 int AliEMCALHistoUtilities::SaveListOfHists(TList *mylist,const char* name,Bool_t kSingleKey,const char* opt)
90 {
91   //write histograms to file
92   printf(" Name of out file |%s|\n", name); 
93   int save = 0;
94   if(mylist && mylist->GetSize() && strlen(name)){
95     TString nf(name); 
96     if(nf.Contains(".root") == kFALSE) nf += ".root";
97     TFile file(nf.Data(),opt);
98     TIter nextHist(mylist);
99     TObject* objHist=0;
100     int nh=0;
101     if(kSingleKey) {
102        file.cd();
103        mylist->Write(mylist->GetName(),TObject::kSingleKey);
104        mylist->ls();
105        save = 1;
106     } else {
107       while((objHist=nextHist())) { // loop over list 
108         if(objHist->InheritsFrom("TH1")) {
109           TH1* hid = (TH1*)objHist;
110           file.cd();
111           hid->Write();
112           nh++;
113           printf("Save hist. %s \n",hid ->GetName());
114         }
115       }
116       printf("%i hists. save to file -> %s\n", nh,file.GetName());
117       if(nh>0) save = 1;
118     }
119     file.Close();
120   } else {
121     printf("AliEMCALHistoUtilities::SaveListOfHists : N O  S A V I N G \n");
122   }
123   return save;
124 }
125
126 void AliEMCALHistoUtilities::AddToNameAndTitle(TH1 *h, const char *name, const char *title)
127 {
128   if(h==0) return;
129   if(name  && strlen(name))  h->SetName(Form("%s%s",h->GetName(),name));
130   if(title && strlen(title)) h->SetTitle(Form("%s%s",h->GetTitle(),title));
131 }
132
133 void AliEMCALHistoUtilities::AddToNameAndTitleToList(TList *l, const char *name, const char *title)
134 {
135   if(l==0) return;
136   if(name || title) {
137     for(int i=0; i<l->GetSize(); i++) {
138       TObject *o = l->At(i);
139       if(o->InheritsFrom("TH1")) {
140         TH1 *h = (TH1*)o;
141         AddToNameAndTitle(h, name, title);
142       }
143     }
144   }
145 }
146
147 int AliEMCALHistoUtilities::ParseString(const TString &topt, TObjArray &Opt)
148
149   // Moved from AliEMCALGeometry
150   // Feb 06, 2006
151   Ssiz_t begin, index, end, end2;
152   begin = index = end = end2 = 0;
153   TRegexp separator("[^ ;,\\t\\s/]+");
154   while ( (begin < topt.Length()) && (index != kNPOS) ) {
155     // loop over given options
156     index = topt.Index(separator,&end,begin);
157     if (index >= 0 && end >= 1) {
158       TString substring(topt(index,end));
159       Opt.Add(new TObjString(substring.Data()));
160     }
161     begin += end+1;
162   }
163   return Opt.GetEntries();
164 }