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