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