]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AOD/AliAODInputHandler.cxx
835ade29514a67067788ab3352ae2efe6fe9757e
[u/mrichter/AliRoot.git] / STEER / AOD / 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 #include "AliAODpidUtil.h"
35
36 ClassImp(AliAODInputHandler)
37
38 static Option_t *gAODDataType = "AOD";
39
40 //______________________________________________________________________________
41 AliAODInputHandler::AliAODInputHandler() :
42     AliInputEventHandler(),
43     fEvent(0),
44     fMCEvent(0),
45     fFriends(0),
46     fAODpidUtil(0x0),
47     fMergeEvents(kFALSE),
48     fMergeTracks(kTRUE), 
49     fMergeEMCALClusters(kTRUE), 
50     fMergePHOSClusters(kTRUE), 
51     fMergeEMCALCells(kTRUE), 
52         fMergePHOSCells(kTRUE), 
53         fMergeEMCALTrigger(kTRUE), 
54         fMergePHOSTrigger(kTRUE),
55     fFriendsConnected(kFALSE),
56     fFileToMerge(0),
57     fTreeToMerge(0),
58     fAODEventToMerge(0),
59     fMergeOffset(0)
60 {
61   // Default constructor
62   fHistStatistics[0] = fHistStatistics[1] = NULL;
63 }
64
65 //______________________________________________________________________________
66 AliAODInputHandler::AliAODInputHandler(const char* name, const char* title):
67   AliInputEventHandler(name, title),
68   fEvent(0),
69   fMCEvent(new AliMCEvent()),
70   fFriends(new TList()),
71   fAODpidUtil(0x0),
72   fMergeEvents(kFALSE),
73   fMergeTracks(kTRUE), 
74   fMergeEMCALClusters(kTRUE), 
75   fMergePHOSClusters(kTRUE), 
76   fMergeEMCALCells(kTRUE), 
77   fMergePHOSCells(kTRUE),
78   fMergeEMCALTrigger(kTRUE), 
79   fMergePHOSTrigger(kTRUE),
80   fFriendsConnected(kFALSE),
81   fFileToMerge(0),
82   fTreeToMerge(0),
83   fAODEventToMerge(0),
84   fMergeOffset(0)
85 {
86     // Constructor
87   fHistStatistics[0] = fHistStatistics[1] = NULL;
88 }
89
90 //______________________________________________________________________________
91 AliAODInputHandler::~AliAODInputHandler() 
92 {
93 // Destructor
94   if (fFriends) fFriends->Delete();
95   delete fFriends;
96   delete fHistStatistics[0];
97   delete fHistStatistics[1];
98   delete fAODpidUtil;
99 }
100
101 //______________________________________________________________________________
102 Bool_t AliAODInputHandler::Init(TTree* tree, Option_t* opt)
103 {
104     // Initialisation necessary for each new tree
105     fTree = tree;
106     if (!fTree) return kFALSE;
107     fTree->GetEntries();
108     ConnectFriends();
109
110     SwitchOffBranches();
111     SwitchOnBranches();
112     
113     // Get pointer to AOD event
114     if (!fEvent) fEvent = new AliAODEvent();
115     
116     fEvent->ReadFromTree(fTree);
117     
118     if (fMixingHandler) fMixingHandler->Init(tree, opt);
119     
120     return kTRUE;
121 }
122
123 //______________________________________________________________________________
124 Bool_t AliAODInputHandler::BeginEvent(Long64_t entry)
125 {
126     // Begin event
127     TClonesArray* mcParticles = (TClonesArray*) (fEvent->FindListObject("mcparticles"));
128     if (mcParticles) {
129        if (!fMCEvent) fMCEvent = new AliMCEvent();
130        fMCEvent->SetParticleArray(mcParticles);
131     }
132     // When merging, get current event number from GetReadEntry(), 
133     // entry gives the events in the current file
134     if (fTreeToMerge) fTreeToMerge->GetEntry(GetReadEntry() + fMergeOffset);
135   
136     if (fEventCuts)
137       fIsSelectedResult = fEventCuts->GetSelectionMask(fEvent);
138     else
139       fIsSelectedResult = fEvent->GetHeader()->GetOfflineTrigger();
140
141     if (fMixingHandler) fMixingHandler->BeginEvent(entry);
142     
143     return kTRUE;
144 }
145
146 //______________________________________________________________________________
147 Bool_t AliAODInputHandler::Notify(const char* path)
148 {
149   // Notifaction of directory change
150   if (fMixingHandler) fMixingHandler->Notify(path);
151   if (!fFriendsConnected) {
152       ConnectFriends();
153       fEvent->ReadFromTree(fTree, "reconnect");
154   }
155   fFriendsConnected = kFALSE;
156     
157   TTree *ttree = fTree->GetTree();
158   if (!ttree) ttree = fTree;
159   TString statFname(ttree->GetCurrentFile()->GetName());
160   AliInfo(Form("Moving to file %s", statFname.Data()));
161   Int_t indarchive = statFname.Index("#");
162   if (indarchive<0) {
163      statFname = gSystem->DirName(statFname);
164      statFname += "/";
165   } else {
166      statFname.Remove(indarchive+1);
167   }   
168   statFname += "EventStat_temp.root";
169   TFile *statFile = 0;
170   if (IsCheckStatistics()) statFile = TFile::Open(statFname, "READ");
171   if (statFile) {
172      TList *list = (TList*)statFile->Get("cstatsout");
173      if (list) {
174         AliVCuts *physSel = (AliVCuts*)list->At(0);
175         if (physSel) {
176            TH2F *hAll = dynamic_cast<TH2F*>(physSel->GetStatistics("ALL"));
177            TH2F *hBin0 = dynamic_cast<TH2F*>(physSel->GetStatistics("BIN0"));
178            if (fHistStatistics[0] && hAll) {
179               TList tmplist;
180               tmplist.Add(hAll);
181               fHistStatistics[0]->Merge(&tmplist);
182               tmplist.Clear();
183               tmplist.Add(hBin0);
184               if (fHistStatistics[1] && hBin0) fHistStatistics[1]->Merge(&tmplist);
185            } else {
186              if (hAll && hBin0) {
187                 fHistStatistics[0] = static_cast<TH2F*>(hAll->Clone());
188                 fHistStatistics[1] = static_cast<TH2F*>(hBin0->Clone());
189                 fHistStatistics[0]->SetDirectory(0);
190                 fHistStatistics[1]->SetDirectory(0);
191              }   
192            }   
193         }
194         delete list;
195      }
196      delete statFile;
197   }
198   return kTRUE;
199 }
200
201 //______________________________________________________________________________
202 Bool_t AliAODInputHandler::FinishEvent()
203 {
204   // Finish event
205   if (fMixingHandler) fMixingHandler->FinishEvent();
206   if (fEvent) fEvent->Reset();
207   return kTRUE;
208 }
209
210 //______________________________________________________________________________
211 void AliAODInputHandler::AddFriend(char* filename)
212 {
213     // Add a friend tree 
214     TNamed* obj = new TNamed(filename, filename);
215     if (!fFriends) fFriends = new TList();
216     fFriends->Add(obj);
217 }
218
219 //______________________________________________________________________________
220 Option_t *AliAODInputHandler::GetDataType() const
221 {
222 // Returns handled data type.
223    return gAODDataType;
224 }
225
226 //______________________________________________________________________________
227 TObject *AliAODInputHandler::GetStatistics(Option_t *option) const
228 {
229 // Get the statistics histogram(s) from the physics selection object. This
230 // should be called during FinishTaskOutput(). Option can be empty (default
231 // statistics histogram) or BIN0.
232    TString opt(option);
233    opt.ToUpper();
234    if (opt=="BIN0") return fHistStatistics[1];
235    return fHistStatistics[0];
236 }   
237
238 void AliAODInputHandler::ConnectFriends()
239 {
240     // Connect the friend trees 
241     if (!fFriends) return;
242     if (!fMergeEvents) {
243         TIter next(fFriends);
244         TNamed* obj;
245         TString aodTreeFName,aodFriendTreeFName;
246         TTree *ttree = fTree->GetTree();
247         if (!ttree) ttree = fTree;
248         aodTreeFName = ttree->GetCurrentFile()->GetName();
249         
250         while((obj = (TNamed*)next())) {
251             aodFriendTreeFName = aodTreeFName;
252             aodFriendTreeFName.ReplaceAll("AliAOD.root",obj->GetName());
253             aodFriendTreeFName.ReplaceAll("AliAODs.root",obj->GetName());
254             ttree->AddFriend("aodTree", aodFriendTreeFName.Data());
255         }
256     } else {
257         // Friends have to be merged
258         TNamed* filename = (TNamed*) (fFriends->At(0));
259         fFileToMerge = TFile::Open(filename->GetName());
260         if (fFileToMerge) {
261             fFileToMerge->GetObject("aodTree", fTreeToMerge);
262             if (!fAODEventToMerge) fAODEventToMerge = new AliAODEvent();
263             fAODEventToMerge->ReadFromTree(fTreeToMerge);
264         }
265     }
266     fFriendsConnected = kTRUE;
267 }
268
269 //______________________________________________________________________________
270 void AliAODInputHandler::CreatePIDResponse(Bool_t isMC/*=kFALSE*/)
271 {
272   //
273   // create the pid response object if it does not exist yet
274   //
275   if (fAODpidUtil) return;
276   fAODpidUtil=new AliAODpidUtil(isMC);
277   
278 }
279