]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EMCAL/AliEMCALHistoUtilities.cxx
Test Digit macro
[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 /*
17 $Log$
18 */
19
20 //*-- Authors: J.L. Klay (LLNL) & Aleksei Pavlinov (WSU) 
21
22 //*
23
24 #include <TBrowser.h>
25 #include <TFile.h>
26 #include <TList.h>
27 #include <TH1.h>
28 #include <TH2.h>
29 #include <TROOT.h>
30 #include <TString.h>
31
32 #include "AliEMCALHistoUtilities.h"
33
34 ClassImp(AliEMCALHistoUtilities)
35
36 AliEMCALHistoUtilities::AliEMCALHistoUtilities(const char *name, const char *tit) : TNamed(name,tit)
37 {
38         //constructor
39   fDebug = 0;
40   gROOT->cd();
41   fListHist = MoveHistsToList("Hist For AliEMCALHistoUtilities", kFALSE); 
42 }
43
44 AliEMCALHistoUtilities::~AliEMCALHistoUtilities()
45 {
46         //destructor
47 }  
48
49 void AliEMCALHistoUtilities::Browse(TBrowser* b) const 
50 {
51   // Browse
52    if(fListHist)  b->Add((TObject*)fListHist);
53    //   TObject::Browse(b);
54 }
55
56 Bool_t  AliEMCALHistoUtilities::IsFolder() const
57 {
58   // Is folder
59   if(fListHist) return kTRUE;
60   else                   return kFALSE;
61 }
62
63 TList* AliEMCALHistoUtilities::MoveHistsToList(const char* name, Bool_t putToBrowser)
64 {
65   // Move HIST to list
66   gROOT->cd();
67   TIter nextHist(gDirectory->GetList());
68   TList *list = new TList;
69   list->SetName(name);
70   TObject *objHist;
71   while((objHist=nextHist())){
72     if (!objHist->InheritsFrom("TH1")) continue;
73     ((TH1*)objHist)->SetDirectory(0); // Remove from gROOT
74     list->Add(objHist);
75   }
76   if(putToBrowser) gROOT->GetListOfBrowsables()->Add((TObject*)list);
77   return list;
78 }
79
80 void AliEMCALHistoUtilities::FillH1(TList *l, Int_t ind, Double_t x, Double_t w)
81 {
82   static TH1* hid=0;
83   if(l == 0) return;
84   if(ind < l->GetSize()){
85     hid = (TH1*)l->At(ind);
86     hid->Fill(x,w);
87   }
88 }
89
90 void AliEMCALHistoUtilities::FillH2(TList *l, Int_t ind, Double_t x, Double_t y, Double_t w)
91 {
92   static TH2* hid=0;
93   if(l == 0) return;
94   if(ind < l->GetSize()){
95     hid = (TH2*)l->At(ind);
96     hid->Fill(x,y,w);
97   }
98 }
99
100 int AliEMCALHistoUtilities::SaveListOfHists(TList *list,const char* name,Bool_t kSingleKey,const char* opt)
101 {
102   printf(" Name of out file |%s|\n", name); 
103   int save = 0;
104   if(list && list->GetSize() && strlen(name)){
105     TString nf(name); 
106     if(nf.Contains(".root") == kFALSE) nf += ".root";
107     TFile file(nf.Data(),opt);
108     TIter nextHist(list);
109     TObject* objHist=0;
110     int nh=0;
111     if(kSingleKey) {
112        file.cd();
113        list->Write(list->GetName(),TObject::kSingleKey);
114        list->ls();
115        save = 1;
116     } else {
117       while((objHist=nextHist())) { // loop over list 
118         if(objHist->InheritsFrom("TH1")) {
119           TH1* hid = (TH1*)objHist;
120           file.cd();
121           hid->Write();
122           nh++;
123           printf("Save hist. %s \n",hid ->GetName());
124         }
125       }
126       printf("%i hists. save to file -> %s\n", nh,file.GetName());
127       if(nh>0) save = 1;
128     }
129     file.Close();
130   } else {
131     printf("AliEMCALHistoUtilities::SaveListOfHists : N O  S A V I N G \n");
132     if(list==0) printf("List of object 0 : %p \n", list);
133     else printf("Size of list %i \n", list->GetSize());
134   }
135   return save;
136 }