]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/AliAODHandler.cxx
Do not reset a zero pointer to MC info
[u/mrichter/AliRoot.git] / STEER / AliAODHandler.cxx
CommitLineData
3fbf06a3 1
ec4af4c1 2/**************************************************************************
3 * Copyright(c) 1998-2007, ALICE Experiment at CERN, All rights reserved. *
4 * *
5 * Author: The ALICE Off-line Project. *
6 * Contributors are mentioned in the code where appropriate. *
7 * *
8 * Permission to use, copy, modify and distribute this software and its *
9 * documentation strictly for non-commercial purposes is hereby granted *
10 * without fee, provided that the above copyright notice appears in all *
11 * copies and that both the copyright notice and this permission notice *
12 * appear in the supporting documentation. The authors make no claims *
13 * about the suitability of this software for any purpose. It is *
14 * provided "as is" without express or implied warranty. *
15 **************************************************************************/
16
17/* $Id$ */
18
19//-------------------------------------------------------------------------
20// Implementation of the Virtual Event Handler Interface for AOD
21// Author: Andreas Morsch, CERN
22//-------------------------------------------------------------------------
23
052994fb 24
ec4af4c1 25#include <TTree.h>
e910dd36 26#include <TFile.h>
7970f4ac 27#include <TString.h>
dce1b636 28#include <TList.h>
160959a9 29#include <TROOT.h>
e910dd36 30
da97a08a 31#include "AliLog.h"
ec4af4c1 32#include "AliAODHandler.h"
33#include "AliAODEvent.h"
da97a08a 34#include "AliAODTracklets.h"
35#include "AliStack.h"
36#include "AliAODMCParticle.h"
dce1b636 37#include "AliAODMCHeader.h"
da97a08a 38#include "AliMCEventHandler.h"
39#include "AliMCEvent.h"
dce1b636 40#include "AliGenEventHeader.h"
41#include "AliGenHijingEventHeader.h"
42#include "AliGenDPMjetEventHeader.h"
43#include "AliGenPythiaEventHeader.h"
44#include "AliGenCocktailEventHeader.h"
45
46
ec4af4c1 47
48ClassImp(AliAODHandler)
49
50//______________________________________________________________________________
51AliAODHandler::AliAODHandler() :
f3214a54 52 AliVEventHandler(),
78f7f935 53 fIsStandard(kTRUE),
da97a08a 54 fFillAOD(kTRUE),
41b01ae4 55 fFillAODRun(kTRUE),
7c3a9fbf 56 fNeedsHeaderReplication(kFALSE),
75754ba8 57 fNeedsTracksBranchReplication(kFALSE),
58 fNeedsVerticesBranchReplication(kFALSE),
59 fNeedsV0sBranchReplication(kFALSE),
e0107012 60 fNeedsCascadesBranchReplication(kFALSE),
75754ba8 61 fNeedsTrackletsBranchReplication(kFALSE),
62 fNeedsPMDClustersBranchReplication(kFALSE),
63 fNeedsJetsBranchReplication(kFALSE),
64 fNeedsFMDClustersBranchReplication(kFALSE),
65 fNeedsCaloClustersBranchReplication(kFALSE),
3549c522 66 fNeedsMCParticlesBranchReplication(kFALSE),
866d8d78 67 fNeedsDimuonsBranchReplication(kFALSE),
75754ba8 68 fAODIsReplicated(kFALSE),
ec4af4c1 69 fAODEvent(NULL),
da97a08a 70 fMCEventH(NULL),
e910dd36 71 fTreeA(NULL),
72 fFileA(NULL),
9066c676 73 fFileName(""),
582cfeb5 74 fExtensions(NULL),
75 fFilters(NULL)
ec4af4c1 76{
77 // default constructor
78}
79
80//______________________________________________________________________________
81AliAODHandler::AliAODHandler(const char* name, const char* title):
f3214a54 82 AliVEventHandler(name, title),
78f7f935 83 fIsStandard(kTRUE),
da97a08a 84 fFillAOD(kTRUE),
41b01ae4 85 fFillAODRun(kTRUE),
7c3a9fbf 86 fNeedsHeaderReplication(kFALSE),
75754ba8 87 fNeedsTracksBranchReplication(kFALSE),
88 fNeedsVerticesBranchReplication(kFALSE),
89 fNeedsV0sBranchReplication(kFALSE),
e0107012 90 fNeedsCascadesBranchReplication(kFALSE),
75754ba8 91 fNeedsTrackletsBranchReplication(kFALSE),
92 fNeedsPMDClustersBranchReplication(kFALSE),
93 fNeedsJetsBranchReplication(kFALSE),
94 fNeedsFMDClustersBranchReplication(kFALSE),
95 fNeedsCaloClustersBranchReplication(kFALSE),
3549c522 96 fNeedsMCParticlesBranchReplication(kFALSE),
866d8d78 97 fNeedsDimuonsBranchReplication(kFALSE),
75754ba8 98 fAODIsReplicated(kFALSE),
ec4af4c1 99 fAODEvent(NULL),
da97a08a 100 fMCEventH(NULL),
e910dd36 101 fTreeA(NULL),
102 fFileA(NULL),
9066c676 103 fFileName(""),
582cfeb5 104 fExtensions(NULL),
105 fFilters(NULL)
ec4af4c1 106{
582cfeb5 107// Normal constructor.
ec4af4c1 108}
109
110//______________________________________________________________________________
111AliAODHandler::~AliAODHandler()
112{
9066c676 113 // Destructor.
48f1c230 114 if (fAODEvent) delete fAODEvent;
6989bff3 115 if(fFileA){
116 // is already handled in TerminateIO
117 fFileA->Close();
118 delete fFileA;
48f1c230 119 fTreeA = 0;
6989bff3 120 }
48f1c230 121 if (fTreeA) delete fTreeA;
122 if (fExtensions) {fExtensions->Delete(); delete fExtensions;}
123 if (fFilters) {fFilters->Delete(); delete fFilters;}
ec4af4c1 124}
125
7970f4ac 126//______________________________________________________________________________
300d5701 127Bool_t AliAODHandler::Init(Option_t* opt)
ec4af4c1 128{
6989bff3 129 // Initialize IO
130 //
131 // Create the AODevent object
e0107012 132
4e6d5854 133 Bool_t createStdAOD = fIsStandard || fFillAOD;
aa399a26 134 if(!fAODEvent && createStdAOD){
ec4af4c1 135 fAODEvent = new AliAODEvent();
78f7f935 136 if (fIsStandard) fAODEvent->CreateStdContent();
6989bff3 137 }
138 //
139 // File opening according to execution mode
7970f4ac 140 TString option(opt);
141 option.ToLower();
aa399a26 142 if (createStdAOD) {
143 TDirectory *owd = gDirectory;
144 if (option.Contains("proof")) {
145 // proof
146 // Merging via files. Need to access analysis manager via interpreter.
147 gROOT->ProcessLine(Form("AliAnalysisDataContainer *c_common_out = AliAnalysisManager::GetAnalysisManager()->GetCommonOutputContainer();"));
148 gROOT->ProcessLine(Form("AliAnalysisManager::GetAnalysisManager()->OpenProofFile(c_common_out, \"RECREATE\");"));
149 fFileA = gFile;
150 } else {
151 // local and grid
152 fFileA = new TFile(fFileName.Data(), "RECREATE");
153 }
154 CreateTree(1);
155 owd->cd();
156 }
9066c676 157 if (fExtensions) {
158 TIter next(fExtensions);
159 AliAODExtension *ext;
160 while ((ext=(AliAODExtension*)next())) ext->Init(option);
161 }
582cfeb5 162 if (fFilters) {
163 TIter nextf(fFilters);
164 AliAODExtension *filteredAOD;
165 while ((filteredAOD=(AliAODExtension*)nextf())) {
166 filteredAOD->SetEvent(fAODEvent);
167 filteredAOD->Init(option);
168 }
169 }
6989bff3 170 return kTRUE;
ec4af4c1 171}
172
9066c676 173//______________________________________________________________________________
da97a08a 174void AliAODHandler::StoreMCParticles(){
dce1b636 175
da97a08a 176 //
177 // Remap the labels from ESD stack and store
178 // the AODMCParticles, makes only sense if we have
179 // the mcparticles branch
180 // has to be done here since we cannot know in advance
181 // which particles are needed (e.g. by the tracks etc.)
182 //
183 // Particles have been selected by AliMCEventhanlder->SelectParticle()
184 // To use the MCEventhandler here we need to set it from the outside
185 // can vanish when Handler go to the ANALYSISalice library
dce1b636 186 //
187 // The Branch booking for mcParticles and mcHeader has to happen
188 // in an external task for now since the AODHandler does not have access
189 // the AnalysisManager. For the same reason the pointer t o the MCEventH
190 // has to passed to the AOD Handler by this task
191 // (doing this in the steering macro would not work on PROOF)
da97a08a 192
aa399a26 193 if (!fAODEvent) return;
da97a08a 194 TClonesArray *mcarray = (TClonesArray*)fAODEvent->FindListObject(AliAODMCParticle::StdBranchName());
195 if(!mcarray)return;
da97a08a 196
dce1b636 197 AliAODMCHeader *mcHeader = (AliAODMCHeader*)fAODEvent->FindListObject(AliAODMCHeader::StdBranchName());
198 if(!mcHeader)return;
199
da97a08a 200 // Get the MC Infos.. Handler needs to be set before
201 // while adding the branch
202 // This needs to be done, not to depend on the AnalysisManager
203
204 if(!fMCEventH)return;
205 if(!fMCEventH->MCEvent())return;
206 AliStack *pStack = fMCEventH->MCEvent()->Stack();
207 if(!pStack)return;
208
209 fMCEventH->CreateLabelMap();
210
dce1b636 211 //
212 // Get the Event Header
213 //
214
215 AliHeader* header = fMCEventH->MCEvent()->Header();
9efd8e10 216 // get the MC vertex
217 AliGenEventHeader* genHeader = 0;
218 if (header) genHeader = header->GenEventHeader();
219 if (genHeader) {
220 TArrayF vtxMC(3);
221 genHeader->PrimaryVertex(vtxMC);
222 mcHeader->SetVertex(vtxMC[0],vtxMC[1],vtxMC[2]);
223
224 // we search the MCEventHeaders first
225 // Two cases, cocktail or not...
226 AliGenCocktailEventHeader* genCocktailHeader = dynamic_cast<AliGenCocktailEventHeader*>(genHeader);
227 if(genCocktailHeader){
228 // we have a coktail header
229 mcHeader->AddGeneratorName(genHeader->GetName());
230 // Loop from the back so that the first one sets the process type
231 TList* headerList = genCocktailHeader->GetHeaders();
232 for(int i = headerList->GetEntries()-1;i>=0;--i){
233 AliGenEventHeader *headerEntry = dynamic_cast<AliGenEventHeader*>(headerList->At(i));
234 SetMCHeaderInfo(mcHeader,headerEntry);
235 }
236 }
237 else{
238 // No Cocktail just take the first one
239 SetMCHeaderInfo(mcHeader,genHeader);
240 }
dce1b636 241 }
9efd8e10 242
dce1b636 243
244
245
246
247 // Store the AliAODParticlesMC
93836e1b 248 AliMCEvent* mcEvent = fMCEventH->MCEvent();
249
250 Int_t np = mcEvent->GetNumberOfTracks();
251 Int_t nprim = mcEvent->GetNumberOfPrimaries();
da97a08a 252
253
254 Int_t j = 0;
255 TClonesArray& l = *mcarray;
256
93836e1b 257 for(int i = 0; i < np; ++i){
258 if(fMCEventH->IsParticleSelected(i)){
259 Int_t flag = 0;
6cd07d0a 260 AliMCParticle* mcpart = (AliMCParticle*) mcEvent->GetTrack(i);
93836e1b 261 if(i<nprim)flag |= AliAODMCParticle::kPrimary;
262
263 if(mcEvent->IsPhysicalPrimary(i))flag |= AliAODMCParticle::kPhysicalPrim;
264
265 if(fMCEventH->GetNewLabel(i)!=j){
266 AliError(Form("MISMATCH New label %d j: %d",fMCEventH->GetNewLabel(i),j));
da97a08a 267 }
da97a08a 268
93836e1b 269 AliAODMCParticle mcpart_tmp(mcpart,i,flag);
270
6326aeae 271 mcpart_tmp.SetStatus(mcpart->Particle()->GetStatusCode());
93836e1b 272 //
273 Int_t d0 = mcpart_tmp.GetDaughter(0);
274 Int_t d1 = mcpart_tmp.GetDaughter(1);
275 Int_t m = mcpart_tmp.GetMother();
276
277 // other than for the track labels, negative values mean
278 // no daughter/mother so preserve it
279
280 if(d0<0 && d1<0){
281 // no first daughter -> no second daughter
282 // nothing to be done
283 // second condition not needed just for sanity check at the end
284 mcpart_tmp.SetDaughter(0,d0);
285 mcpart_tmp.SetDaughter(1,d1);
286 } else if(d1 < 0 && d0 >= 0) {
287 // Only one daughter
288 // second condition not needed just for sanity check at the end
289 if(fMCEventH->IsParticleSelected(d0)){
290 mcpart_tmp.SetDaughter(0,fMCEventH->GetNewLabel(d0));
291 } else {
292 mcpart_tmp.SetDaughter(0,-1);
293 }
294 mcpart_tmp.SetDaughter(1,d1);
295 }
296 else if (d0 > 0 && d1 > 0 ){
297 // we have two or more daughters loop on the stack to see if they are
298 // selected
299 Int_t d0_tmp = -1;
300 Int_t d1_tmp = -1;
301 for(int id = d0; id<=d1;++id){
302 if(fMCEventH->IsParticleSelected(id)){
303 if(d0_tmp==-1){
304 // first time
305 d0_tmp = fMCEventH->GetNewLabel(id);
306 d1_tmp = d0_tmp; // this is to have the same schema as on the stack i.e. with one daugther d0 and d1 are the same
307 }
308 else d1_tmp = fMCEventH->GetNewLabel(id);
309 }
310 }
311 mcpart_tmp.SetDaughter(0,d0_tmp);
312 mcpart_tmp.SetDaughter(1,d1_tmp);
313 } else {
314 AliError(Form("Unxpected indices %d %d",d0,d1));
315 }
316
317 if(m<0){
318 mcpart_tmp.SetMother(m);
319 } else {
320 if(fMCEventH->IsParticleSelected(m))mcpart_tmp.SetMother(fMCEventH->GetNewLabel(m));
321 else AliError(Form("PROBLEM Mother not selected %d \n", m));
322 }
6326aeae 323
93836e1b 324 new (l[j++]) AliAODMCParticle(mcpart_tmp);
325
da97a08a 326 }
da97a08a 327 }
93836e1b 328 AliInfo(Form("AliAODHandler::StoreMCParticles: Selected %d (Primaries %d / total %d) after validation \n",
da97a08a 329 j,nprim,np));
330
331 // Set the labels in the AOD output...
332 // Remapping
333
334 // AODTracks
335 TClonesArray* tracks = fAODEvent->GetTracks();
336 if(tracks){
337 for(int it = 0; it < fAODEvent->GetNTracks();++it){
338 AliAODTrack *track = fAODEvent->GetTrack(it);
339
91dece68 340 Int_t sign = 1;
341 Int_t label = track->GetLabel();
342 if(label<0){ // preserve the sign for later usage
343 label *= -1;
344 sign = -1;
345 }
346
93836e1b 347 if (label >= AliMCEvent::BgLabelOffset()) label = mcEvent->BgLabelToIndex(label);
93836e1b 348 if(label > np || track->GetLabel() == 0){
349 AliWarning(Form("Wrong ESD track label %5d (%5d)",track->GetLabel(), label));
da97a08a 350 }
93836e1b 351 if(fMCEventH->GetNewLabel(label) == 0){
352 AliWarning(Form("New label not found for %5d (%5d)",track->GetLabel(), label));
da97a08a 353 }
91dece68 354 track->SetLabel(sign*fMCEventH->GetNewLabel(label));
da97a08a 355 }
356 }
357
358 // AOD calo cluster
359 TClonesArray *clusters = fAODEvent->GetCaloClusters();
360 if(clusters){
361 for (Int_t iClust = 0;iClust < fAODEvent->GetNCaloClusters(); ++iClust) {
362 AliAODCaloCluster * cluster = fAODEvent->GetCaloCluster(iClust);
363 UInt_t nLabel = cluster->GetNLabel();
364 // Ugly but do not want to fragment memory by creating
365 // new Int_t (nLabel)
366 Int_t* labels = const_cast<Int_t*>(cluster->GetLabels());
367 if (labels){
368 for(UInt_t i = 0;i < nLabel;++i){
369 labels[i] = fMCEventH->GetNewLabel(cluster->GetLabel(i));
370 }
371 }
372 // cluster->SetLabels(labels,nLabel);
373 }// iClust
374 }// clusters
375
376 // AOD tracklets
377 AliAODTracklets *tracklets = fAODEvent->GetTracklets();
378 if(tracklets){
379 for(int it = 0;it < tracklets->GetNumberOfTracklets();++it){
380 int label0 = tracklets->GetLabel(it,0);
381 int label1 = tracklets->GetLabel(it,1);
382 if(label0>=0)label0 = fMCEventH->GetNewLabel(label0);
383 if(label1>=0)label1 = fMCEventH->GetNewLabel(label1);
384 tracklets->SetLabel(it,0,label0);
385 tracklets->SetLabel(it,1,label1);
386 }
387 }
388
389}
390
9066c676 391//______________________________________________________________________________
5f380da9 392Bool_t AliAODHandler::FinishEvent()
ec4af4c1 393{
da97a08a 394 // Fill data structures
4e6d5854 395 if(fFillAOD && fFillAODRun && fAODEvent){
b8b7dd5c 396 fAODEvent->MakeEntriesReferencable();
e0107012 397 fTreeA->BranchRef();
b8b7dd5c 398 FillTree();
4e6d5854 399 }
f133e976 400
401 if (fFillAOD) {
b8b7dd5c 402 if (fExtensions) {
403 TIter next(fExtensions);
404 AliAODExtension *ext;
405 while ((ext=(AliAODExtension*)next())) ext->FinishEvent();
406 }
407 if (fFilters) {
408 TIter nextf(fFilters);
409 AliAODExtension *ext;
410 while ((ext=(AliAODExtension*)nextf())) {
411 ext->FinishEvent();
412 }
413 }
aa399a26 414 }
da97a08a 415 if (fIsStandard) fAODEvent->ResetStd();
e0107012 416 TClonesArray *mcarray = (TClonesArray*)fAODEvent->FindListObject(AliAODMCParticle::StdBranchName());
417 if(mcarray) mcarray->Delete();
418
f133e976 419 AliAODMCHeader *mcHeader = (AliAODMCHeader*)fAODEvent->FindListObject(AliAODMCHeader::StdBranchName());
849d80b1 420 if(mcHeader) mcHeader->Reset();
f133e976 421
da97a08a 422 // Reset AOD replication flag
423 fAODIsReplicated = kFALSE;
424 return kTRUE;
ec4af4c1 425}
426
7970f4ac 427//______________________________________________________________________________
ec4af4c1 428Bool_t AliAODHandler::Terminate()
429{
9066c676 430 // Terminate
431 AddAODtoTreeUserInfo();
432 if (fExtensions) {
433 TIter next(fExtensions);
434 AliAODExtension *ext;
435 while ((ext=(AliAODExtension*)next())) ext->GetTree()->GetUserInfo()->Add(ext->GetAOD());
436 }
437 return kTRUE;
ec4af4c1 438}
439
7970f4ac 440//______________________________________________________________________________
ec4af4c1 441Bool_t AliAODHandler::TerminateIO()
442{
9066c676 443 // Terminate IO
444 if (fFileA) {
aa399a26 445 fFileA->Write();
9066c676 446 fFileA->Close();
447 delete fFileA;
448 fFileA = 0;
48f1c230 449 // When closing the file, the tree is also deleted.
450 fTreeA = 0;
9066c676 451 }
452 if (fExtensions) {
453 TIter next(fExtensions);
454 AliAODExtension *ext;
455 while ((ext=(AliAODExtension*)next())) ext->TerminateIO();
456 }
582cfeb5 457 if (fFilters) {
458 TIter nextf(fFilters);
459 AliAODExtension *ext;
460 while ((ext=(AliAODExtension*)nextf())) ext->TerminateIO();
461 }
9066c676 462 return kTRUE;
ec4af4c1 463}
464
7970f4ac 465//______________________________________________________________________________
954526ed 466void AliAODHandler::CreateTree(Int_t flag)
ec4af4c1 467{
468 // Creates the AOD Tree
f3214a54 469 fTreeA = new TTree("aodTree", "AliAOD tree");
ec4af4c1 470 fTreeA->Branch(fAODEvent->GetList());
954526ed 471 if (flag == 0) fTreeA->SetDirectory(0);
ec4af4c1 472}
473
7970f4ac 474//______________________________________________________________________________
ec4af4c1 475void AliAODHandler::FillTree()
476{
866d8d78 477
ec4af4c1 478 // Fill the AOD Tree
e0107012 479 fTreeA->Fill();
ec4af4c1 480}
481
7970f4ac 482//______________________________________________________________________________
ec4af4c1 483void AliAODHandler::AddAODtoTreeUserInfo()
484{
dce1b636 485 // Add aod event to tree user info
aa399a26 486 if (fTreeA) fTreeA->GetUserInfo()->Add(fAODEvent);
48f1c230 487 // Now the tree owns our fAODEvent...
488 fAODEvent = 0;
ec4af4c1 489}
490e9023 490
7970f4ac 491//______________________________________________________________________________
9066c676 492void AliAODHandler::AddBranch(const char* cname, void* addobj, const char* filename)
490e9023 493{
9066c676 494 // Add a new branch to the aod. Added optional filename parameter if the
495 // branch should be written to a separate file.
496 if (strlen(filename)) {
497 AliAODExtension *ext = AddExtension(filename);
498 ext->AddBranch(cname, addobj);
499 return;
500 }
490e9023 501 TDirectory *owd = gDirectory;
502 if (fFileA) {
dce1b636 503 fFileA->cd();
490e9023 504 }
0134949d 505 char** apointer = (char**) addobj;
506 TObject* obj = (TObject*) *apointer;
dce1b636 507
0134949d 508 fAODEvent->AddObject(obj);
dce1b636 509
510 const Int_t kSplitlevel = 99; // default value in TTree::Branch()
511 const Int_t kBufsize = 32000; // default value in TTree::Branch()
512
513 if (!fTreeA->FindBranch(obj->GetName())) {
514 // Do the same as if we book via
515 // TTree::Branch(TCollection*)
516
517 fTreeA->Bronch(obj->GetName(), cname, fAODEvent->GetList()->GetObjectRef(obj),
518 kBufsize, kSplitlevel - 1);
dce1b636 519 }
490e9023 520 owd->cd();
521}
7970f4ac 522
9066c676 523//______________________________________________________________________________
524AliAODExtension *AliAODHandler::AddExtension(const char *filename, const char *title)
525{
526// Add an AOD extension with some branches in a different file.
527 TString fname(filename);
528 if (!fname.EndsWith(".root")) fname += ".root";
529 if (!fExtensions) {
530 fExtensions = new TObjArray();
531 fExtensions->SetOwner();
532 }
533 AliAODExtension *ext = (AliAODExtension*)fExtensions->FindObject(fname);
534 if (!ext) {
535 ext = new AliAODExtension(fname, title);
536 fExtensions->Add(ext);
537 }
538 return ext;
539}
582cfeb5 540
541//______________________________________________________________________________
542AliAODExtension *AliAODHandler::GetExtension(const char *filename) const
543{
544// Getter for AOD extensions via file name.
545 if (!fExtensions) return NULL;
546 return (AliAODExtension*)fExtensions->FindObject(filename);
547}
548
549//______________________________________________________________________________
550AliAODExtension *AliAODHandler::AddFilteredAOD(const char *filename, const char *filtername)
551{
552// Add an AOD extension that can write only AOD events that pass a user filter.
553 if (!fFilters) {
554 fFilters = new TObjArray();
555 fFilters->SetOwner();
556 }
557 AliAODExtension *filter = (AliAODExtension*)fFilters->FindObject(filename);
558 if (!filter) {
559 filter = new AliAODExtension(filename, filtername, kTRUE);
560 fFilters->Add(filter);
561 }
562 return filter;
563}
564
565//______________________________________________________________________________
566AliAODExtension *AliAODHandler::GetFilteredAOD(const char *filename) const
567{
568// Getter for AOD filters via file name.
569 if (!fFilters) return NULL;
570 return (AliAODExtension*)fFilters->FindObject(filename);
571}
9066c676 572
7970f4ac 573//______________________________________________________________________________
574void AliAODHandler::SetOutputFileName(const char* fname)
575{
576// Set file name.
577 fFileName = fname;
578}
579
580//______________________________________________________________________________
581const char *AliAODHandler::GetOutputFileName()
582{
583// Get file name.
584 return fFileName.Data();
585}
dce1b636 586
9066c676 587//______________________________________________________________________________
dce1b636 588void AliAODHandler::SetMCHeaderInfo(AliAODMCHeader *mcHeader,AliGenEventHeader *genHeader){
589
590
591 // Utility function to cover different cases for the AliGenEventHeader
592 // Needed since different ProcessType and ImpactParamter are not
593 // in the base class...
594 // We don't encode process types for event cocktails yet
9066c676 595 // could be done e.g. by adding offsets depnding on the generator
dce1b636 596
597 mcHeader->AddGeneratorName(genHeader->GetName());
dce1b636 598 if(!genHeader)return;
599 AliGenPythiaEventHeader *pythiaGenHeader = dynamic_cast<AliGenPythiaEventHeader*>(genHeader);
600 if (pythiaGenHeader) {
601 mcHeader->SetEventType(pythiaGenHeader->ProcessType());
0fd37a1f 602 mcHeader->SetPtHard(pythiaGenHeader->GetPtHard());
dce1b636 603 return;
604 }
605
606 AliGenDPMjetEventHeader* dpmJetGenHeader = dynamic_cast<AliGenDPMjetEventHeader*>(genHeader);
607
608 if (dpmJetGenHeader){
609 mcHeader->SetEventType(dpmJetGenHeader->ProcessType());
610 return;
611 }
612
613 AliGenHijingEventHeader* hijingGenHeader = dynamic_cast<AliGenHijingEventHeader*>(genHeader);
614 if(hijingGenHeader){
615 mcHeader->SetImpactParameter(hijingGenHeader->ImpactParameter());
616 return;
617 }
618
619 AliWarning(Form("MC Eventheader not known: %s",genHeader->GetName()));
620
621}
9066c676 622
623ClassImp(AliAODExtension)
624
625//-------------------------------------------------------------------------
626// Support class for AOD extensions. This is created by the user analysis
627// that requires a separate file for some AOD branches. The name of the
628// AliAODExtension object is the file name where the AOD branches will be
629// stored.
630//-------------------------------------------------------------------------
631
582cfeb5 632//______________________________________________________________________________
633AliAODExtension::AliAODExtension(const char* name, const char* title, Bool_t isfilter)
634 :TNamed(name,title),
635 fAODEvent(0),
636 fTreeE(0),
637 fFileE(0),
638 fNtotal(0),
639 fNpassed(0),
640 fSelected(kFALSE)
641{
642// Constructor.
48f1c230 643 if (isfilter) {
644 TObject::SetBit(kFilteredAOD);
645 printf("####### Added AOD filter %s\n", name);
646 } else printf("####### Added AOD extension %s\n", name);
582cfeb5 647}
648
9066c676 649//______________________________________________________________________________
650AliAODExtension::~AliAODExtension()
651{
652// Destructor.
9066c676 653 if(fFileE){
654 // is already handled in TerminateIO
655 fFileE->Close();
656 delete fFileE;
48f1c230 657 fTreeE = 0;
658 fAODEvent = 0;
9066c676 659 }
48f1c230 660 if (fTreeE) delete fTreeE;
9066c676 661}
662
663//______________________________________________________________________________
664void AliAODExtension::AddBranch(const char* cname, void* addobj)
665{
666 // Add a new branch to the aod
582cfeb5 667 if (IsFilteredAOD()) {
668 Error("AddBranch", "Not allowed to add branched to filtered AOD's.");
669 return;
670 }
d29168d6 671 if (!fAODEvent) {
672 char type[20];
673 gROOT->ProcessLine(Form("TString s_tmp; AliAnalysisManager::GetAnalysisManager()->GetAnalysisTypeString(s_tmp); sprintf((char*)0x%lx, \"%%s\", s_tmp.Data());", type));
674 Init(type);
675 }
9066c676 676 TDirectory *owd = gDirectory;
677 if (fFileE) {
678 fFileE->cd();
679 }
680 char** apointer = (char**) addobj;
681 TObject* obj = (TObject*) *apointer;
682
683 fAODEvent->AddObject(obj);
684
685 const Int_t kSplitlevel = 99; // default value in TTree::Branch()
686 const Int_t kBufsize = 32000; // default value in TTree::Branch()
687
688 if (!fTreeE->FindBranch(obj->GetName())) {
689 // Do the same as if we book via
690 // TTree::Branch(TCollection*)
9066c676 691 fTreeE->Bronch(obj->GetName(), cname, fAODEvent->GetList()->GetObjectRef(obj),
692 kBufsize, kSplitlevel - 1);
9066c676 693 }
694 owd->cd();
695}
696
582cfeb5 697//______________________________________________________________________________
698Bool_t AliAODExtension::FinishEvent()
699{
700// Fill current event.
701 fNtotal++;
702 if (!IsFilteredAOD()) {
703 fAODEvent->MakeEntriesReferencable();
704 fTreeE->Fill();
705 return kTRUE;
706 }
707 // Filtered AOD. Fill only if event is selected.
708 if (!fSelected) return kTRUE;
582cfeb5 709 fNpassed++;
710 fTreeE->Fill();
711 fSelected = kFALSE; // so that next event will not be selected unless demanded
712 return kTRUE;
713}
714
9066c676 715//______________________________________________________________________________
160959a9 716Bool_t AliAODExtension::Init(Option_t *option)
9066c676 717{
718// Initialize IO.
719 if(!fAODEvent) fAODEvent = new AliAODEvent();
720 TDirectory *owd = gDirectory;
160959a9 721 TString opt(option);
722 opt.ToLower();
723 if (opt.Contains("proof")) {
724 // proof
725 // Merging via files. Need to access analysis manager via interpreter.
20653795 726 gROOT->ProcessLine(Form("AliAnalysisManager::GetAnalysisManager()->OpenProofFile(\"%s\", \"RECREATE\");", fName.Data()));
160959a9 727 fFileE = gFile;
728 } else {
729 fFileE = new TFile(GetName(), "RECREATE");
730 }
9066c676 731 fTreeE = new TTree("aodTree", "AliAOD tree");
732 fTreeE->Branch(fAODEvent->GetList());
866d8d78 733 fTreeE->BranchRef();
9066c676 734 owd->cd();
735 return kTRUE;
736}
737
582cfeb5 738//______________________________________________________________________________
739void AliAODExtension::SetEvent(AliAODEvent *event)
740{
741// Connects to an external event
742 if (!IsFilteredAOD()) {
743 Error("SetEvent", "Not allowed to set external event for filtered AOD's");
744 return;
745 }
48f1c230 746 // Use the copy constructor or assignment operator to synchronize with external event.
747// AliAODEvent &other = *event;
748// if (!fAODEvent) fAODEvent = new AliAODEvent(other);
749// else if (fSelected) *fAODEvent = other;
582cfeb5 750 fAODEvent = event;
751}
752
9066c676 753//______________________________________________________________________________
754Bool_t AliAODExtension::TerminateIO()
755{
756 // Terminate IO
48f1c230 757 if (TObject::TestBit(kFilteredAOD))
758 printf("AOD Filter %s: events processed: %d passed: %d\n", GetName(), fNtotal, fNpassed);
759 else
760 printf("AOD extension %s: events processed: %d\n", GetName(), fNtotal);
9066c676 761 if (fFileE) {
762 fFileE->Write();
763 fFileE->Close();
764 delete fFileE;
765 fFileE = 0;
48f1c230 766 fTreeE = 0;
767 fAODEvent = 0;
9066c676 768 }
769 return kTRUE;
770}