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 "AliAODExtension.h"
35 #include "AliAODTracklets.h"
37 #include "AliAODMCParticle.h"
38 #include "AliAODMCHeader.h"
39 #include "AliMCEventHandler.h"
40 #include "AliMCEvent.h"
41 #include "AliGenEventHeader.h"
42 #include "AliGenHijingEventHeader.h"
43 #include "AliGenDPMjetEventHeader.h"
44 #include "AliGenPythiaEventHeader.h"
45 #include "AliGenCocktailEventHeader.h"
46 #include "AliCodeTimer.h"
47 #include "AliAODBranchReplicator.h"
48 #include "Riostream.h"
52 ClassImp(AliAODHandler)
54 //______________________________________________________________________________
55 AliAODHandler::AliAODHandler() :
60 fFillExtension(kTRUE),
61 fNeedsHeaderReplication(kFALSE),
62 fNeedsTOFHeaderReplication(kFALSE),
63 fNeedsVZEROReplication(kFALSE),
64 fNeedsTracksBranchReplication(kFALSE),
65 fNeedsVerticesBranchReplication(kFALSE),
66 fNeedsV0sBranchReplication(kFALSE),
67 fNeedsCascadesBranchReplication(kFALSE),
68 fNeedsTrackletsBranchReplication(kFALSE),
69 fNeedsPMDClustersBranchReplication(kFALSE),
70 fNeedsJetsBranchReplication(kFALSE),
71 fNeedsFMDClustersBranchReplication(kFALSE),
72 fNeedsCaloClustersBranchReplication(kFALSE),
73 fNeedsCaloTriggerBranchReplication(kFALSE),
74 fNeedsMCParticlesBranchReplication(kFALSE),
75 fNeedsDimuonsBranchReplication(kFALSE),
76 fNeedsHMPIDBranchReplication(kFALSE),
77 fAODIsReplicated(kFALSE),
86 // default constructor
89 //______________________________________________________________________________
90 AliAODHandler::AliAODHandler(const char* name, const char* title):
91 AliVEventHandler(name, title),
95 fFillExtension(kTRUE),
96 fNeedsHeaderReplication(kFALSE),
97 fNeedsTOFHeaderReplication(kFALSE),
98 fNeedsVZEROReplication(kFALSE),
99 fNeedsTracksBranchReplication(kFALSE),
100 fNeedsVerticesBranchReplication(kFALSE),
101 fNeedsV0sBranchReplication(kFALSE),
102 fNeedsCascadesBranchReplication(kFALSE),
103 fNeedsTrackletsBranchReplication(kFALSE),
104 fNeedsPMDClustersBranchReplication(kFALSE),
105 fNeedsJetsBranchReplication(kFALSE),
106 fNeedsFMDClustersBranchReplication(kFALSE),
107 fNeedsCaloClustersBranchReplication(kFALSE),
108 fNeedsCaloTriggerBranchReplication(kFALSE),
109 fNeedsMCParticlesBranchReplication(kFALSE),
110 fNeedsDimuonsBranchReplication(kFALSE),
111 fNeedsHMPIDBranchReplication(kFALSE),
112 fAODIsReplicated(kFALSE),
121 // Normal constructor.
124 //______________________________________________________________________________
125 AliAODHandler::~AliAODHandler()
131 if (fFileA) fFileA->Close();
139 //______________________________________________________________________________
140 Bool_t AliAODHandler::Init(Option_t* opt)
144 // Create the AODevent object
146 Bool_t createStdAOD = fIsStandard || fFillAOD;
147 if(!fAODEvent && createStdAOD){
148 fAODEvent = new AliAODEvent();
150 fAODEvent->CreateStdContent();
153 // File opening according to execution mode
157 TDirectory *owd = gDirectory;
158 if (option.Contains("proof")) {
160 // Merging via files. Need to access analysis manager via interpreter.
161 gROOT->ProcessLine(Form("AliAnalysisDataContainer *c_common_out = AliAnalysisManager::GetAnalysisManager()->GetCommonOutputContainer();"));
162 gROOT->ProcessLine(Form("AliAnalysisManager::GetAnalysisManager()->OpenProofFile(c_common_out, \"RECREATE\");"));
166 fFileA = new TFile(fFileName.Data(), "RECREATE");
172 TIter next(fExtensions);
173 AliAODExtension *ext;
174 while ((ext=(AliAODExtension*)next())) ext->Init(option);
177 TIter nextf(fFilters);
178 AliAODExtension *filteredAOD;
179 while ((filteredAOD=(AliAODExtension*)nextf())) {
180 filteredAOD->SetEvent(fAODEvent);
181 filteredAOD->Init(option);
188 //______________________________________________________________________________
189 void AliAODHandler::Print(Option_t* opt) const
191 // Print info about this object
193 cout << opt << Form("IsStandard %d filename=%s",fIsStandard,fFileName.Data()) << endl;
197 cout << opt << fExtensions->GetEntries() << " extensions :" << endl;
198 PrintExtensions(*fExtensions);
202 cout << opt << fFilters->GetEntries() << " filters :" << endl;
203 PrintExtensions(*fFilters);
207 //______________________________________________________________________________
208 void AliAODHandler::PrintExtensions(const TObjArray& array) const
210 // Show the list of aod extensions
212 AliAODExtension* ext(0x0);
213 while ( ( ext = static_cast<AliAODExtension*>(next()) ) )
219 //______________________________________________________________________________
220 void AliAODHandler::StoreMCParticles(){
223 // Remap the labels from ESD stack and store
224 // the AODMCParticles, makes only sense if we have
225 // the mcparticles branch
226 // has to be done here since we cannot know in advance
227 // which particles are needed (e.g. by the tracks etc.)
229 // Particles have been selected by AliMCEventhanlder->SelectParticle()
230 // To use the MCEventhandler here we need to set it from the outside
231 // can vanish when Handler go to the ANALYSISalice library
233 // The Branch booking for mcParticles and mcHeader has to happen
234 // in an external task for now since the AODHandler does not have access
235 // the AnalysisManager. For the same reason the pointer t o the MCEventH
236 // has to passed to the AOD Handler by this task
237 // (doing this in the steering macro would not work on PROOF)
239 if (!fAODEvent) return;
240 TClonesArray *mcarray = (TClonesArray*)fAODEvent->FindListObject(AliAODMCParticle::StdBranchName());
243 AliAODMCHeader *mcHeader = (AliAODMCHeader*)fAODEvent->FindListObject(AliAODMCHeader::StdBranchName());
246 // Get the MC Infos.. Handler needs to be set before
247 // while adding the branch
248 // This needs to be done, not to depend on the AnalysisManager
250 if(!fMCEventH)return;
251 if(!fMCEventH->MCEvent())return;
252 AliStack *pStack = fMCEventH->MCEvent()->Stack();
255 fMCEventH->CreateLabelMap();
258 // Get the Event Header
261 AliHeader* header = fMCEventH->MCEvent()->Header();
263 AliGenEventHeader* genHeader = 0;
264 if (header) genHeader = header->GenEventHeader();
267 genHeader->PrimaryVertex(vtxMC);
268 mcHeader->SetVertex(vtxMC[0],vtxMC[1],vtxMC[2]);
269 // we search the MCEventHeaders first
270 // Two cases, cocktail or not...
271 AliGenCocktailEventHeader* genCocktailHeader = dynamic_cast<AliGenCocktailEventHeader*>(genHeader);
272 if(genCocktailHeader){
273 // we have a coktail header add the name once
274 mcHeader->AddGeneratorName(genHeader->GetName());
275 TList* headerList = genCocktailHeader->GetHeaders();
276 // the first entry defines some extra general settings
277 AliGenEventHeader *headerEntry = dynamic_cast<AliGenEventHeader*>(headerList->At(0));
279 AliFatal("AliGenEventHeader entry not found in the header list");
281 SetMCHeaderInfo(mcHeader,headerEntry);
285 // No Cocktail just take the first one
286 SetMCHeaderInfo(mcHeader,genHeader);
288 // Add all the headers and names, if no cocktail header
289 // there will be only one entry
290 mcHeader->AddCocktailHeaders(genHeader);
297 // Store the AliAODParticlesMC
298 AliMCEvent* mcEvent = fMCEventH->MCEvent();
300 Int_t np = mcEvent->GetNumberOfTracks();
301 Int_t nprim = mcEvent->GetNumberOfPrimaries();
305 TClonesArray& l = *mcarray;
307 for(int i = 0; i < np; ++i){
308 if(fMCEventH->IsParticleSelected(i)){
310 AliMCParticle* mcpart = (AliMCParticle*) mcEvent->GetTrack(i);
311 if(i<nprim)flag |= AliAODMCParticle::kPrimary;
313 if(mcEvent->IsPhysicalPrimary(i))flag |= AliAODMCParticle::kPhysicalPrim;
314 if(mcEvent->IsSecondaryFromWeakDecay(i))flag |= AliAODMCParticle::kSecondaryFromWeakDecay;
315 if(mcEvent->IsSecondaryFromMaterial(i))flag |= AliAODMCParticle::kSecondaryFromMaterial;
317 if(fMCEventH->GetNewLabel(i)!=j){
318 AliError(Form("MISMATCH New label %d j: %d",fMCEventH->GetNewLabel(i),j));
321 AliAODMCParticle mcpartTmp(mcpart,i,flag);
323 mcpartTmp.SetStatus(mcpart->Particle()->GetStatusCode());
324 mcpartTmp.SetMCProcessCode(mcpart->Particle()->GetUniqueID());
326 Int_t d0 = mcpartTmp.GetDaughter(0);
327 Int_t d1 = mcpartTmp.GetDaughter(1);
328 Int_t m = mcpartTmp.GetMother();
330 // other than for the track labels, negative values mean
331 // no daughter/mother so preserve it
334 // no first daughter -> no second daughter
335 // nothing to be done
336 // second condition not needed just for sanity check at the end
337 mcpartTmp.SetDaughter(0,d0);
338 mcpartTmp.SetDaughter(1,d1);
339 } else if(d1 < 0 && d0 >= 0) {
341 // second condition not needed just for sanity check at the end
342 if(fMCEventH->IsParticleSelected(d0)){
343 mcpartTmp.SetDaughter(0,fMCEventH->GetNewLabel(d0));
345 mcpartTmp.SetDaughter(0,-1);
347 mcpartTmp.SetDaughter(1,d1);
349 else if (d0 > 0 && d1 > 0 ){
350 // we have two or more daughters loop on the stack to see if they are
354 for(int id = d0; id<=d1;++id){
355 if(fMCEventH->IsParticleSelected(id)){
358 d0Tmp = fMCEventH->GetNewLabel(id);
359 d1Tmp = d0Tmp; // this is to have the same schema as on the stack i.e. with one daugther d0 and d1 are the same
361 else d1Tmp = fMCEventH->GetNewLabel(id);
364 mcpartTmp.SetDaughter(0,d0Tmp);
365 mcpartTmp.SetDaughter(1,d1Tmp);
367 AliError(Form("Unxpected indices %d %d",d0,d1));
371 mcpartTmp.SetMother(m);
373 if(fMCEventH->IsParticleSelected(m))mcpartTmp.SetMother(fMCEventH->GetNewLabel(m));
374 else AliError(Form("PROBLEM Mother not selected %d \n", m));
377 new (l[j++]) AliAODMCParticle(mcpartTmp);
381 AliInfo(Form("AliAODHandler::StoreMCParticles: Selected %d (Primaries %d / total %d) after validation \n",
384 // Set the labels in the AOD output...
388 TClonesArray* tracks = fAODEvent->GetTracks();
390 for(int it = 0; it < fAODEvent->GetNTracks();++it){
391 AliAODTrack *track = fAODEvent->GetTrack(it);
394 Int_t label = track->GetLabel();
395 if(label<0){ // preserve the sign for later usage
400 if (label >= AliMCEvent::BgLabelOffset()) label = mcEvent->BgLabelToIndex(label);
401 if(label > np || track->GetLabel() == 0){
402 AliWarning(Form("Wrong ESD track label %5d (%5d)",track->GetLabel(), label));
404 if(fMCEventH->GetNewLabel(label) == 0){
405 AliWarning(Form("New label not found for %5d (%5d)",track->GetLabel(), label));
407 track->SetLabel(sign*fMCEventH->GetNewLabel(label));
412 TClonesArray *clusters = fAODEvent->GetCaloClusters();
414 for (Int_t iClust = 0;iClust < fAODEvent->GetNumberOfCaloClusters(); ++iClust) {
415 AliAODCaloCluster * cluster = fAODEvent->GetCaloCluster(iClust);
416 UInt_t nLabel = cluster->GetNLabels();
417 // Ugly but do not want to fragment memory by creating
418 // new Int_t (nLabel)
419 Int_t* labels = const_cast<Int_t*>(cluster->GetLabels());
421 for(UInt_t i = 0;i < nLabel;++i){
422 labels[i] = fMCEventH->GetNewLabel(cluster->GetLabelAt(i));
425 // cluster->SetLabels(labels,nLabel);
429 // AOD calo cells MC label re-index
430 Int_t iCell, nCell, cellMCLabel, cellMCLabelNew;;
432 Double_t cellE, cellT, cellEFrac;
433 AliAODCaloCells *cells;
436 cells = fAODEvent->GetEMCALCells();
438 nCell = cells->GetNumberOfCells() ;
439 for( iCell = 0; iCell < nCell; iCell++ ){
440 cells->GetCell( iCell, cellAbsId, cellE, cellT, cellMCLabel, cellEFrac );
441 // GetNewLabel returns 1 in case when -1 is supplied
442 if( cellMCLabel < 0 )
443 cellMCLabelNew = cellMCLabel;
445 cellMCLabelNew = fMCEventH->GetNewLabel( cellMCLabel );
447 cells->SetCell( iCell, cellAbsId, cellE, cellT, cellMCLabelNew, cellEFrac );
451 cells = fAODEvent->GetPHOSCells();
453 nCell = cells->GetNumberOfCells() ;
454 for( iCell = 0; iCell < nCell; iCell++ ){
455 cells->GetCell( iCell, cellAbsId, cellE, cellT, cellMCLabel, cellEFrac );
456 // GetNewLabel returns 1 in case when -1 is supplied
457 if( cellMCLabel < 0 )
458 cellMCLabelNew = cellMCLabel;
460 cellMCLabelNew = fMCEventH->GetNewLabel( cellMCLabel );
462 cells->SetCell( iCell, cellAbsId, cellE, cellT, cellMCLabelNew, cellEFrac );
467 AliAODTracklets *tracklets = fAODEvent->GetTracklets();
469 for(int it = 0;it < tracklets->GetNumberOfTracklets();++it){
470 int label0 = tracklets->GetLabel(it,0);
471 int label1 = tracklets->GetLabel(it,1);
472 if(label0>=0)label0 = fMCEventH->GetNewLabel(label0);
473 if(label1>=0)label1 = fMCEventH->GetNewLabel(label1);
474 tracklets->SetLabel(it,0,label0);
475 tracklets->SetLabel(it,1,label1);
481 //______________________________________________________________________________
482 Bool_t AliAODHandler::FinishEvent()
484 // Fill data structures
485 if(fFillAOD && fFillAODRun && fAODEvent){
486 fAODEvent->MakeEntriesReferencable();
491 if ((fFillAOD && fFillAODRun) || fFillExtension) {
492 if (fExtensions && fFillExtension) {
493 // fFillExtension can be set by the ESD filter or by a delta filter in case of AOD inputs
494 TIter next(fExtensions);
495 AliAODExtension *ext;
496 while ((ext=(AliAODExtension*)next())) ext->FinishEvent();
498 if (fFilters && fFillAOD && fFillAODRun) {
499 TIter nextf(fFilters);
500 AliAODExtension *ext;
501 while ((ext=(AliAODExtension*)nextf())) {
507 if (fIsStandard && fAODEvent)
509 fAODEvent->ResetStd();
514 TClonesArray *mcarray = static_cast<TClonesArray*>(fAODEvent->FindListObject(AliAODMCParticle::StdBranchName()));
515 if(mcarray) mcarray->Delete();
517 AliAODMCHeader *mcHeader = static_cast<AliAODMCHeader*>(fAODEvent->FindListObject(AliAODMCHeader::StdBranchName()));
518 if(mcHeader) mcHeader->Reset();
521 // Reset AOD replication flag
522 fAODIsReplicated = kFALSE;
526 //______________________________________________________________________________
527 Bool_t AliAODHandler::Terminate()
530 AddAODtoTreeUserInfo();
532 TIter nextF(fFilters);
533 AliAODExtension *ext;
534 while ((ext=static_cast<AliAODExtension*>(nextF())))
536 ext->AddAODtoTreeUserInfo();
539 TIter nextE(fExtensions);
540 while ((ext=static_cast<AliAODExtension*>(nextE())))
542 ext->AddAODtoTreeUserInfo();
548 //______________________________________________________________________________
549 Bool_t AliAODHandler::TerminateIO()
557 // When closing the file, the tree is also deleted.
561 TIter nextF(fFilters);
562 AliAODExtension *ext;
563 while ((ext=static_cast<AliAODExtension*>(nextF())))
568 TIter nextE(fExtensions);
569 while ((ext=static_cast<AliAODExtension*>(nextE())))
577 //______________________________________________________________________________
578 void AliAODHandler::CreateTree(Int_t flag)
580 // Creates the AOD Tree
581 fTreeA = new TTree("aodTree", "AliAOD tree");
582 fTreeA->Branch(fAODEvent->GetList());
583 if (flag == 0) fTreeA->SetDirectory(0);
586 //______________________________________________________________________________
587 void AliAODHandler::FillTree()
594 //______________________________________________________________________________
595 void AliAODHandler::AddAODtoTreeUserInfo()
597 // Add aod event to tree user info
598 if (fTreeA) fTreeA->GetUserInfo()->Add(fAODEvent);
599 // Now the tree owns our fAODEvent...
603 //______________________________________________________________________________
604 void AliAODHandler::AddBranch(const char* cname, void* addobj, const char* filename)
606 // Add a new branch to the aod. Added optional filename parameter if the
607 // branch should be written to a separate file.
609 if (strlen(filename))
611 AliAODExtension *ext = AddExtension(filename);
612 ext->AddBranch(cname, addobj);
616 // Add branch to all filters
617 // Add branch to all filters
619 TIter next(fFilters);
620 AliAODExtension *ext;
621 while ((ext=(AliAODExtension*)next())) ext->AddBranch(cname, addobj);
624 TDirectory *owd = gDirectory;
630 char** apointer = (char**) addobj;
631 TObject* obj = (TObject*) *apointer;
633 fAODEvent->AddObject(obj);
635 const Int_t kSplitlevel = 99; // default value in TTree::Branch()
636 const Int_t kBufsize = 32000; // default value in TTree::Branch()
638 if (!fTreeA->FindBranch(obj->GetName()))
640 // Do the same as if we book via
641 // TTree::Branch(TCollection*)
643 fTreeA->Bronch(obj->GetName(), cname, fAODEvent->GetList()->GetObjectRef(obj),
644 kBufsize, kSplitlevel - 1);
649 //______________________________________________________________________________
650 AliAODExtension *AliAODHandler::AddExtension(const char *filename, const char *title)
652 // Add an AOD extension with some branches in a different file.
654 TString fname(filename);
655 if (!fname.EndsWith(".root")) fname += ".root";
657 fExtensions = new TObjArray();
658 fExtensions->SetOwner();
660 AliAODExtension *ext = (AliAODExtension*)fExtensions->FindObject(fname);
662 ext = new AliAODExtension(fname, title);
663 fExtensions->Add(ext);
668 //______________________________________________________________________________
669 AliAODExtension *AliAODHandler::GetExtension(const char *filename) const
671 // Getter for AOD extensions via file name.
672 if (!fExtensions) return NULL;
673 return (AliAODExtension*)fExtensions->FindObject(filename);
676 //______________________________________________________________________________
677 AliAODExtension *AliAODHandler::AddFilteredAOD(const char *filename, const char *filtername)
679 // Add an AOD extension that can write only AOD events that pass a user filter.
681 fFilters = new TObjArray();
682 fFilters->SetOwner();
684 AliAODExtension *filter = (AliAODExtension*)fFilters->FindObject(filename);
686 filter = new AliAODExtension(filename, filtername, kTRUE);
687 fFilters->Add(filter);
692 //______________________________________________________________________________
693 AliAODExtension *AliAODHandler::GetFilteredAOD(const char *filename) const
695 // Getter for AOD filters via file name.
696 if (!fFilters) return NULL;
697 return (AliAODExtension*)fFilters->FindObject(filename);
700 //______________________________________________________________________________
701 void AliAODHandler::SetOutputFileName(const char* fname)
707 //______________________________________________________________________________
708 const char *AliAODHandler::GetOutputFileName() const
711 return fFileName.Data();
714 //______________________________________________________________________________
715 const char *AliAODHandler::GetExtraOutputs() const
717 // Get extra outputs as a string separated by commas.
718 static TString eoutputs;
722 TIter next1(fExtensions);
723 while ((obj=next1())) {
724 if (!eoutputs.IsNull()) eoutputs += ",";
725 eoutputs += obj->GetName();
729 TIter next2(fFilters);
730 while ((obj=next2())) {
731 if (!eoutputs.IsNull()) eoutputs += ",";
732 eoutputs += obj->GetName();
735 return eoutputs.Data();
738 //______________________________________________________________________________
739 Bool_t AliAODHandler::HasExtensions() const
741 // Whether or not we manage extensions
743 if ( fExtensions && fExtensions->GetEntries()>0 ) return kTRUE;
748 //______________________________________________________________________________
749 void AliAODHandler::SetMCHeaderInfo(AliAODMCHeader *mcHeader,AliGenEventHeader *genHeader){
752 // Utility function to cover different cases for the AliGenEventHeader
753 // Needed since different ProcessType and ImpactParamter are not
754 // in the base class...
756 if(!genHeader)return;
757 AliGenPythiaEventHeader *pythiaGenHeader = dynamic_cast<AliGenPythiaEventHeader*>(genHeader);
758 if (pythiaGenHeader) {
759 mcHeader->SetEventType(pythiaGenHeader->ProcessType());
760 mcHeader->SetPtHard(pythiaGenHeader->GetPtHard());
764 AliGenDPMjetEventHeader* dpmJetGenHeader = dynamic_cast<AliGenDPMjetEventHeader*>(genHeader);
766 if (dpmJetGenHeader){
767 mcHeader->SetEventType(dpmJetGenHeader->ProcessType());
771 AliGenHijingEventHeader* hijingGenHeader = dynamic_cast<AliGenHijingEventHeader*>(genHeader);
773 mcHeader->SetImpactParameter(hijingGenHeader->ImpactParameter());
777 // AliWarning(Form("MC Eventheader not known: %s",genHeader->GetName()));