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"
50 ClassImp(AliAODHandler)
52 //______________________________________________________________________________
53 AliAODHandler::AliAODHandler() :
58 fFillExtension(kTRUE),
59 fNeedsHeaderReplication(kFALSE),
60 fNeedsTracksBranchReplication(kFALSE),
61 fNeedsVerticesBranchReplication(kFALSE),
62 fNeedsV0sBranchReplication(kFALSE),
63 fNeedsCascadesBranchReplication(kFALSE),
64 fNeedsTrackletsBranchReplication(kFALSE),
65 fNeedsPMDClustersBranchReplication(kFALSE),
66 fNeedsJetsBranchReplication(kFALSE),
67 fNeedsFMDClustersBranchReplication(kFALSE),
68 fNeedsCaloClustersBranchReplication(kFALSE),
69 fNeedsMCParticlesBranchReplication(kFALSE),
70 fNeedsDimuonsBranchReplication(kFALSE),
71 fAODIsReplicated(kFALSE),
80 // default constructor
83 //______________________________________________________________________________
84 AliAODHandler::AliAODHandler(const char* name, const char* title):
85 AliVEventHandler(name, title),
89 fFillExtension(kTRUE),
90 fNeedsHeaderReplication(kFALSE),
91 fNeedsTracksBranchReplication(kFALSE),
92 fNeedsVerticesBranchReplication(kFALSE),
93 fNeedsV0sBranchReplication(kFALSE),
94 fNeedsCascadesBranchReplication(kFALSE),
95 fNeedsTrackletsBranchReplication(kFALSE),
96 fNeedsPMDClustersBranchReplication(kFALSE),
97 fNeedsJetsBranchReplication(kFALSE),
98 fNeedsFMDClustersBranchReplication(kFALSE),
99 fNeedsCaloClustersBranchReplication(kFALSE),
100 fNeedsMCParticlesBranchReplication(kFALSE),
101 fNeedsDimuonsBranchReplication(kFALSE),
102 fAODIsReplicated(kFALSE),
111 // Normal constructor.
114 //______________________________________________________________________________
115 AliAODHandler::~AliAODHandler()
123 // is already handled in TerminateIO
133 //______________________________________________________________________________
134 Bool_t AliAODHandler::Init(Option_t* opt)
138 // Create the AODevent object
140 Bool_t createStdAOD = fIsStandard || fFillAOD;
141 if(!fAODEvent && createStdAOD){
142 fAODEvent = new AliAODEvent();
144 fAODEvent->CreateStdContent();
147 // File opening according to execution mode
151 TDirectory *owd = gDirectory;
152 if (option.Contains("proof")) {
154 // Merging via files. Need to access analysis manager via interpreter.
155 gROOT->ProcessLine(Form("AliAnalysisDataContainer *c_common_out = AliAnalysisManager::GetAnalysisManager()->GetCommonOutputContainer();"));
156 gROOT->ProcessLine(Form("AliAnalysisManager::GetAnalysisManager()->OpenProofFile(c_common_out, \"RECREATE\");"));
160 fFileA = new TFile(fFileName.Data(), "RECREATE");
166 TIter next(fExtensions);
167 AliAODExtension *ext;
168 while ((ext=(AliAODExtension*)next())) ext->Init(option);
171 TIter nextf(fFilters);
172 AliAODExtension *filteredAOD;
173 while ((filteredAOD=(AliAODExtension*)nextf())) {
174 filteredAOD->SetEvent(fAODEvent);
175 filteredAOD->Init(option);
182 //______________________________________________________________________________
183 void AliAODHandler::Print(Option_t* opt) const
185 // Print info about this object
187 cout << opt << Form("IsStandard %d filename=%s",fIsStandard,fFileName.Data()) << endl;
191 cout << opt << fExtensions->GetEntries() << " extensions :" << endl;
192 PrintExtensions(*fExtensions);
196 cout << opt << fFilters->GetEntries() << " filters :" << endl;
197 PrintExtensions(*fFilters);
201 //______________________________________________________________________________
202 void AliAODHandler::PrintExtensions(const TObjArray& array) const
204 // Show the list of aod extensions
206 AliAODExtension* ext(0x0);
207 while ( ( ext = static_cast<AliAODExtension*>(next()) ) )
213 //______________________________________________________________________________
214 void AliAODHandler::StoreMCParticles(){
217 // Remap the labels from ESD stack and store
218 // the AODMCParticles, makes only sense if we have
219 // the mcparticles branch
220 // has to be done here since we cannot know in advance
221 // which particles are needed (e.g. by the tracks etc.)
223 // Particles have been selected by AliMCEventhanlder->SelectParticle()
224 // To use the MCEventhandler here we need to set it from the outside
225 // can vanish when Handler go to the ANALYSISalice library
227 // The Branch booking for mcParticles and mcHeader has to happen
228 // in an external task for now since the AODHandler does not have access
229 // the AnalysisManager. For the same reason the pointer t o the MCEventH
230 // has to passed to the AOD Handler by this task
231 // (doing this in the steering macro would not work on PROOF)
233 if (!fAODEvent) return;
234 TClonesArray *mcarray = (TClonesArray*)fAODEvent->FindListObject(AliAODMCParticle::StdBranchName());
237 AliAODMCHeader *mcHeader = (AliAODMCHeader*)fAODEvent->FindListObject(AliAODMCHeader::StdBranchName());
240 // Get the MC Infos.. Handler needs to be set before
241 // while adding the branch
242 // This needs to be done, not to depend on the AnalysisManager
244 if(!fMCEventH)return;
245 if(!fMCEventH->MCEvent())return;
246 AliStack *pStack = fMCEventH->MCEvent()->Stack();
249 fMCEventH->CreateLabelMap();
252 // Get the Event Header
255 AliHeader* header = fMCEventH->MCEvent()->Header();
257 AliGenEventHeader* genHeader = 0;
258 if (header) genHeader = header->GenEventHeader();
261 genHeader->PrimaryVertex(vtxMC);
262 mcHeader->SetVertex(vtxMC[0],vtxMC[1],vtxMC[2]);
264 // we search the MCEventHeaders first
265 // Two cases, cocktail or not...
266 AliGenCocktailEventHeader* genCocktailHeader = dynamic_cast<AliGenCocktailEventHeader*>(genHeader);
267 if(genCocktailHeader){
268 // we have a coktail header
269 mcHeader->AddGeneratorName(genHeader->GetName());
270 // Loop from the back so that the first one sets the process type
271 TList* headerList = genCocktailHeader->GetHeaders();
272 for(int i = headerList->GetEntries()-1;i>=0;--i){
273 AliGenEventHeader *headerEntry = dynamic_cast<AliGenEventHeader*>(headerList->At(i));
275 AliFatal("AliGenEventHeader entry not found in the header list");
277 SetMCHeaderInfo(mcHeader,headerEntry);
282 // No Cocktail just take the first one
283 SetMCHeaderInfo(mcHeader,genHeader);
291 // Store the AliAODParticlesMC
292 AliMCEvent* mcEvent = fMCEventH->MCEvent();
294 Int_t np = mcEvent->GetNumberOfTracks();
295 Int_t nprim = mcEvent->GetNumberOfPrimaries();
299 TClonesArray& l = *mcarray;
301 for(int i = 0; i < np; ++i){
302 if(fMCEventH->IsParticleSelected(i)){
304 AliMCParticle* mcpart = (AliMCParticle*) mcEvent->GetTrack(i);
305 if(i<nprim)flag |= AliAODMCParticle::kPrimary;
307 if(mcEvent->IsPhysicalPrimary(i))flag |= AliAODMCParticle::kPhysicalPrim;
309 if(fMCEventH->GetNewLabel(i)!=j){
310 AliError(Form("MISMATCH New label %d j: %d",fMCEventH->GetNewLabel(i),j));
313 AliAODMCParticle mcpart_tmp(mcpart,i,flag);
315 mcpart_tmp.SetStatus(mcpart->Particle()->GetStatusCode());
317 Int_t d0 = mcpart_tmp.GetDaughter(0);
318 Int_t d1 = mcpart_tmp.GetDaughter(1);
319 Int_t m = mcpart_tmp.GetMother();
321 // other than for the track labels, negative values mean
322 // no daughter/mother so preserve it
325 // no first daughter -> no second daughter
326 // nothing to be done
327 // second condition not needed just for sanity check at the end
328 mcpart_tmp.SetDaughter(0,d0);
329 mcpart_tmp.SetDaughter(1,d1);
330 } else if(d1 < 0 && d0 >= 0) {
332 // second condition not needed just for sanity check at the end
333 if(fMCEventH->IsParticleSelected(d0)){
334 mcpart_tmp.SetDaughter(0,fMCEventH->GetNewLabel(d0));
336 mcpart_tmp.SetDaughter(0,-1);
338 mcpart_tmp.SetDaughter(1,d1);
340 else if (d0 > 0 && d1 > 0 ){
341 // we have two or more daughters loop on the stack to see if they are
345 for(int id = d0; id<=d1;++id){
346 if(fMCEventH->IsParticleSelected(id)){
349 d0_tmp = fMCEventH->GetNewLabel(id);
350 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
352 else d1_tmp = fMCEventH->GetNewLabel(id);
355 mcpart_tmp.SetDaughter(0,d0_tmp);
356 mcpart_tmp.SetDaughter(1,d1_tmp);
358 AliError(Form("Unxpected indices %d %d",d0,d1));
362 mcpart_tmp.SetMother(m);
364 if(fMCEventH->IsParticleSelected(m))mcpart_tmp.SetMother(fMCEventH->GetNewLabel(m));
365 else AliError(Form("PROBLEM Mother not selected %d \n", m));
368 new (l[j++]) AliAODMCParticle(mcpart_tmp);
372 AliInfo(Form("AliAODHandler::StoreMCParticles: Selected %d (Primaries %d / total %d) after validation \n",
375 // Set the labels in the AOD output...
379 TClonesArray* tracks = fAODEvent->GetTracks();
381 for(int it = 0; it < fAODEvent->GetNTracks();++it){
382 AliAODTrack *track = fAODEvent->GetTrack(it);
385 Int_t label = track->GetLabel();
386 if(label<0){ // preserve the sign for later usage
391 if (label >= AliMCEvent::BgLabelOffset()) label = mcEvent->BgLabelToIndex(label);
392 if(label > np || track->GetLabel() == 0){
393 AliWarning(Form("Wrong ESD track label %5d (%5d)",track->GetLabel(), label));
395 if(fMCEventH->GetNewLabel(label) == 0){
396 AliWarning(Form("New label not found for %5d (%5d)",track->GetLabel(), label));
398 track->SetLabel(sign*fMCEventH->GetNewLabel(label));
403 TClonesArray *clusters = fAODEvent->GetCaloClusters();
405 for (Int_t iClust = 0;iClust < fAODEvent->GetNumberOfCaloClusters(); ++iClust) {
406 AliAODCaloCluster * cluster = fAODEvent->GetCaloCluster(iClust);
407 UInt_t nLabel = cluster->GetNLabels();
408 // Ugly but do not want to fragment memory by creating
409 // new Int_t (nLabel)
410 Int_t* labels = const_cast<Int_t*>(cluster->GetLabels());
412 for(UInt_t i = 0;i < nLabel;++i){
413 labels[i] = fMCEventH->GetNewLabel(cluster->GetLabelAt(i));
416 // cluster->SetLabels(labels,nLabel);
421 AliAODTracklets *tracklets = fAODEvent->GetTracklets();
423 for(int it = 0;it < tracklets->GetNumberOfTracklets();++it){
424 int label0 = tracklets->GetLabel(it,0);
425 int label1 = tracklets->GetLabel(it,1);
426 if(label0>=0)label0 = fMCEventH->GetNewLabel(label0);
427 if(label1>=0)label1 = fMCEventH->GetNewLabel(label1);
428 tracklets->SetLabel(it,0,label0);
429 tracklets->SetLabel(it,1,label1);
435 //______________________________________________________________________________
436 Bool_t AliAODHandler::FinishEvent()
438 // Fill data structures
439 if(fFillAOD && fFillAODRun && fAODEvent){
440 fAODEvent->MakeEntriesReferencable();
445 if ((fFillAOD && fFillAODRun) || fFillExtension) {
446 if (fExtensions && fFillExtension) {
447 // fFillExtension can be set by the ESD filter or by a delta filter in case of AOD inputs
448 TIter next(fExtensions);
449 AliAODExtension *ext;
450 while ((ext=(AliAODExtension*)next())) ext->FinishEvent();
452 if (fFilters && fFillAOD && fFillAODRun) {
453 TIter nextf(fFilters);
454 AliAODExtension *ext;
455 while ((ext=(AliAODExtension*)nextf())) {
461 if (fIsStandard && fAODEvent)
463 fAODEvent->ResetStd();
468 TClonesArray *mcarray = static_cast<TClonesArray*>(fAODEvent->FindListObject(AliAODMCParticle::StdBranchName()));
469 if(mcarray) mcarray->Delete();
471 AliAODMCHeader *mcHeader = static_cast<AliAODMCHeader*>(fAODEvent->FindListObject(AliAODMCHeader::StdBranchName()));
472 if(mcHeader) mcHeader->Reset();
475 // Reset AOD replication flag
476 fAODIsReplicated = kFALSE;
480 //______________________________________________________________________________
481 Bool_t AliAODHandler::Terminate()
484 AddAODtoTreeUserInfo();
486 TIter nextF(fFilters);
487 AliAODExtension *ext;
488 while ((ext=static_cast<AliAODExtension*>(nextF())))
490 ext->AddAODtoTreeUserInfo();
493 TIter nextE(fExtensions);
494 while ((ext=static_cast<AliAODExtension*>(nextE())))
496 ext->AddAODtoTreeUserInfo();
502 //______________________________________________________________________________
503 Bool_t AliAODHandler::TerminateIO()
511 // When closing the file, the tree is also deleted.
515 TIter nextF(fFilters);
516 AliAODExtension *ext;
517 while ((ext=static_cast<AliAODExtension*>(nextF())))
522 TIter nextE(fExtensions);
523 while ((ext=static_cast<AliAODExtension*>(nextE())))
531 //______________________________________________________________________________
532 void AliAODHandler::CreateTree(Int_t flag)
534 // Creates the AOD Tree
535 fTreeA = new TTree("aodTree", "AliAOD tree");
536 fTreeA->Branch(fAODEvent->GetList());
537 if (flag == 0) fTreeA->SetDirectory(0);
540 //______________________________________________________________________________
541 void AliAODHandler::FillTree()
548 //______________________________________________________________________________
549 void AliAODHandler::AddAODtoTreeUserInfo()
551 // Add aod event to tree user info
552 if (fTreeA) fTreeA->GetUserInfo()->Add(fAODEvent);
553 // Now the tree owns our fAODEvent...
557 //______________________________________________________________________________
558 void AliAODHandler::AddBranch(const char* cname, void* addobj, const char* filename)
560 // Add a new branch to the aod. Added optional filename parameter if the
561 // branch should be written to a separate file.
563 if (strlen(filename))
565 AliAODExtension *ext = AddExtension(filename);
566 ext->AddBranch(cname, addobj);
570 // Add branch to all filters
571 // Add branch to all filters
573 TIter next(fFilters);
574 AliAODExtension *ext;
575 while ((ext=(AliAODExtension*)next())) ext->AddBranch(cname, addobj);
578 TDirectory *owd = gDirectory;
584 char** apointer = (char**) addobj;
585 TObject* obj = (TObject*) *apointer;
587 fAODEvent->AddObject(obj);
589 const Int_t kSplitlevel = 99; // default value in TTree::Branch()
590 const Int_t kBufsize = 32000; // default value in TTree::Branch()
592 if (!fTreeA->FindBranch(obj->GetName()))
594 // Do the same as if we book via
595 // TTree::Branch(TCollection*)
597 fTreeA->Bronch(obj->GetName(), cname, fAODEvent->GetList()->GetObjectRef(obj),
598 kBufsize, kSplitlevel - 1);
603 //______________________________________________________________________________
604 AliAODExtension *AliAODHandler::AddExtension(const char *filename, const char *title)
606 // Add an AOD extension with some branches in a different file.
608 TString fname(filename);
609 if (!fname.EndsWith(".root")) fname += ".root";
611 fExtensions = new TObjArray();
612 fExtensions->SetOwner();
614 AliAODExtension *ext = (AliAODExtension*)fExtensions->FindObject(fname);
616 ext = new AliAODExtension(fname, title);
617 fExtensions->Add(ext);
622 //______________________________________________________________________________
623 AliAODExtension *AliAODHandler::GetExtension(const char *filename) const
625 // Getter for AOD extensions via file name.
626 if (!fExtensions) return NULL;
627 return (AliAODExtension*)fExtensions->FindObject(filename);
630 //______________________________________________________________________________
631 AliAODExtension *AliAODHandler::AddFilteredAOD(const char *filename, const char *filtername)
633 // Add an AOD extension that can write only AOD events that pass a user filter.
635 fFilters = new TObjArray();
636 fFilters->SetOwner();
638 AliAODExtension *filter = (AliAODExtension*)fFilters->FindObject(filename);
640 filter = new AliAODExtension(filename, filtername, kTRUE);
641 fFilters->Add(filter);
646 //______________________________________________________________________________
647 AliAODExtension *AliAODHandler::GetFilteredAOD(const char *filename) const
649 // Getter for AOD filters via file name.
650 if (!fFilters) return NULL;
651 return (AliAODExtension*)fFilters->FindObject(filename);
654 //______________________________________________________________________________
655 void AliAODHandler::SetOutputFileName(const char* fname)
661 //______________________________________________________________________________
662 const char *AliAODHandler::GetOutputFileName()
665 return fFileName.Data();
668 //______________________________________________________________________________
669 const char *AliAODHandler::GetExtraOutputs() const
671 // Get extra outputs as a string separated by commas.
672 static TString eoutputs;
676 TIter next1(fExtensions);
677 while ((obj=next1())) {
678 if (!eoutputs.IsNull()) eoutputs += ",";
679 eoutputs += obj->GetName();
683 TIter next2(fFilters);
684 while ((obj=next2())) {
685 if (!eoutputs.IsNull()) eoutputs += ",";
686 eoutputs += obj->GetName();
689 return eoutputs.Data();
692 //______________________________________________________________________________
693 Bool_t AliAODHandler::HasExtensions() const
695 // Whether or not we manage extensions
697 if ( fExtensions && fExtensions->GetEntries()>0 ) return kTRUE;
702 //______________________________________________________________________________
703 void AliAODHandler::SetMCHeaderInfo(AliAODMCHeader *mcHeader,AliGenEventHeader *genHeader){
706 // Utility function to cover different cases for the AliGenEventHeader
707 // Needed since different ProcessType and ImpactParamter are not
708 // in the base class...
709 // We don't encode process types for event cocktails yet
710 // could be done e.g. by adding offsets depnding on the generator
712 if(!genHeader)return;
713 mcHeader->AddGeneratorName(genHeader->GetName());
714 AliGenPythiaEventHeader *pythiaGenHeader = dynamic_cast<AliGenPythiaEventHeader*>(genHeader);
715 if (pythiaGenHeader) {
716 mcHeader->SetEventType(pythiaGenHeader->ProcessType());
717 mcHeader->SetPtHard(pythiaGenHeader->GetPtHard());
721 AliGenDPMjetEventHeader* dpmJetGenHeader = dynamic_cast<AliGenDPMjetEventHeader*>(genHeader);
723 if (dpmJetGenHeader){
724 mcHeader->SetEventType(dpmJetGenHeader->ProcessType());
728 AliGenHijingEventHeader* hijingGenHeader = dynamic_cast<AliGenHijingEventHeader*>(genHeader);
730 mcHeader->SetImpactParameter(hijingGenHeader->ImpactParameter());
734 AliWarning(Form("MC Eventheader not known: %s",genHeader->GetName()));