]> git.uio.no Git - u/mrichter/AliRoot.git/blob - JETAN/AliAnalysisTaskJets.cxx
Coverity fix.
[u/mrichter/AliRoot.git] / JETAN / AliAnalysisTaskJets.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, 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 #include <Riostream.h> 
18 #include <TROOT.h>
19 #include <TSystem.h>
20 #include <TInterpreter.h>
21 #include <TChain.h>
22 #include <TFile.h>
23 #include <TList.h>
24 #include <TH1.h>
25
26 #include "AliAnalysisTaskJets.h"
27 #include "AliAnalysisManager.h"
28 #include "AliJetFinder.h"
29 #include "AliJetHeader.h"
30 #include "AliJetReader.h"
31 #include "AliJetReaderHeader.h"
32 #include "AliJetHistos.h"
33 #include "AliESDEvent.h"
34 #include "AliESD.h"
35 #include "AliAODEvent.h"
36 #include "AliAODJetEventBackground.h"
37 #include "AliAODHandler.h"
38 #include "AliMCEventHandler.h"
39 #include "AliESDInputHandler.h"
40 #include "AliMCEvent.h"
41 #include "AliStack.h"
42
43
44 ClassImp(AliAnalysisTaskJets)
45
46 ////////////////////////////////////////////////////////////////////////
47
48 AliAnalysisTaskJets::AliAnalysisTaskJets():
49   AliAnalysisTaskSE(),
50   fConfigFile("ConfigJetAnalysis.C"),
51   fNonStdBranch(""), 
52   fNonStdFile(""),
53   fJetFinder(0x0),
54   fHistos(0x0),
55   fAODExtension(0x0),
56   fListOfHistos(0x0),
57   fChain(0x0),
58   fOpt(0),
59   fReadAODFromOutput(0),
60   fUseAODBackground(kFALSE),
61   fFilterPt(0.)
62 {
63   // Default constructor
64 }
65
66 AliAnalysisTaskJets::AliAnalysisTaskJets(const char* name):
67   AliAnalysisTaskSE(name),
68   fConfigFile("ConfigJetAnalysis.C"),
69   fNonStdBranch(""),
70   fNonStdFile(""),
71   fJetFinder(0x0),
72   fHistos(0x0),
73   fAODExtension(0x0),
74   fListOfHistos(0x0),
75   fChain(0x0),
76   fOpt(0),
77   fReadAODFromOutput(0),
78   fUseAODBackground(kFALSE),
79   fFilterPt(0.)
80 {
81   // Default constructor
82   DefineOutput(1, TList::Class());
83 }
84
85 AliAnalysisTaskJets::AliAnalysisTaskJets(const char* name, TChain* chain):
86   AliAnalysisTaskSE(name),
87   fConfigFile("ConfigJetAnalysis.C"),
88   fNonStdBranch(""),
89   fNonStdFile(""),
90   fJetFinder(0x0),
91   fHistos(0x0),
92   fAODExtension(0x0),
93   fListOfHistos(0x0),
94   fChain(chain),
95   fOpt(0),
96   fReadAODFromOutput(0),
97   fUseAODBackground(kFALSE),
98   fFilterPt(0.)
99 {
100   // Default constructor
101   DefineOutput(1, TList::Class());
102 }
103
104 //----------------------------------------------------------------
105 void AliAnalysisTaskJets::UserCreateOutputObjects()
106 {
107   // Create the output container
108   //
109   if (fDebug > 1) printf("AnalysisTaskJets::CreateOutPutData() \n");
110
111   AliJetHeader *fH = fJetFinder->GetHeader();
112
113   if(fNonStdBranch.Length()==0)
114     {
115       //  Connec default AOD to jet finder
116       // create a new branch for the background
117
118       if(fUseAODBackground){
119         if(!AODEvent()->FindListObject(AliAODJetEventBackground::StdBranchName())){
120           AliAODJetEventBackground* evBkg = new AliAODJetEventBackground();
121           evBkg->SetName(AliAODJetEventBackground::StdBranchName());
122           AddAODBranch("AliAODJetEventBackground",&evBkg);
123         }
124       }
125       fJetFinder->ConnectAOD(AODEvent());
126     }
127   else
128     {
129       // Create a new branch for jets...
130       // how is this reset? -> cleared in the UserExec....
131       // Can this be handled by the framework?
132       // here we can also have the case that the brnaches are written to a separate file
133
134       TClonesArray *tca = new TClonesArray("AliAODJet", 0);
135       tca->SetName(fNonStdBranch.Data());
136       AddAODBranch("TClonesArray",&tca,fNonStdFile.Data());
137       if(fUseAODBackground){
138         if(!AODEvent() || !AODEvent()->FindListObject(Form("%s_%s",AliAODJetEventBackground::StdBranchName(),fNonStdBranch.Data()))){
139           AliAODJetEventBackground* evBkg = new AliAODJetEventBackground();
140           evBkg->SetName(Form("%s_%s",AliAODJetEventBackground::StdBranchName(),fNonStdBranch.Data()));
141           AddAODBranch("AliAODJetEventBackground",&evBkg,fNonStdFile.Data());
142         }
143       }
144       if(fNonStdFile.Length()!=0){
145         // 
146         // case that we have an AOD extension we need to fetch the jets from the extended output
147         // we identifay the extension aod event by looking for the branchname
148         AliAODHandler *aodH = dynamic_cast<AliAODHandler*>(AliAnalysisManager::GetAnalysisManager()->GetOutputEventHandler());
149         fAODExtension = 0;
150         if(aodH){
151           TObjArray* extArray = aodH->GetExtensions();
152           if (extArray) {
153             TIter next(extArray);
154             while ((fAODExtension=(AliAODExtension*)next())){
155               TObject *obj = fAODExtension->GetAOD()->FindListObject(fNonStdBranch.Data());
156               if(fDebug>10){
157                 Printf("%s:%d Dumping..",(char*)__FILE__,__LINE__);
158                 fAODExtension->GetAOD()->Dump();
159               }
160               if(obj){
161               if(fDebug>1)Printf("AODExtension found for %s",fNonStdBranch.Data());
162               break;
163               }
164               fAODExtension = 0;
165             }
166           }
167         }
168         if(fAODExtension)fJetFinder->ConnectAODNonStd(fAODExtension->GetAOD(), fNonStdBranch.Data()); 
169       }
170       else{
171         if (fDebug > 1) printf("Connecting Non Std Branch AOD %p %s \n",AODEvent(),fNonStdBranch.Data());
172         fJetFinder->ConnectAODNonStd(AODEvent(), fNonStdBranch.Data()); 
173       }
174     }
175
176   // do not add the histograms in the directory
177   // all handled by the list
178   Bool_t oldStatus = TH1::AddDirectoryStatus();
179   TH1::AddDirectory(kFALSE);
180
181
182   // Histograms
183   fListOfHistos = new TList();
184   fHistos       = new AliJetHistos();
185   fHistos->AddHistosToList(fListOfHistos);
186   
187   // Add the JetFinderInformation to the Outputlist
188   
189   // Compose a characteristic output name
190   // with the name of the output branch
191   if(fH) {
192     if(fNonStdBranch.Length()==0) {
193       fH->SetName("AliJetHeader_jets");
194     }
195     else {
196       fH->SetName(Form("AliJetHeader_%s",fNonStdBranch.Data()));
197     }
198   }
199
200   TH1::AddDirectory(oldStatus);
201   
202   
203   if(!fAODExtension)OutputTree()->GetUserInfo()->Add(fH);
204   else fAODExtension->GetTree()->GetUserInfo()->Add(fH);
205 }
206
207 //----------------------------------------------------------------
208 void AliAnalysisTaskJets::Init()
209 {
210   // Initialization
211   if (fDebug > 1) printf("AnalysisTaskJets::Init() \n");
212   
213   // Call configuration file
214   if (fConfigFile.Length()) {
215     gROOT->LoadMacro(fConfigFile);
216     fJetFinder = (AliJetFinder*) gInterpreter->ProcessLine("ConfigJetAnalysis()");
217   }
218   AliJetReaderHeader *header = (AliJetReaderHeader*)fJetFinder->GetReader()->GetReaderHeader();
219   fOpt = header->GetDetector();
220
221   // Initialise Jet Analysis
222   if(fOpt == 0) fJetFinder->Init();
223   else fJetFinder->InitTask(fChain); // V2
224 }
225
226
227 //----------------------------------------------------------------
228 void AliAnalysisTaskJets::UserExec(Option_t */*option*/)
229 {
230   // Execute analysis for current event
231   //
232   // Fill control histos
233   TClonesArray* jarray = 0;
234   AliAODJetEventBackground*  evBkg = 0;
235
236   // only need this once
237   static AliAODHandler *aodH = dynamic_cast<AliAODHandler*>(AliAnalysisManager::GetAnalysisManager()->GetOutputEventHandler());
238
239   if(fNonStdBranch.Length()==0) {
240     jarray = AODEvent()->GetJets();
241     if(fUseAODBackground){
242       evBkg = (AliAODJetEventBackground*)(AODEvent()->FindListObject(AliAODJetEventBackground::StdBranchName()));
243       evBkg->Reset();
244     }
245   }
246   else {
247     if(AODEvent())jarray = (TClonesArray*)(AODEvent()->FindListObject(fNonStdBranch.Data()));
248     if(!jarray)jarray = (TClonesArray*)(fAODExtension->GetAOD()->FindListObject(fNonStdBranch.Data()));
249     if(jarray)jarray->Delete();    // this is our responsibility, clear before filling again
250     if(fUseAODBackground){
251       if(AODEvent())evBkg = (AliAODJetEventBackground*)(AODEvent()->FindListObject(Form("%s_%s",AliAODJetEventBackground::StdBranchName(),fNonStdBranch.Data())));
252       if(!evBkg)  evBkg = (AliAODJetEventBackground*)(fAODExtension->GetAOD()->FindListObject(Form("%s_%s",AliAODJetEventBackground::StdBranchName(),fNonStdBranch.Data())));
253       if(evBkg)evBkg->Reset();
254     }
255   }
256
257   if (dynamic_cast<AliAODEvent*>(InputEvent()) !=  0 && !fReadAODFromOutput) {
258 // AOD is input event..........................................V                                       
259       fJetFinder->GetReader()->SetInputEvent(InputEvent(), InputEvent(), MCEvent());
260   } else {
261 // AOD is read from output ....................................V      
262       fJetFinder->GetReader()->SetInputEvent(InputEvent(), AODEvent(), MCEvent());
263   }
264   
265   
266
267   if(fOpt==0) fJetFinder->ProcessEvent();
268   else fJetFinder->ProcessEvent2();    // V2
269  
270   // Fill control histos
271   if(jarray)fHistos->FillHistos(jarray);
272
273
274   if(jarray&&aodH&&fFilterPt>0){
275     if(jarray->GetEntries()>0){
276       AliAODJet *jet = (AliAODJet*)jarray->At(0);
277       if(jet->Pt()>fFilterPt){
278         //      aodH->EnableFillAODforEvent();
279         aodH->SetFillAOD(kTRUE);
280       }
281     }
282   }
283
284   // Post the data
285   PostData(1, fListOfHistos);
286   return;
287 }
288
289
290 //*************************************************************
291
292 void AliAnalysisTaskJets::Terminate(Option_t */*option*/)
293 {
294 // Terminate analysis
295 //
296     if (fDebug > 1) printf("AnalysisJets: Terminate() \n");
297 //    if (fJetFinder) fJetFinder->FinishRun();
298 }
299