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