2 /**************************************************************************
3 * Copyright(c) 1998-2007, ALICE Experiment at CERN, All rights reserved. *
5 * Author: The ALICE Off-line Project. *
6 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
19 //-------------------------------------------------------------------------
20 // Implementation of the Virtual Event Handler Interface for AOD
21 // Author: Andreas Morsch, CERN
22 //-------------------------------------------------------------------------
32 #include "AliAODHandler.h"
33 #include "AliAODEvent.h"
34 #include "AliAODTracklets.h"
36 #include "AliAODMCParticle.h"
37 #include "AliAODMCHeader.h"
38 #include "AliMCEventHandler.h"
39 #include "AliMCEvent.h"
40 #include "AliGenEventHeader.h"
41 #include "AliGenHijingEventHeader.h"
42 #include "AliGenDPMjetEventHeader.h"
43 #include "AliGenPythiaEventHeader.h"
44 #include "AliGenCocktailEventHeader.h"
45 #include "AliCodeTimer.h"
46 #include "AliAODBranchReplicator.h"
47 #include "Riostream.h"
49 ClassImp(AliAODHandler)
51 //______________________________________________________________________________
52 AliAODHandler::AliAODHandler() :
57 fFillExtension(kTRUE),
58 fNeedsHeaderReplication(kFALSE),
59 fNeedsTracksBranchReplication(kFALSE),
60 fNeedsVerticesBranchReplication(kFALSE),
61 fNeedsV0sBranchReplication(kFALSE),
62 fNeedsCascadesBranchReplication(kFALSE),
63 fNeedsTrackletsBranchReplication(kFALSE),
64 fNeedsPMDClustersBranchReplication(kFALSE),
65 fNeedsJetsBranchReplication(kFALSE),
66 fNeedsFMDClustersBranchReplication(kFALSE),
67 fNeedsCaloClustersBranchReplication(kFALSE),
68 fNeedsMCParticlesBranchReplication(kFALSE),
69 fNeedsDimuonsBranchReplication(kFALSE),
70 fAODIsReplicated(kFALSE),
79 // default constructor
82 //______________________________________________________________________________
83 AliAODHandler::AliAODHandler(const char* name, const char* title):
84 AliVEventHandler(name, title),
88 fFillExtension(kTRUE),
89 fNeedsHeaderReplication(kFALSE),
90 fNeedsTracksBranchReplication(kFALSE),
91 fNeedsVerticesBranchReplication(kFALSE),
92 fNeedsV0sBranchReplication(kFALSE),
93 fNeedsCascadesBranchReplication(kFALSE),
94 fNeedsTrackletsBranchReplication(kFALSE),
95 fNeedsPMDClustersBranchReplication(kFALSE),
96 fNeedsJetsBranchReplication(kFALSE),
97 fNeedsFMDClustersBranchReplication(kFALSE),
98 fNeedsCaloClustersBranchReplication(kFALSE),
99 fNeedsMCParticlesBranchReplication(kFALSE),
100 fNeedsDimuonsBranchReplication(kFALSE),
101 fAODIsReplicated(kFALSE),
110 // Normal constructor.
113 //______________________________________________________________________________
114 AliAODHandler::~AliAODHandler()
122 // is already handled in TerminateIO
132 //______________________________________________________________________________
133 Bool_t AliAODHandler::Init(Option_t* opt)
137 // Create the AODevent object
139 Bool_t createStdAOD = fIsStandard || fFillAOD;
140 if(!fAODEvent && createStdAOD){
141 fAODEvent = new AliAODEvent();
143 fAODEvent->CreateStdContent();
146 // File opening according to execution mode
150 TDirectory *owd = gDirectory;
151 if (option.Contains("proof")) {
153 // Merging via files. Need to access analysis manager via interpreter.
154 gROOT->ProcessLine(Form("AliAnalysisDataContainer *c_common_out = AliAnalysisManager::GetAnalysisManager()->GetCommonOutputContainer();"));
155 gROOT->ProcessLine(Form("AliAnalysisManager::GetAnalysisManager()->OpenProofFile(c_common_out, \"RECREATE\");"));
159 fFileA = new TFile(fFileName.Data(), "RECREATE");
165 TIter next(fExtensions);
166 AliAODExtension *ext;
167 while ((ext=(AliAODExtension*)next())) ext->Init(option);
170 TIter nextf(fFilters);
171 AliAODExtension *filteredAOD;
172 while ((filteredAOD=(AliAODExtension*)nextf())) {
173 filteredAOD->SetEvent(fAODEvent);
174 filteredAOD->Init(option);
181 //______________________________________________________________________________
182 void AliAODHandler::Print(Option_t* opt) const
184 // Print info about this object
186 cout << opt << Form("IsStandard %d filename=%s",fIsStandard,fFileName.Data()) << endl;
190 cout << opt << fExtensions->GetEntries() << " extensions :" << endl;
191 PrintExtensions(*fExtensions);
195 cout << opt << fFilters->GetEntries() << " filters :" << endl;
196 PrintExtensions(*fFilters);
200 //______________________________________________________________________________
201 void AliAODHandler::PrintExtensions(const TObjArray& array) const
203 // Show the list of aod extensions
205 AliAODExtension* ext(0x0);
206 while ( ( ext = static_cast<AliAODExtension*>(next()) ) )
212 //______________________________________________________________________________
213 void AliAODHandler::StoreMCParticles(){
216 // Remap the labels from ESD stack and store
217 // the AODMCParticles, makes only sense if we have
218 // the mcparticles branch
219 // has to be done here since we cannot know in advance
220 // which particles are needed (e.g. by the tracks etc.)
222 // Particles have been selected by AliMCEventhanlder->SelectParticle()
223 // To use the MCEventhandler here we need to set it from the outside
224 // can vanish when Handler go to the ANALYSISalice library
226 // The Branch booking for mcParticles and mcHeader has to happen
227 // in an external task for now since the AODHandler does not have access
228 // the AnalysisManager. For the same reason the pointer t o the MCEventH
229 // has to passed to the AOD Handler by this task
230 // (doing this in the steering macro would not work on PROOF)
232 if (!fAODEvent) return;
233 TClonesArray *mcarray = (TClonesArray*)fAODEvent->FindListObject(AliAODMCParticle::StdBranchName());
236 AliAODMCHeader *mcHeader = (AliAODMCHeader*)fAODEvent->FindListObject(AliAODMCHeader::StdBranchName());
239 // Get the MC Infos.. Handler needs to be set before
240 // while adding the branch
241 // This needs to be done, not to depend on the AnalysisManager
243 if(!fMCEventH)return;
244 if(!fMCEventH->MCEvent())return;
245 AliStack *pStack = fMCEventH->MCEvent()->Stack();
248 fMCEventH->CreateLabelMap();
251 // Get the Event Header
254 AliHeader* header = fMCEventH->MCEvent()->Header();
256 AliGenEventHeader* genHeader = 0;
257 if (header) genHeader = header->GenEventHeader();
260 genHeader->PrimaryVertex(vtxMC);
261 mcHeader->SetVertex(vtxMC[0],vtxMC[1],vtxMC[2]);
263 // we search the MCEventHeaders first
264 // Two cases, cocktail or not...
265 AliGenCocktailEventHeader* genCocktailHeader = dynamic_cast<AliGenCocktailEventHeader*>(genHeader);
266 if(genCocktailHeader){
267 // we have a coktail header
268 mcHeader->AddGeneratorName(genHeader->GetName());
269 // Loop from the back so that the first one sets the process type
270 TList* headerList = genCocktailHeader->GetHeaders();
271 for(int i = headerList->GetEntries()-1;i>=0;--i){
272 AliGenEventHeader *headerEntry = dynamic_cast<AliGenEventHeader*>(headerList->At(i));
273 SetMCHeaderInfo(mcHeader,headerEntry);
277 // No Cocktail just take the first one
278 SetMCHeaderInfo(mcHeader,genHeader);
286 // Store the AliAODParticlesMC
287 AliMCEvent* mcEvent = fMCEventH->MCEvent();
289 Int_t np = mcEvent->GetNumberOfTracks();
290 Int_t nprim = mcEvent->GetNumberOfPrimaries();
294 TClonesArray& l = *mcarray;
296 for(int i = 0; i < np; ++i){
297 if(fMCEventH->IsParticleSelected(i)){
299 AliMCParticle* mcpart = (AliMCParticle*) mcEvent->GetTrack(i);
300 if(i<nprim)flag |= AliAODMCParticle::kPrimary;
302 if(mcEvent->IsPhysicalPrimary(i))flag |= AliAODMCParticle::kPhysicalPrim;
304 if(fMCEventH->GetNewLabel(i)!=j){
305 AliError(Form("MISMATCH New label %d j: %d",fMCEventH->GetNewLabel(i),j));
308 AliAODMCParticle mcpart_tmp(mcpart,i,flag);
310 mcpart_tmp.SetStatus(mcpart->Particle()->GetStatusCode());
312 Int_t d0 = mcpart_tmp.GetDaughter(0);
313 Int_t d1 = mcpart_tmp.GetDaughter(1);
314 Int_t m = mcpart_tmp.GetMother();
316 // other than for the track labels, negative values mean
317 // no daughter/mother so preserve it
320 // no first daughter -> no second daughter
321 // nothing to be done
322 // second condition not needed just for sanity check at the end
323 mcpart_tmp.SetDaughter(0,d0);
324 mcpart_tmp.SetDaughter(1,d1);
325 } else if(d1 < 0 && d0 >= 0) {
327 // second condition not needed just for sanity check at the end
328 if(fMCEventH->IsParticleSelected(d0)){
329 mcpart_tmp.SetDaughter(0,fMCEventH->GetNewLabel(d0));
331 mcpart_tmp.SetDaughter(0,-1);
333 mcpart_tmp.SetDaughter(1,d1);
335 else if (d0 > 0 && d1 > 0 ){
336 // we have two or more daughters loop on the stack to see if they are
340 for(int id = d0; id<=d1;++id){
341 if(fMCEventH->IsParticleSelected(id)){
344 d0_tmp = fMCEventH->GetNewLabel(id);
345 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
347 else d1_tmp = fMCEventH->GetNewLabel(id);
350 mcpart_tmp.SetDaughter(0,d0_tmp);
351 mcpart_tmp.SetDaughter(1,d1_tmp);
353 AliError(Form("Unxpected indices %d %d",d0,d1));
357 mcpart_tmp.SetMother(m);
359 if(fMCEventH->IsParticleSelected(m))mcpart_tmp.SetMother(fMCEventH->GetNewLabel(m));
360 else AliError(Form("PROBLEM Mother not selected %d \n", m));
363 new (l[j++]) AliAODMCParticle(mcpart_tmp);
367 AliInfo(Form("AliAODHandler::StoreMCParticles: Selected %d (Primaries %d / total %d) after validation \n",
370 // Set the labels in the AOD output...
374 TClonesArray* tracks = fAODEvent->GetTracks();
376 for(int it = 0; it < fAODEvent->GetNTracks();++it){
377 AliAODTrack *track = fAODEvent->GetTrack(it);
380 Int_t label = track->GetLabel();
381 if(label<0){ // preserve the sign for later usage
386 if (label >= AliMCEvent::BgLabelOffset()) label = mcEvent->BgLabelToIndex(label);
387 if(label > np || track->GetLabel() == 0){
388 AliWarning(Form("Wrong ESD track label %5d (%5d)",track->GetLabel(), label));
390 if(fMCEventH->GetNewLabel(label) == 0){
391 AliWarning(Form("New label not found for %5d (%5d)",track->GetLabel(), label));
393 track->SetLabel(sign*fMCEventH->GetNewLabel(label));
398 TClonesArray *clusters = fAODEvent->GetCaloClusters();
400 for (Int_t iClust = 0;iClust < fAODEvent->GetNumberOfCaloClusters(); ++iClust) {
401 AliAODCaloCluster * cluster = fAODEvent->GetCaloCluster(iClust);
402 UInt_t nLabel = cluster->GetNLabels();
403 // Ugly but do not want to fragment memory by creating
404 // new Int_t (nLabel)
405 Int_t* labels = const_cast<Int_t*>(cluster->GetLabels());
407 for(UInt_t i = 0;i < nLabel;++i){
408 labels[i] = fMCEventH->GetNewLabel(cluster->GetLabelAt(i));
411 // cluster->SetLabels(labels,nLabel);
416 AliAODTracklets *tracklets = fAODEvent->GetTracklets();
418 for(int it = 0;it < tracklets->GetNumberOfTracklets();++it){
419 int label0 = tracklets->GetLabel(it,0);
420 int label1 = tracklets->GetLabel(it,1);
421 if(label0>=0)label0 = fMCEventH->GetNewLabel(label0);
422 if(label1>=0)label1 = fMCEventH->GetNewLabel(label1);
423 tracklets->SetLabel(it,0,label0);
424 tracklets->SetLabel(it,1,label1);
430 //______________________________________________________________________________
431 Bool_t AliAODHandler::FinishEvent()
433 // Fill data structures
435 if(fFillAOD && fFillAODRun && fAODEvent){
436 fAODEvent->MakeEntriesReferencable();
441 if ((fFillAOD && fFillAODRun) || fFillExtension) {
443 TIter next(fExtensions);
444 AliAODExtension *ext;
445 while ((ext=(AliAODExtension*)next())) ext->FinishEvent();
448 TIter nextf(fFilters);
449 AliAODExtension *ext;
450 while ((ext=(AliAODExtension*)nextf())) {
458 fAODEvent->ResetStd();
463 TClonesArray *mcarray = static_cast<TClonesArray*>(fAODEvent->FindListObject(AliAODMCParticle::StdBranchName()));
464 if(mcarray) mcarray->Delete();
466 AliAODMCHeader *mcHeader = static_cast<AliAODMCHeader*>(fAODEvent->FindListObject(AliAODMCHeader::StdBranchName()));
467 if(mcHeader) mcHeader->Reset();
470 // Reset AOD replication flag
471 fAODIsReplicated = kFALSE;
475 //______________________________________________________________________________
476 Bool_t AliAODHandler::Terminate()
479 AddAODtoTreeUserInfo();
481 TIter nextF(fFilters);
482 AliAODExtension *ext;
483 while ((ext=static_cast<AliAODExtension*>(nextF())))
485 ext->AddAODtoTreeUserInfo();
488 TIter nextE(fExtensions);
489 while ((ext=static_cast<AliAODExtension*>(nextE())))
491 ext->AddAODtoTreeUserInfo();
497 //______________________________________________________________________________
498 Bool_t AliAODHandler::TerminateIO()
506 // When closing the file, the tree is also deleted.
510 TIter nextF(fFilters);
511 AliAODExtension *ext;
512 while ((ext=static_cast<AliAODExtension*>(nextF())))
517 TIter nextE(fExtensions);
518 while ((ext=static_cast<AliAODExtension*>(nextE())))
526 //______________________________________________________________________________
527 void AliAODHandler::CreateTree(Int_t flag)
529 // Creates the AOD Tree
530 fTreeA = new TTree("aodTree", "AliAOD tree");
531 fTreeA->Branch(fAODEvent->GetList());
532 if (flag == 0) fTreeA->SetDirectory(0);
535 //______________________________________________________________________________
536 void AliAODHandler::FillTree()
543 //______________________________________________________________________________
544 void AliAODHandler::AddAODtoTreeUserInfo()
546 // Add aod event to tree user info
547 if (fTreeA) fTreeA->GetUserInfo()->Add(fAODEvent);
548 // Now the tree owns our fAODEvent...
552 //______________________________________________________________________________
553 void AliAODHandler::AddBranch(const char* cname, void* addobj, const char* filename)
555 // Add a new branch to the aod. Added optional filename parameter if the
556 // branch should be written to a separate file.
558 if (strlen(filename))
560 AliAODExtension *ext = AddExtension(filename);
561 ext->AddBranch(cname, addobj);
565 // Add branch to all filters
566 // Add branch to all filters
568 TIter next(fFilters);
569 AliAODExtension *ext;
570 while ((ext=(AliAODExtension*)next())) ext->AddBranch(cname, addobj);
573 TDirectory *owd = gDirectory;
579 char** apointer = (char**) addobj;
580 TObject* obj = (TObject*) *apointer;
582 fAODEvent->AddObject(obj);
584 const Int_t kSplitlevel = 99; // default value in TTree::Branch()
585 const Int_t kBufsize = 32000; // default value in TTree::Branch()
587 if (!fTreeA->FindBranch(obj->GetName()))
589 // Do the same as if we book via
590 // TTree::Branch(TCollection*)
592 fTreeA->Bronch(obj->GetName(), cname, fAODEvent->GetList()->GetObjectRef(obj),
593 kBufsize, kSplitlevel - 1);
598 //______________________________________________________________________________
599 AliAODExtension *AliAODHandler::AddExtension(const char *filename, const char *title)
601 // Add an AOD extension with some branches in a different file.
603 TString fname(filename);
604 if (!fname.EndsWith(".root")) fname += ".root";
606 fExtensions = new TObjArray();
607 fExtensions->SetOwner();
609 AliAODExtension *ext = (AliAODExtension*)fExtensions->FindObject(fname);
611 ext = new AliAODExtension(fname, title);
612 fExtensions->Add(ext);
617 //______________________________________________________________________________
618 AliAODExtension *AliAODHandler::GetExtension(const char *filename) const
620 // Getter for AOD extensions via file name.
621 if (!fExtensions) return NULL;
622 return (AliAODExtension*)fExtensions->FindObject(filename);
625 //______________________________________________________________________________
626 AliAODExtension *AliAODHandler::AddFilteredAOD(const char *filename, const char *filtername)
628 // Add an AOD extension that can write only AOD events that pass a user filter.
630 fFilters = new TObjArray();
631 fFilters->SetOwner();
633 AliAODExtension *filter = (AliAODExtension*)fFilters->FindObject(filename);
635 filter = new AliAODExtension(filename, filtername, kTRUE);
636 fFilters->Add(filter);
641 //______________________________________________________________________________
642 AliAODExtension *AliAODHandler::GetFilteredAOD(const char *filename) const
644 // Getter for AOD filters via file name.
645 if (!fFilters) return NULL;
646 return (AliAODExtension*)fFilters->FindObject(filename);
649 //______________________________________________________________________________
650 void AliAODHandler::SetOutputFileName(const char* fname)
656 //______________________________________________________________________________
657 const char *AliAODHandler::GetOutputFileName()
660 return fFileName.Data();
663 //______________________________________________________________________________
664 const char *AliAODHandler::GetExtraOutputs() const
666 // Get extra outputs as a string separated by commas.
667 static TString eoutputs;
671 TIter next1(fExtensions);
672 while ((obj=next1())) {
673 if (!eoutputs.IsNull()) eoutputs += ",";
674 eoutputs += obj->GetName();
678 TIter next2(fFilters);
679 while ((obj=next2())) {
680 if (!eoutputs.IsNull()) eoutputs += ",";
681 eoutputs += obj->GetName();
684 return eoutputs.Data();
687 //______________________________________________________________________________
688 Bool_t AliAODHandler::HasExtensions() const
690 // Whether or not we manage extensions
692 if ( fExtensions && fExtensions->GetEntries()>0 ) return kTRUE;
697 //______________________________________________________________________________
698 void AliAODHandler::SetMCHeaderInfo(AliAODMCHeader *mcHeader,AliGenEventHeader *genHeader){
701 // Utility function to cover different cases for the AliGenEventHeader
702 // Needed since different ProcessType and ImpactParamter are not
703 // in the base class...
704 // We don't encode process types for event cocktails yet
705 // could be done e.g. by adding offsets depnding on the generator
707 mcHeader->AddGeneratorName(genHeader->GetName());
708 if(!genHeader)return;
709 AliGenPythiaEventHeader *pythiaGenHeader = dynamic_cast<AliGenPythiaEventHeader*>(genHeader);
710 if (pythiaGenHeader) {
711 mcHeader->SetEventType(pythiaGenHeader->ProcessType());
712 mcHeader->SetPtHard(pythiaGenHeader->GetPtHard());
716 AliGenDPMjetEventHeader* dpmJetGenHeader = dynamic_cast<AliGenDPMjetEventHeader*>(genHeader);
718 if (dpmJetGenHeader){
719 mcHeader->SetEventType(dpmJetGenHeader->ProcessType());
723 AliGenHijingEventHeader* hijingGenHeader = dynamic_cast<AliGenHijingEventHeader*>(genHeader);
725 mcHeader->SetImpactParameter(hijingGenHeader->ImpactParameter());
729 AliWarning(Form("MC Eventheader not known: %s",genHeader->GetName()));