Protected retrieval of statistics histograms from EventStat_temp.root when AOD is...
[u/mrichter/AliRoot.git] / STEER / AliAODInputHandler.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-2007, 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 //     Event handler for AOD input 
20 //     Author: Andreas Morsch, CERN
21 //-------------------------------------------------------------------------
22
23 #include <TSystem.h>
24 #include <TTree.h>
25 #include <TList.h>
26 #include <TNamed.h>
27 #include <TFile.h>
28 #include <TH2.h>
29
30 #include "AliAODInputHandler.h"
31 #include "AliAODEvent.h"
32 #include "AliVCuts.h"
33 #include "AliMCEvent.h"
34
35 ClassImp(AliAODInputHandler)
36
37 static Option_t *gAODDataType = "AOD";
38
39 //______________________________________________________________________________
40 AliAODInputHandler::AliAODInputHandler() :
41     AliInputEventHandler(),
42     fEvent(0),
43     fMCEvent(new AliMCEvent()),
44     fFriends(new TList()),
45     fMergeEvents(kFALSE),
46     fFriendsConnected(kFALSE),
47     fFileToMerge(0),
48     fTreeToMerge(0),
49     fAODEventToMerge(0),
50     fMergeOffset(0)
51 {
52   // Default constructor
53   fHistStatistics[0] = fHistStatistics[1] = NULL;
54 }
55
56 //______________________________________________________________________________
57 AliAODInputHandler::AliAODInputHandler(const char* name, const char* title):
58   AliInputEventHandler(name, title),
59   fEvent(0),
60   fMCEvent(new AliMCEvent()),
61   fFriends(new TList()),
62   fMergeEvents(kFALSE),
63   fFriendsConnected(kFALSE),
64   fFileToMerge(0),
65   fTreeToMerge(0),
66   fAODEventToMerge(0),
67   fMergeOffset(0)
68 {
69     // Constructor
70   fHistStatistics[0] = fHistStatistics[1] = NULL;
71 }
72
73 //______________________________________________________________________________
74 AliAODInputHandler::~AliAODInputHandler() 
75 {
76 // Destructor
77   fFriends->Delete();
78   if (fHistStatistics[0]) {
79     delete fHistStatistics[0];
80     fHistStatistics[0] = 0;
81   }
82   if (fHistStatistics[1]) {
83     delete fHistStatistics[1];
84     fHistStatistics[1] = 0;
85   }
86 }
87
88 //______________________________________________________________________________
89 Bool_t AliAODInputHandler::Init(TTree* tree, Option_t* opt)
90 {
91     // Initialisation necessary for each new tree
92     fTree = tree;
93     if (!fTree) return kFALSE;
94     fTree->GetEntries();
95     ConnectFriends();
96
97     SwitchOffBranches();
98     SwitchOnBranches();
99     
100     // Get pointer to AOD event
101     if (!fEvent) fEvent = new AliAODEvent();
102     
103     fEvent->ReadFromTree(fTree);
104     
105     if (fMixingHandler) fMixingHandler->Init(tree, opt);
106     
107     return kTRUE;
108 }
109
110 //______________________________________________________________________________
111 Bool_t AliAODInputHandler::BeginEvent(Long64_t entry)
112 {
113     // Begin event
114     TClonesArray* mcParticles = (TClonesArray*) (fEvent->FindListObject("mcparticles"));
115     if (mcParticles) fMCEvent->SetParticleArray(mcParticles);
116     if (fTreeToMerge) fTreeToMerge->GetEntry(entry + fMergeOffset);
117     
118     fIsSelectedResult = fEvent->GetHeader()->GetOfflineTrigger();
119
120     if (fMixingHandler) fMixingHandler->BeginEvent(entry);
121     
122     return kTRUE;
123 }
124
125 //______________________________________________________________________________
126 Bool_t AliAODInputHandler::Notify(const char* path)
127 {
128   // Notifaction of directory change
129   if (fMixingHandler) fMixingHandler->Notify(path);
130   if (!fFriendsConnected) {
131       ConnectFriends();
132       fEvent->ReadFromTree(fTree, "reconnect");
133   }
134   fFriendsConnected = kFALSE;
135     
136   TTree *ttree = fTree->GetTree();
137   if (!ttree) ttree = fTree;
138   TString statFname(ttree->GetCurrentFile()->GetName());
139   Int_t indarchive = statFname.Index("#");
140   if (indarchive<0) {
141      statFname = gSystem->DirName(statFname);
142      statFname += "/";
143   } else {
144      statFname.Remove(indarchive+1);
145   }   
146   statFname += "EventStat_temp.root";
147   TFile *statFile = 0;
148   if (IsCheckStatistics()) statFile = TFile::Open(statFname, "READ");
149   if (statFile) {
150      TList *list = (TList*)statFile->Get("cstatsout");
151      if (list) {
152         AliVCuts *physSel = (AliVCuts*)list->At(0);
153         if (physSel) {
154            TH2F *hAll = dynamic_cast<TH2F*>(physSel->GetStatistics("ALL"));
155            TH2F *hBin0 = dynamic_cast<TH2F*>(physSel->GetStatistics("BIN0"));
156            if (fHistStatistics[0] && hAll) {
157               TList tmplist;
158               tmplist.Add(hAll);
159               fHistStatistics[0]->Merge(&tmplist);
160               tmplist.Clear();
161               tmplist.Add(hBin0);
162               if (fHistStatistics[1] && hBin0) fHistStatistics[1]->Merge(&tmplist);
163            } else {
164              fHistStatistics[0] = static_cast<TH2F*>(hAll->Clone());
165              fHistStatistics[1] = static_cast<TH2F*>(hBin0->Clone());
166              fHistStatistics[0]->SetDirectory(0);
167              fHistStatistics[1]->SetDirectory(0);
168            }   
169         }
170         delete list;
171      }
172      delete statFile;
173   }
174   return kTRUE;
175 }
176
177 //______________________________________________________________________________
178 Bool_t AliAODInputHandler::FinishEvent()
179 {
180   // Finish event
181   if (fMixingHandler) fMixingHandler->FinishEvent();
182   return kTRUE;
183 }
184
185 //______________________________________________________________________________
186 void AliAODInputHandler::AddFriend(char* filename)
187 {
188     // Add a friend tree 
189     TNamed* obj = new TNamed(filename, filename);
190     fFriends->Add(obj);
191 }
192
193 //______________________________________________________________________________
194 Option_t *AliAODInputHandler::GetDataType() const
195 {
196 // Returns handled data type.
197    return gAODDataType;
198 }
199
200 //______________________________________________________________________________
201 TObject *AliAODInputHandler::GetStatistics(Option_t *option) const
202 {
203 // Get the statistics histogram(s) from the physics selection object. This
204 // should be called during FinishTaskOutput(). Option can be empty (default
205 // statistics histogram) or BIN0.
206    TString opt(option);
207    opt.ToUpper();
208    if (opt=="BIN0") return fHistStatistics[1];
209    return fHistStatistics[0];
210 }   
211
212 void AliAODInputHandler::ConnectFriends()
213 {
214     // Connect the friend trees 
215     if (!fMergeEvents) {
216         TIter next(fFriends);
217         TNamed* obj;
218         TString aodTreeFName,aodFriendTreeFName;
219         TTree *ttree = fTree->GetTree();
220         if (!ttree) ttree = fTree;
221         aodTreeFName = ttree->GetCurrentFile()->GetName();
222         
223         while((obj = (TNamed*)next())) {
224             aodFriendTreeFName = aodTreeFName;
225             aodFriendTreeFName.ReplaceAll("AliAOD.root",obj->GetName());
226             aodFriendTreeFName.ReplaceAll("AliAODs.root",obj->GetName());
227             ttree->AddFriend("aodTree", aodFriendTreeFName.Data());
228         }
229     } else {
230         // Friends have to be merged
231         TNamed* filename = (TNamed*) (fFriends->At(0));
232         fFileToMerge = new TFile(filename->GetName());
233         if (fFileToMerge) {
234             fFileToMerge->GetObject("aodTree", fTreeToMerge);
235             if (!fAODEventToMerge) fAODEventToMerge = new AliAODEvent();
236             fAODEventToMerge->ReadFromTree(fTreeToMerge);
237         }
238     }
239     fFriendsConnected = kTRUE;
240 }
241