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"
48 ClassImp(AliAODHandler)
50 //______________________________________________________________________________
51 AliAODHandler::AliAODHandler() :
56 fNeedsHeaderReplication(kFALSE),
57 fNeedsTracksBranchReplication(kFALSE),
58 fNeedsVerticesBranchReplication(kFALSE),
59 fNeedsV0sBranchReplication(kFALSE),
60 fNeedsCascadesBranchReplication(kFALSE),
61 fNeedsTrackletsBranchReplication(kFALSE),
62 fNeedsPMDClustersBranchReplication(kFALSE),
63 fNeedsJetsBranchReplication(kFALSE),
64 fNeedsFMDClustersBranchReplication(kFALSE),
65 fNeedsCaloClustersBranchReplication(kFALSE),
66 fNeedsMCParticlesBranchReplication(kFALSE),
67 fNeedsDimuonsBranchReplication(kFALSE),
68 fAODIsReplicated(kFALSE),
77 // default constructor
80 //______________________________________________________________________________
81 AliAODHandler::AliAODHandler(const char* name, const char* title):
82 AliVEventHandler(name, title),
86 fNeedsHeaderReplication(kFALSE),
87 fNeedsTracksBranchReplication(kFALSE),
88 fNeedsVerticesBranchReplication(kFALSE),
89 fNeedsV0sBranchReplication(kFALSE),
90 fNeedsCascadesBranchReplication(kFALSE),
91 fNeedsTrackletsBranchReplication(kFALSE),
92 fNeedsPMDClustersBranchReplication(kFALSE),
93 fNeedsJetsBranchReplication(kFALSE),
94 fNeedsFMDClustersBranchReplication(kFALSE),
95 fNeedsCaloClustersBranchReplication(kFALSE),
96 fNeedsMCParticlesBranchReplication(kFALSE),
97 fNeedsDimuonsBranchReplication(kFALSE),
98 fAODIsReplicated(kFALSE),
107 // Normal constructor.
110 //______________________________________________________________________________
111 AliAODHandler::~AliAODHandler()
114 if (fAODEvent) delete fAODEvent;
116 // is already handled in TerminateIO
121 if (fTreeA) delete fTreeA;
122 if (fExtensions) {fExtensions->Delete(); delete fExtensions;}
123 if (fFilters) {fFilters->Delete(); delete fFilters;}
126 //______________________________________________________________________________
127 Bool_t AliAODHandler::Init(Option_t* opt)
131 // Create the AODevent object
133 Bool_t createStdAOD = fIsStandard || fFillAOD;
134 if(!fAODEvent && createStdAOD){
135 fAODEvent = new AliAODEvent();
137 fAODEvent->CreateStdContent();
140 // File opening according to execution mode
144 TDirectory *owd = gDirectory;
145 if (option.Contains("proof")) {
147 // Merging via files. Need to access analysis manager via interpreter.
148 gROOT->ProcessLine(Form("AliAnalysisDataContainer *c_common_out = AliAnalysisManager::GetAnalysisManager()->GetCommonOutputContainer();"));
149 gROOT->ProcessLine(Form("AliAnalysisManager::GetAnalysisManager()->OpenProofFile(c_common_out, \"RECREATE\");"));
153 fFileA = new TFile(fFileName.Data(), "RECREATE");
159 TIter next(fExtensions);
160 AliAODExtension *ext;
161 while ((ext=(AliAODExtension*)next())) ext->Init(option);
164 TIter nextf(fFilters);
165 AliAODExtension *filteredAOD;
166 while ((filteredAOD=(AliAODExtension*)nextf())) {
167 filteredAOD->SetEvent(fAODEvent);
168 filteredAOD->Init(option);
174 //______________________________________________________________________________
175 void AliAODHandler::StoreMCParticles(){
178 // Remap the labels from ESD stack and store
179 // the AODMCParticles, makes only sense if we have
180 // the mcparticles branch
181 // has to be done here since we cannot know in advance
182 // which particles are needed (e.g. by the tracks etc.)
184 // Particles have been selected by AliMCEventhanlder->SelectParticle()
185 // To use the MCEventhandler here we need to set it from the outside
186 // can vanish when Handler go to the ANALYSISalice library
188 // The Branch booking for mcParticles and mcHeader has to happen
189 // in an external task for now since the AODHandler does not have access
190 // the AnalysisManager. For the same reason the pointer t o the MCEventH
191 // has to passed to the AOD Handler by this task
192 // (doing this in the steering macro would not work on PROOF)
194 if (!fAODEvent) return;
195 TClonesArray *mcarray = (TClonesArray*)fAODEvent->FindListObject(AliAODMCParticle::StdBranchName());
198 AliAODMCHeader *mcHeader = (AliAODMCHeader*)fAODEvent->FindListObject(AliAODMCHeader::StdBranchName());
201 // Get the MC Infos.. Handler needs to be set before
202 // while adding the branch
203 // This needs to be done, not to depend on the AnalysisManager
205 if(!fMCEventH)return;
206 if(!fMCEventH->MCEvent())return;
207 AliStack *pStack = fMCEventH->MCEvent()->Stack();
210 fMCEventH->CreateLabelMap();
213 // Get the Event Header
216 AliHeader* header = fMCEventH->MCEvent()->Header();
218 AliGenEventHeader* genHeader = 0;
219 if (header) genHeader = header->GenEventHeader();
222 genHeader->PrimaryVertex(vtxMC);
223 mcHeader->SetVertex(vtxMC[0],vtxMC[1],vtxMC[2]);
225 // we search the MCEventHeaders first
226 // Two cases, cocktail or not...
227 AliGenCocktailEventHeader* genCocktailHeader = dynamic_cast<AliGenCocktailEventHeader*>(genHeader);
228 if(genCocktailHeader){
229 // we have a coktail header
230 mcHeader->AddGeneratorName(genHeader->GetName());
231 // Loop from the back so that the first one sets the process type
232 TList* headerList = genCocktailHeader->GetHeaders();
233 for(int i = headerList->GetEntries()-1;i>=0;--i){
234 AliGenEventHeader *headerEntry = dynamic_cast<AliGenEventHeader*>(headerList->At(i));
235 SetMCHeaderInfo(mcHeader,headerEntry);
239 // No Cocktail just take the first one
240 SetMCHeaderInfo(mcHeader,genHeader);
248 // Store the AliAODParticlesMC
249 AliMCEvent* mcEvent = fMCEventH->MCEvent();
251 Int_t np = mcEvent->GetNumberOfTracks();
252 Int_t nprim = mcEvent->GetNumberOfPrimaries();
256 TClonesArray& l = *mcarray;
258 for(int i = 0; i < np; ++i){
259 if(fMCEventH->IsParticleSelected(i)){
261 AliMCParticle* mcpart = (AliMCParticle*) mcEvent->GetTrack(i);
262 if(i<nprim)flag |= AliAODMCParticle::kPrimary;
264 if(mcEvent->IsPhysicalPrimary(i))flag |= AliAODMCParticle::kPhysicalPrim;
266 if(fMCEventH->GetNewLabel(i)!=j){
267 AliError(Form("MISMATCH New label %d j: %d",fMCEventH->GetNewLabel(i),j));
270 AliAODMCParticle mcpart_tmp(mcpart,i,flag);
272 mcpart_tmp.SetStatus(mcpart->Particle()->GetStatusCode());
274 Int_t d0 = mcpart_tmp.GetDaughter(0);
275 Int_t d1 = mcpart_tmp.GetDaughter(1);
276 Int_t m = mcpart_tmp.GetMother();
278 // other than for the track labels, negative values mean
279 // no daughter/mother so preserve it
282 // no first daughter -> no second daughter
283 // nothing to be done
284 // second condition not needed just for sanity check at the end
285 mcpart_tmp.SetDaughter(0,d0);
286 mcpart_tmp.SetDaughter(1,d1);
287 } else if(d1 < 0 && d0 >= 0) {
289 // second condition not needed just for sanity check at the end
290 if(fMCEventH->IsParticleSelected(d0)){
291 mcpart_tmp.SetDaughter(0,fMCEventH->GetNewLabel(d0));
293 mcpart_tmp.SetDaughter(0,-1);
295 mcpart_tmp.SetDaughter(1,d1);
297 else if (d0 > 0 && d1 > 0 ){
298 // we have two or more daughters loop on the stack to see if they are
302 for(int id = d0; id<=d1;++id){
303 if(fMCEventH->IsParticleSelected(id)){
306 d0_tmp = fMCEventH->GetNewLabel(id);
307 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
309 else d1_tmp = fMCEventH->GetNewLabel(id);
312 mcpart_tmp.SetDaughter(0,d0_tmp);
313 mcpart_tmp.SetDaughter(1,d1_tmp);
315 AliError(Form("Unxpected indices %d %d",d0,d1));
319 mcpart_tmp.SetMother(m);
321 if(fMCEventH->IsParticleSelected(m))mcpart_tmp.SetMother(fMCEventH->GetNewLabel(m));
322 else AliError(Form("PROBLEM Mother not selected %d \n", m));
325 new (l[j++]) AliAODMCParticle(mcpart_tmp);
329 AliInfo(Form("AliAODHandler::StoreMCParticles: Selected %d (Primaries %d / total %d) after validation \n",
332 // Set the labels in the AOD output...
336 TClonesArray* tracks = fAODEvent->GetTracks();
338 for(int it = 0; it < fAODEvent->GetNTracks();++it){
339 AliAODTrack *track = fAODEvent->GetTrack(it);
342 Int_t label = track->GetLabel();
343 if(label<0){ // preserve the sign for later usage
348 if (label >= AliMCEvent::BgLabelOffset()) label = mcEvent->BgLabelToIndex(label);
349 if(label > np || track->GetLabel() == 0){
350 AliWarning(Form("Wrong ESD track label %5d (%5d)",track->GetLabel(), label));
352 if(fMCEventH->GetNewLabel(label) == 0){
353 AliWarning(Form("New label not found for %5d (%5d)",track->GetLabel(), label));
355 track->SetLabel(sign*fMCEventH->GetNewLabel(label));
360 TClonesArray *clusters = fAODEvent->GetCaloClusters();
362 for (Int_t iClust = 0;iClust < fAODEvent->GetNumberOfCaloClusters(); ++iClust) {
363 AliAODCaloCluster * cluster = fAODEvent->GetCaloCluster(iClust);
364 UInt_t nLabel = cluster->GetNLabels();
365 // Ugly but do not want to fragment memory by creating
366 // new Int_t (nLabel)
367 Int_t* labels = const_cast<Int_t*>(cluster->GetLabels());
369 for(UInt_t i = 0;i < nLabel;++i){
370 labels[i] = fMCEventH->GetNewLabel(cluster->GetLabelAt(i));
373 // cluster->SetLabels(labels,nLabel);
378 AliAODTracklets *tracklets = fAODEvent->GetTracklets();
380 for(int it = 0;it < tracklets->GetNumberOfTracklets();++it){
381 int label0 = tracklets->GetLabel(it,0);
382 int label1 = tracklets->GetLabel(it,1);
383 if(label0>=0)label0 = fMCEventH->GetNewLabel(label0);
384 if(label1>=0)label1 = fMCEventH->GetNewLabel(label1);
385 tracklets->SetLabel(it,0,label0);
386 tracklets->SetLabel(it,1,label1);
392 //______________________________________________________________________________
393 Bool_t AliAODHandler::FinishEvent()
395 // Fill data structures
396 if(fFillAOD && fFillAODRun && fAODEvent){
397 fAODEvent->MakeEntriesReferencable();
402 if (fFillAOD && fFillAODRun) {
404 TIter next(fExtensions);
405 AliAODExtension *ext;
406 while ((ext=(AliAODExtension*)next())) ext->FinishEvent();
409 TIter nextf(fFilters);
410 AliAODExtension *ext;
411 while ((ext=(AliAODExtension*)nextf())) {
416 if (fIsStandard) fAODEvent->ResetStd();
418 TClonesArray *mcarray = (TClonesArray*)fAODEvent->FindListObject(AliAODMCParticle::StdBranchName());
419 if(mcarray) mcarray->Delete();
421 AliAODMCHeader *mcHeader = (AliAODMCHeader*)fAODEvent->FindListObject(AliAODMCHeader::StdBranchName());
422 if(mcHeader) mcHeader->Reset();
424 // Reset AOD replication flag
425 fAODIsReplicated = kFALSE;
429 //______________________________________________________________________________
430 Bool_t AliAODHandler::Terminate()
433 AddAODtoTreeUserInfo();
435 TIter next(fExtensions);
436 AliAODExtension *ext;
437 while ((ext=(AliAODExtension*)next())) ext->GetTree()->GetUserInfo()->Add(ext->GetAOD());
442 //______________________________________________________________________________
443 Bool_t AliAODHandler::TerminateIO()
451 // When closing the file, the tree is also deleted.
455 TIter next(fExtensions);
456 AliAODExtension *ext;
457 while ((ext=(AliAODExtension*)next())) ext->TerminateIO();
460 TIter nextf(fFilters);
461 AliAODExtension *ext;
462 while ((ext=(AliAODExtension*)nextf())) ext->TerminateIO();
467 //______________________________________________________________________________
468 void AliAODHandler::CreateTree(Int_t flag)
470 // Creates the AOD Tree
471 fTreeA = new TTree("aodTree", "AliAOD tree");
472 fTreeA->Branch(fAODEvent->GetList());
473 if (flag == 0) fTreeA->SetDirectory(0);
476 //______________________________________________________________________________
477 void AliAODHandler::FillTree()
484 //______________________________________________________________________________
485 void AliAODHandler::AddAODtoTreeUserInfo()
487 // Add aod event to tree user info
488 if (fTreeA) fTreeA->GetUserInfo()->Add(fAODEvent);
489 // Now the tree owns our fAODEvent...
493 //______________________________________________________________________________
494 void AliAODHandler::AddBranch(const char* cname, void* addobj, const char* filename)
496 // Add a new branch to the aod. Added optional filename parameter if the
497 // branch should be written to a separate file.
498 if (strlen(filename)) {
499 AliAODExtension *ext = AddExtension(filename);
500 ext->AddBranch(cname, addobj);
503 // Add branch to all filters
505 TIter next(fFilters);
506 AliAODExtension *ext;
507 while ((ext=(AliAODExtension*)next())) ext->AddBranch(cname, addobj);
510 TDirectory *owd = gDirectory;
514 char** apointer = (char**) addobj;
515 TObject* obj = (TObject*) *apointer;
517 fAODEvent->AddObject(obj);
519 const Int_t kSplitlevel = 99; // default value in TTree::Branch()
520 const Int_t kBufsize = 32000; // default value in TTree::Branch()
522 if (!fTreeA->FindBranch(obj->GetName())) {
523 // Do the same as if we book via
524 // TTree::Branch(TCollection*)
526 fTreeA->Bronch(obj->GetName(), cname, fAODEvent->GetList()->GetObjectRef(obj),
527 kBufsize, kSplitlevel - 1);
532 //______________________________________________________________________________
533 AliAODExtension *AliAODHandler::AddExtension(const char *filename, const char *title)
535 // Add an AOD extension with some branches in a different file.
536 TString fname(filename);
537 if (!fname.EndsWith(".root")) fname += ".root";
539 fExtensions = new TObjArray();
540 fExtensions->SetOwner();
542 AliAODExtension *ext = (AliAODExtension*)fExtensions->FindObject(fname);
544 ext = new AliAODExtension(fname, title);
545 fExtensions->Add(ext);
550 //______________________________________________________________________________
551 AliAODExtension *AliAODHandler::GetExtension(const char *filename) const
553 // Getter for AOD extensions via file name.
554 if (!fExtensions) return NULL;
555 return (AliAODExtension*)fExtensions->FindObject(filename);
558 //______________________________________________________________________________
559 AliAODExtension *AliAODHandler::AddFilteredAOD(const char *filename, const char *filtername)
561 // Add an AOD extension that can write only AOD events that pass a user filter.
563 fFilters = new TObjArray();
564 fFilters->SetOwner();
566 AliAODExtension *filter = (AliAODExtension*)fFilters->FindObject(filename);
568 filter = new AliAODExtension(filename, filtername, kTRUE);
569 fFilters->Add(filter);
574 //______________________________________________________________________________
575 AliAODExtension *AliAODHandler::GetFilteredAOD(const char *filename) const
577 // Getter for AOD filters via file name.
578 if (!fFilters) return NULL;
579 return (AliAODExtension*)fFilters->FindObject(filename);
582 //______________________________________________________________________________
583 void AliAODHandler::SetOutputFileName(const char* fname)
589 //______________________________________________________________________________
590 const char *AliAODHandler::GetOutputFileName()
593 return fFileName.Data();
596 //______________________________________________________________________________
597 const char *AliAODHandler::GetExtraOutputs() const
599 // Get extra outputs as a string separated by commas.
600 static TString eoutputs;
604 TIter next1(fExtensions);
605 while ((obj=next1())) {
606 if (!eoutputs.IsNull()) eoutputs += ",";
607 eoutputs += obj->GetName();
611 TIter next2(fFilters);
612 while ((obj=next2())) {
613 if (!eoutputs.IsNull()) eoutputs += ",";
614 eoutputs += obj->GetName();
617 return eoutputs.Data();
620 //______________________________________________________________________________
621 void AliAODHandler::SetMCHeaderInfo(AliAODMCHeader *mcHeader,AliGenEventHeader *genHeader){
624 // Utility function to cover different cases for the AliGenEventHeader
625 // Needed since different ProcessType and ImpactParamter are not
626 // in the base class...
627 // We don't encode process types for event cocktails yet
628 // could be done e.g. by adding offsets depnding on the generator
630 mcHeader->AddGeneratorName(genHeader->GetName());
631 if(!genHeader)return;
632 AliGenPythiaEventHeader *pythiaGenHeader = dynamic_cast<AliGenPythiaEventHeader*>(genHeader);
633 if (pythiaGenHeader) {
634 mcHeader->SetEventType(pythiaGenHeader->ProcessType());
635 mcHeader->SetPtHard(pythiaGenHeader->GetPtHard());
639 AliGenDPMjetEventHeader* dpmJetGenHeader = dynamic_cast<AliGenDPMjetEventHeader*>(genHeader);
641 if (dpmJetGenHeader){
642 mcHeader->SetEventType(dpmJetGenHeader->ProcessType());
646 AliGenHijingEventHeader* hijingGenHeader = dynamic_cast<AliGenHijingEventHeader*>(genHeader);
648 mcHeader->SetImpactParameter(hijingGenHeader->ImpactParameter());
652 AliWarning(Form("MC Eventheader not known: %s",genHeader->GetName()));
656 ClassImp(AliAODExtension)
658 //-------------------------------------------------------------------------
659 // Support class for AOD extensions. This is created by the user analysis
660 // that requires a separate file for some AOD branches. The name of the
661 // AliAODExtension object is the file name where the AOD branches will be
663 //-------------------------------------------------------------------------
665 //______________________________________________________________________________
666 AliAODExtension::AliAODExtension(const char* name, const char* title, Bool_t isfilter)
677 TObject::SetBit(kFilteredAOD);
678 printf("####### Added AOD filter %s\n", name);
679 } else printf("####### Added AOD extension %s\n", name);
682 //______________________________________________________________________________
683 AliAODExtension::~AliAODExtension()
687 // is already handled in TerminateIO
693 if (fTreeE) delete fTreeE;
696 //______________________________________________________________________________
697 void AliAODExtension::AddBranch(const char* cname, void* addobj)
699 // Add a new branch to the aod
700 // if (IsFilteredAOD()) {
701 // Error("AddBranch", "Not allowed to add branched to filtered AOD's.");
706 gROOT->ProcessLine(Form("TString s_tmp; AliAnalysisManager::GetAnalysisManager()->GetAnalysisTypeString(s_tmp); sprintf((char*)%p, \"%%s\", s_tmp.Data());", type));
709 TDirectory *owd = gDirectory;
713 char** apointer = (char**) addobj;
714 TObject* obj = (TObject*) *apointer;
716 fAODEvent->AddObject(obj);
718 const Int_t kSplitlevel = 99; // default value in TTree::Branch()
719 const Int_t kBufsize = 32000; // default value in TTree::Branch()
721 if (!fTreeE->FindBranch(obj->GetName())) {
722 // Do the same as if we book via
723 // TTree::Branch(TCollection*)
724 fTreeE->Bronch(obj->GetName(), cname, fAODEvent->GetList()->GetObjectRef(obj),
725 kBufsize, kSplitlevel - 1);
730 //______________________________________________________________________________
731 Bool_t AliAODExtension::FinishEvent()
733 // Fill current event.
735 if (!IsFilteredAOD()) {
736 fAODEvent->MakeEntriesReferencable();
740 // Filtered AOD. Fill only if event is selected.
741 if (!fSelected) return kTRUE;
744 fSelected = kFALSE; // so that next event will not be selected unless demanded
748 //______________________________________________________________________________
749 Bool_t AliAODExtension::Init(Option_t *option)
752 if(!fAODEvent) fAODEvent = new AliAODEvent();
753 TDirectory *owd = gDirectory;
756 if (opt.Contains("proof")) {
758 // Merging via files. Need to access analysis manager via interpreter.
759 gROOT->ProcessLine(Form("AliAnalysisDataContainer *c_common_out = AliAnalysisManager::GetAnalysisManager()->GetCommonOutputContainer();"));
760 gROOT->ProcessLine(Form("AliAnalysisManager::GetAnalysisManager()->OpenProofFile(c_common_out, \"RECREATE\", \"%s\");", fName.Data()));
763 fFileE = new TFile(GetName(), "RECREATE");
765 fTreeE = new TTree("aodTree", "AliAOD tree");
766 fTreeE->Branch(fAODEvent->GetList());
772 //______________________________________________________________________________
773 void AliAODExtension::SetEvent(AliAODEvent *event)
775 // Connects to an external event
776 if (!IsFilteredAOD()) {
777 Error("SetEvent", "Not allowed to set external event for filtered AOD's");
780 // Use the copy constructor or assignment operator to synchronize with external event.
781 // AliAODEvent &other = *event;
782 // if (!fAODEvent) fAODEvent = new AliAODEvent(other);
783 // else if (fSelected) *fAODEvent = other;
787 //______________________________________________________________________________
788 Bool_t AliAODExtension::TerminateIO()
791 if (TObject::TestBit(kFilteredAOD))
792 printf("AOD Filter %s: events processed: %d passed: %d\n", GetName(), fNtotal, fNpassed);
794 printf("AOD extension %s: events processed: %d\n", GetName(), fNtotal);