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 fNeedsTracksBranchReplication(kFALSE),
63 fNeedsVerticesBranchReplication(kFALSE),
64 fNeedsV0sBranchReplication(kFALSE),
65 fNeedsCascadesBranchReplication(kFALSE),
66 fNeedsTrackletsBranchReplication(kFALSE),
67 fNeedsPMDClustersBranchReplication(kFALSE),
68 fNeedsJetsBranchReplication(kFALSE),
69 fNeedsFMDClustersBranchReplication(kFALSE),
70 fNeedsCaloClustersBranchReplication(kFALSE),
71 fNeedsCaloTriggerBranchReplication(kFALSE),
72 fNeedsMCParticlesBranchReplication(kFALSE),
73 fNeedsDimuonsBranchReplication(kFALSE),
74 fNeedsHMPIDBranchReplication(kFALSE),
75 fAODIsReplicated(kFALSE),
84 // default constructor
87 //______________________________________________________________________________
88 AliAODHandler::AliAODHandler(const char* name, const char* title):
89 AliVEventHandler(name, title),
93 fFillExtension(kTRUE),
94 fNeedsHeaderReplication(kFALSE),
95 fNeedsTracksBranchReplication(kFALSE),
96 fNeedsVerticesBranchReplication(kFALSE),
97 fNeedsV0sBranchReplication(kFALSE),
98 fNeedsCascadesBranchReplication(kFALSE),
99 fNeedsTrackletsBranchReplication(kFALSE),
100 fNeedsPMDClustersBranchReplication(kFALSE),
101 fNeedsJetsBranchReplication(kFALSE),
102 fNeedsFMDClustersBranchReplication(kFALSE),
103 fNeedsCaloClustersBranchReplication(kFALSE),
104 fNeedsCaloTriggerBranchReplication(kFALSE),
105 fNeedsMCParticlesBranchReplication(kFALSE),
106 fNeedsDimuonsBranchReplication(kFALSE),
107 fNeedsHMPIDBranchReplication(kFALSE),
108 fAODIsReplicated(kFALSE),
117 // Normal constructor.
120 //______________________________________________________________________________
121 AliAODHandler::~AliAODHandler()
127 if (fFileA) fFileA->Close();
135 //______________________________________________________________________________
136 Bool_t AliAODHandler::Init(Option_t* opt)
140 // Create the AODevent object
142 Bool_t createStdAOD = fIsStandard || fFillAOD;
143 if(!fAODEvent && createStdAOD){
144 fAODEvent = new AliAODEvent();
146 fAODEvent->CreateStdContent();
149 // File opening according to execution mode
153 TDirectory *owd = gDirectory;
154 if (option.Contains("proof")) {
156 // Merging via files. Need to access analysis manager via interpreter.
157 gROOT->ProcessLine(Form("AliAnalysisDataContainer *c_common_out = AliAnalysisManager::GetAnalysisManager()->GetCommonOutputContainer();"));
158 gROOT->ProcessLine(Form("AliAnalysisManager::GetAnalysisManager()->OpenProofFile(c_common_out, \"RECREATE\");"));
162 fFileA = new TFile(fFileName.Data(), "RECREATE");
168 TIter next(fExtensions);
169 AliAODExtension *ext;
170 while ((ext=(AliAODExtension*)next())) ext->Init(option);
173 TIter nextf(fFilters);
174 AliAODExtension *filteredAOD;
175 while ((filteredAOD=(AliAODExtension*)nextf())) {
176 filteredAOD->SetEvent(fAODEvent);
177 filteredAOD->Init(option);
184 //______________________________________________________________________________
185 void AliAODHandler::Print(Option_t* opt) const
187 // Print info about this object
189 cout << opt << Form("IsStandard %d filename=%s",fIsStandard,fFileName.Data()) << endl;
193 cout << opt << fExtensions->GetEntries() << " extensions :" << endl;
194 PrintExtensions(*fExtensions);
198 cout << opt << fFilters->GetEntries() << " filters :" << endl;
199 PrintExtensions(*fFilters);
203 //______________________________________________________________________________
204 void AliAODHandler::PrintExtensions(const TObjArray& array) const
206 // Show the list of aod extensions
208 AliAODExtension* ext(0x0);
209 while ( ( ext = static_cast<AliAODExtension*>(next()) ) )
215 //______________________________________________________________________________
216 void AliAODHandler::StoreMCParticles(){
219 // Remap the labels from ESD stack and store
220 // the AODMCParticles, makes only sense if we have
221 // the mcparticles branch
222 // has to be done here since we cannot know in advance
223 // which particles are needed (e.g. by the tracks etc.)
225 // Particles have been selected by AliMCEventhanlder->SelectParticle()
226 // To use the MCEventhandler here we need to set it from the outside
227 // can vanish when Handler go to the ANALYSISalice library
229 // The Branch booking for mcParticles and mcHeader has to happen
230 // in an external task for now since the AODHandler does not have access
231 // the AnalysisManager. For the same reason the pointer t o the MCEventH
232 // has to passed to the AOD Handler by this task
233 // (doing this in the steering macro would not work on PROOF)
235 if (!fAODEvent) return;
236 TClonesArray *mcarray = (TClonesArray*)fAODEvent->FindListObject(AliAODMCParticle::StdBranchName());
239 AliAODMCHeader *mcHeader = (AliAODMCHeader*)fAODEvent->FindListObject(AliAODMCHeader::StdBranchName());
242 // Get the MC Infos.. Handler needs to be set before
243 // while adding the branch
244 // This needs to be done, not to depend on the AnalysisManager
246 if(!fMCEventH)return;
247 if(!fMCEventH->MCEvent())return;
248 AliStack *pStack = fMCEventH->MCEvent()->Stack();
251 fMCEventH->CreateLabelMap();
254 // Get the Event Header
257 AliHeader* header = fMCEventH->MCEvent()->Header();
259 AliGenEventHeader* genHeader = 0;
260 if (header) genHeader = header->GenEventHeader();
263 genHeader->PrimaryVertex(vtxMC);
264 mcHeader->SetVertex(vtxMC[0],vtxMC[1],vtxMC[2]);
265 // we search the MCEventHeaders first
266 // Two cases, cocktail or not...
267 AliGenCocktailEventHeader* genCocktailHeader = dynamic_cast<AliGenCocktailEventHeader*>(genHeader);
268 if(genCocktailHeader){
269 // we have a coktail header add the name once
270 mcHeader->AddGeneratorName(genHeader->GetName());
271 TList* headerList = genCocktailHeader->GetHeaders();
272 // the first entry defines some extra general settings
273 AliGenEventHeader *headerEntry = dynamic_cast<AliGenEventHeader*>(headerList->At(0));
275 AliFatal("AliGenEventHeader entry not found in the header list");
277 SetMCHeaderInfo(mcHeader,headerEntry);
281 // No Cocktail just take the first one
282 SetMCHeaderInfo(mcHeader,genHeader);
284 // Add all the headers and names, if no cocktail header
285 // there will be only one entry
286 mcHeader->AddCocktailHeaders(genHeader);
293 // Store the AliAODParticlesMC
294 AliMCEvent* mcEvent = fMCEventH->MCEvent();
296 Int_t np = mcEvent->GetNumberOfTracks();
297 Int_t nprim = mcEvent->GetNumberOfPrimaries();
301 TClonesArray& l = *mcarray;
303 for(int i = 0; i < np; ++i){
304 if(fMCEventH->IsParticleSelected(i)){
306 AliMCParticle* mcpart = (AliMCParticle*) mcEvent->GetTrack(i);
307 if(i<nprim)flag |= AliAODMCParticle::kPrimary;
309 if(mcEvent->IsPhysicalPrimary(i))flag |= AliAODMCParticle::kPhysicalPrim;
310 if(mcEvent->IsSecondaryFromWeakDecay(i))flag |= AliAODMCParticle::kSecondaryFromWeakDecay;
311 if(mcEvent->IsSecondaryFromMaterial(i))flag |= AliAODMCParticle::kSecondaryFromMaterial;
313 if(fMCEventH->GetNewLabel(i)!=j){
314 AliError(Form("MISMATCH New label %d j: %d",fMCEventH->GetNewLabel(i),j));
317 AliAODMCParticle mcpartTmp(mcpart,i,flag);
319 mcpartTmp.SetStatus(mcpart->Particle()->GetStatusCode());
320 mcpartTmp.SetMCProcessCode(mcpart->Particle()->GetUniqueID());
322 Int_t d0 = mcpartTmp.GetDaughter(0);
323 Int_t d1 = mcpartTmp.GetDaughter(1);
324 Int_t m = mcpartTmp.GetMother();
326 // other than for the track labels, negative values mean
327 // no daughter/mother so preserve it
330 // no first daughter -> no second daughter
331 // nothing to be done
332 // second condition not needed just for sanity check at the end
333 mcpartTmp.SetDaughter(0,d0);
334 mcpartTmp.SetDaughter(1,d1);
335 } else if(d1 < 0 && d0 >= 0) {
337 // second condition not needed just for sanity check at the end
338 if(fMCEventH->IsParticleSelected(d0)){
339 mcpartTmp.SetDaughter(0,fMCEventH->GetNewLabel(d0));
341 mcpartTmp.SetDaughter(0,-1);
343 mcpartTmp.SetDaughter(1,d1);
345 else if (d0 > 0 && d1 > 0 ){
346 // we have two or more daughters loop on the stack to see if they are
350 for(int id = d0; id<=d1;++id){
351 if(fMCEventH->IsParticleSelected(id)){
354 d0Tmp = fMCEventH->GetNewLabel(id);
355 d1Tmp = d0Tmp; // this is to have the same schema as on the stack i.e. with one daugther d0 and d1 are the same
357 else d1Tmp = fMCEventH->GetNewLabel(id);
360 mcpartTmp.SetDaughter(0,d0Tmp);
361 mcpartTmp.SetDaughter(1,d1Tmp);
363 AliError(Form("Unxpected indices %d %d",d0,d1));
367 mcpartTmp.SetMother(m);
369 if(fMCEventH->IsParticleSelected(m))mcpartTmp.SetMother(fMCEventH->GetNewLabel(m));
370 else AliError(Form("PROBLEM Mother not selected %d \n", m));
373 new (l[j++]) AliAODMCParticle(mcpartTmp);
377 AliInfo(Form("AliAODHandler::StoreMCParticles: Selected %d (Primaries %d / total %d) after validation \n",
380 // Set the labels in the AOD output...
384 TClonesArray* tracks = fAODEvent->GetTracks();
386 for(int it = 0; it < fAODEvent->GetNTracks();++it){
387 AliAODTrack *track = fAODEvent->GetTrack(it);
390 Int_t label = track->GetLabel();
391 if(label<0){ // preserve the sign for later usage
396 if (label >= AliMCEvent::BgLabelOffset()) label = mcEvent->BgLabelToIndex(label);
397 if(label > np || track->GetLabel() == 0){
398 AliWarning(Form("Wrong ESD track label %5d (%5d)",track->GetLabel(), label));
400 if(fMCEventH->GetNewLabel(label) == 0){
401 AliWarning(Form("New label not found for %5d (%5d)",track->GetLabel(), label));
403 track->SetLabel(sign*fMCEventH->GetNewLabel(label));
408 TClonesArray *clusters = fAODEvent->GetCaloClusters();
410 for (Int_t iClust = 0;iClust < fAODEvent->GetNumberOfCaloClusters(); ++iClust) {
411 AliAODCaloCluster * cluster = fAODEvent->GetCaloCluster(iClust);
412 UInt_t nLabel = cluster->GetNLabels();
413 // Ugly but do not want to fragment memory by creating
414 // new Int_t (nLabel)
415 Int_t* labels = const_cast<Int_t*>(cluster->GetLabels());
417 for(UInt_t i = 0;i < nLabel;++i){
418 labels[i] = fMCEventH->GetNewLabel(cluster->GetLabelAt(i));
421 // cluster->SetLabels(labels,nLabel);
426 AliAODTracklets *tracklets = fAODEvent->GetTracklets();
428 for(int it = 0;it < tracklets->GetNumberOfTracklets();++it){
429 int label0 = tracklets->GetLabel(it,0);
430 int label1 = tracklets->GetLabel(it,1);
431 if(label0>=0)label0 = fMCEventH->GetNewLabel(label0);
432 if(label1>=0)label1 = fMCEventH->GetNewLabel(label1);
433 tracklets->SetLabel(it,0,label0);
434 tracklets->SetLabel(it,1,label1);
440 //______________________________________________________________________________
441 Bool_t AliAODHandler::FinishEvent()
443 // Fill data structures
444 if(fFillAOD && fFillAODRun && fAODEvent){
445 fAODEvent->MakeEntriesReferencable();
450 if ((fFillAOD && fFillAODRun) || fFillExtension) {
451 if (fExtensions && fFillExtension) {
452 // fFillExtension can be set by the ESD filter or by a delta filter in case of AOD inputs
453 TIter next(fExtensions);
454 AliAODExtension *ext;
455 while ((ext=(AliAODExtension*)next())) ext->FinishEvent();
457 if (fFilters && fFillAOD && fFillAODRun) {
458 TIter nextf(fFilters);
459 AliAODExtension *ext;
460 while ((ext=(AliAODExtension*)nextf())) {
466 if (fIsStandard && fAODEvent)
468 fAODEvent->ResetStd();
473 TClonesArray *mcarray = static_cast<TClonesArray*>(fAODEvent->FindListObject(AliAODMCParticle::StdBranchName()));
474 if(mcarray) mcarray->Delete();
476 AliAODMCHeader *mcHeader = static_cast<AliAODMCHeader*>(fAODEvent->FindListObject(AliAODMCHeader::StdBranchName()));
477 if(mcHeader) mcHeader->Reset();
480 // Reset AOD replication flag
481 fAODIsReplicated = kFALSE;
485 //______________________________________________________________________________
486 Bool_t AliAODHandler::Terminate()
489 AddAODtoTreeUserInfo();
491 TIter nextF(fFilters);
492 AliAODExtension *ext;
493 while ((ext=static_cast<AliAODExtension*>(nextF())))
495 ext->AddAODtoTreeUserInfo();
498 TIter nextE(fExtensions);
499 while ((ext=static_cast<AliAODExtension*>(nextE())))
501 ext->AddAODtoTreeUserInfo();
507 //______________________________________________________________________________
508 Bool_t AliAODHandler::TerminateIO()
516 // When closing the file, the tree is also deleted.
520 TIter nextF(fFilters);
521 AliAODExtension *ext;
522 while ((ext=static_cast<AliAODExtension*>(nextF())))
527 TIter nextE(fExtensions);
528 while ((ext=static_cast<AliAODExtension*>(nextE())))
536 //______________________________________________________________________________
537 void AliAODHandler::CreateTree(Int_t flag)
539 // Creates the AOD Tree
540 fTreeA = new TTree("aodTree", "AliAOD tree");
541 fTreeA->Branch(fAODEvent->GetList());
542 if (flag == 0) fTreeA->SetDirectory(0);
545 //______________________________________________________________________________
546 void AliAODHandler::FillTree()
553 //______________________________________________________________________________
554 void AliAODHandler::AddAODtoTreeUserInfo()
556 // Add aod event to tree user info
557 if (fTreeA) fTreeA->GetUserInfo()->Add(fAODEvent);
558 // Now the tree owns our fAODEvent...
562 //______________________________________________________________________________
563 void AliAODHandler::AddBranch(const char* cname, void* addobj, const char* filename)
565 // Add a new branch to the aod. Added optional filename parameter if the
566 // branch should be written to a separate file.
568 if (strlen(filename))
570 AliAODExtension *ext = AddExtension(filename);
571 ext->AddBranch(cname, addobj);
575 // Add branch to all filters
576 // Add branch to all filters
578 TIter next(fFilters);
579 AliAODExtension *ext;
580 while ((ext=(AliAODExtension*)next())) ext->AddBranch(cname, addobj);
583 TDirectory *owd = gDirectory;
589 char** apointer = (char**) addobj;
590 TObject* obj = (TObject*) *apointer;
592 fAODEvent->AddObject(obj);
594 const Int_t kSplitlevel = 99; // default value in TTree::Branch()
595 const Int_t kBufsize = 32000; // default value in TTree::Branch()
597 if (!fTreeA->FindBranch(obj->GetName()))
599 // Do the same as if we book via
600 // TTree::Branch(TCollection*)
602 fTreeA->Bronch(obj->GetName(), cname, fAODEvent->GetList()->GetObjectRef(obj),
603 kBufsize, kSplitlevel - 1);
608 //______________________________________________________________________________
609 AliAODExtension *AliAODHandler::AddExtension(const char *filename, const char *title)
611 // Add an AOD extension with some branches in a different file.
613 TString fname(filename);
614 if (!fname.EndsWith(".root")) fname += ".root";
616 fExtensions = new TObjArray();
617 fExtensions->SetOwner();
619 AliAODExtension *ext = (AliAODExtension*)fExtensions->FindObject(fname);
621 ext = new AliAODExtension(fname, title);
622 fExtensions->Add(ext);
627 //______________________________________________________________________________
628 AliAODExtension *AliAODHandler::GetExtension(const char *filename) const
630 // Getter for AOD extensions via file name.
631 if (!fExtensions) return NULL;
632 return (AliAODExtension*)fExtensions->FindObject(filename);
635 //______________________________________________________________________________
636 AliAODExtension *AliAODHandler::AddFilteredAOD(const char *filename, const char *filtername)
638 // Add an AOD extension that can write only AOD events that pass a user filter.
640 fFilters = new TObjArray();
641 fFilters->SetOwner();
643 AliAODExtension *filter = (AliAODExtension*)fFilters->FindObject(filename);
645 filter = new AliAODExtension(filename, filtername, kTRUE);
646 fFilters->Add(filter);
651 //______________________________________________________________________________
652 AliAODExtension *AliAODHandler::GetFilteredAOD(const char *filename) const
654 // Getter for AOD filters via file name.
655 if (!fFilters) return NULL;
656 return (AliAODExtension*)fFilters->FindObject(filename);
659 //______________________________________________________________________________
660 void AliAODHandler::SetOutputFileName(const char* fname)
666 //______________________________________________________________________________
667 const char *AliAODHandler::GetOutputFileName()
670 return fFileName.Data();
673 //______________________________________________________________________________
674 const char *AliAODHandler::GetExtraOutputs() const
676 // Get extra outputs as a string separated by commas.
677 static TString eoutputs;
681 TIter next1(fExtensions);
682 while ((obj=next1())) {
683 if (!eoutputs.IsNull()) eoutputs += ",";
684 eoutputs += obj->GetName();
688 TIter next2(fFilters);
689 while ((obj=next2())) {
690 if (!eoutputs.IsNull()) eoutputs += ",";
691 eoutputs += obj->GetName();
694 return eoutputs.Data();
697 //______________________________________________________________________________
698 Bool_t AliAODHandler::HasExtensions() const
700 // Whether or not we manage extensions
702 if ( fExtensions && fExtensions->GetEntries()>0 ) return kTRUE;
707 //______________________________________________________________________________
708 void AliAODHandler::SetMCHeaderInfo(AliAODMCHeader *mcHeader,AliGenEventHeader *genHeader){
711 // Utility function to cover different cases for the AliGenEventHeader
712 // Needed since different ProcessType and ImpactParamter are not
713 // in the base class...
715 if(!genHeader)return;
716 AliGenPythiaEventHeader *pythiaGenHeader = dynamic_cast<AliGenPythiaEventHeader*>(genHeader);
717 if (pythiaGenHeader) {
718 mcHeader->SetEventType(pythiaGenHeader->ProcessType());
719 mcHeader->SetPtHard(pythiaGenHeader->GetPtHard());
723 AliGenDPMjetEventHeader* dpmJetGenHeader = dynamic_cast<AliGenDPMjetEventHeader*>(genHeader);
725 if (dpmJetGenHeader){
726 mcHeader->SetEventType(dpmJetGenHeader->ProcessType());
730 AliGenHijingEventHeader* hijingGenHeader = dynamic_cast<AliGenHijingEventHeader*>(genHeader);
732 mcHeader->SetImpactParameter(hijingGenHeader->ImpactParameter());
736 // AliWarning(Form("MC Eventheader not known: %s",genHeader->GetName()));