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 TDirectory *owd = gDirectory;
507 char** apointer = (char**) addobj;
508 TObject* obj = (TObject*) *apointer;
510 fAODEvent->AddObject(obj);
512 const Int_t kSplitlevel = 99; // default value in TTree::Branch()
513 const Int_t kBufsize = 32000; // default value in TTree::Branch()
515 if (!fTreeA->FindBranch(obj->GetName())) {
516 // Do the same as if we book via
517 // TTree::Branch(TCollection*)
519 fTreeA->Bronch(obj->GetName(), cname, fAODEvent->GetList()->GetObjectRef(obj),
520 kBufsize, kSplitlevel - 1);
525 //______________________________________________________________________________
526 AliAODExtension *AliAODHandler::AddExtension(const char *filename, const char *title)
528 // Add an AOD extension with some branches in a different file.
529 TString fname(filename);
530 if (!fname.EndsWith(".root")) fname += ".root";
532 fExtensions = new TObjArray();
533 fExtensions->SetOwner();
535 AliAODExtension *ext = (AliAODExtension*)fExtensions->FindObject(fname);
537 ext = new AliAODExtension(fname, title);
538 fExtensions->Add(ext);
543 //______________________________________________________________________________
544 AliAODExtension *AliAODHandler::GetExtension(const char *filename) const
546 // Getter for AOD extensions via file name.
547 if (!fExtensions) return NULL;
548 return (AliAODExtension*)fExtensions->FindObject(filename);
551 //______________________________________________________________________________
552 AliAODExtension *AliAODHandler::AddFilteredAOD(const char *filename, const char *filtername)
554 // Add an AOD extension that can write only AOD events that pass a user filter.
556 fFilters = new TObjArray();
557 fFilters->SetOwner();
559 AliAODExtension *filter = (AliAODExtension*)fFilters->FindObject(filename);
561 filter = new AliAODExtension(filename, filtername, kTRUE);
562 fFilters->Add(filter);
567 //______________________________________________________________________________
568 AliAODExtension *AliAODHandler::GetFilteredAOD(const char *filename) const
570 // Getter for AOD filters via file name.
571 if (!fFilters) return NULL;
572 return (AliAODExtension*)fFilters->FindObject(filename);
575 //______________________________________________________________________________
576 void AliAODHandler::SetOutputFileName(const char* fname)
582 //______________________________________________________________________________
583 const char *AliAODHandler::GetOutputFileName()
586 return fFileName.Data();
589 //______________________________________________________________________________
590 void AliAODHandler::SetMCHeaderInfo(AliAODMCHeader *mcHeader,AliGenEventHeader *genHeader){
593 // Utility function to cover different cases for the AliGenEventHeader
594 // Needed since different ProcessType and ImpactParamter are not
595 // in the base class...
596 // We don't encode process types for event cocktails yet
597 // could be done e.g. by adding offsets depnding on the generator
599 mcHeader->AddGeneratorName(genHeader->GetName());
600 if(!genHeader)return;
601 AliGenPythiaEventHeader *pythiaGenHeader = dynamic_cast<AliGenPythiaEventHeader*>(genHeader);
602 if (pythiaGenHeader) {
603 mcHeader->SetEventType(pythiaGenHeader->ProcessType());
604 mcHeader->SetPtHard(pythiaGenHeader->GetPtHard());
608 AliGenDPMjetEventHeader* dpmJetGenHeader = dynamic_cast<AliGenDPMjetEventHeader*>(genHeader);
610 if (dpmJetGenHeader){
611 mcHeader->SetEventType(dpmJetGenHeader->ProcessType());
615 AliGenHijingEventHeader* hijingGenHeader = dynamic_cast<AliGenHijingEventHeader*>(genHeader);
617 mcHeader->SetImpactParameter(hijingGenHeader->ImpactParameter());
621 AliWarning(Form("MC Eventheader not known: %s",genHeader->GetName()));
625 ClassImp(AliAODExtension)
627 //-------------------------------------------------------------------------
628 // Support class for AOD extensions. This is created by the user analysis
629 // that requires a separate file for some AOD branches. The name of the
630 // AliAODExtension object is the file name where the AOD branches will be
632 //-------------------------------------------------------------------------
634 //______________________________________________________________________________
635 AliAODExtension::AliAODExtension(const char* name, const char* title, Bool_t isfilter)
646 TObject::SetBit(kFilteredAOD);
647 printf("####### Added AOD filter %s\n", name);
648 } else printf("####### Added AOD extension %s\n", name);
651 //______________________________________________________________________________
652 AliAODExtension::~AliAODExtension()
656 // is already handled in TerminateIO
662 if (fTreeE) delete fTreeE;
665 //______________________________________________________________________________
666 void AliAODExtension::AddBranch(const char* cname, void* addobj)
668 // Add a new branch to the aod
669 if (IsFilteredAOD()) {
670 Error("AddBranch", "Not allowed to add branched to filtered AOD's.");
675 gROOT->ProcessLine(Form("TString s_tmp; AliAnalysisManager::GetAnalysisManager()->GetAnalysisTypeString(s_tmp); sprintf((char*)%p, \"%%s\", s_tmp.Data());", type));
678 TDirectory *owd = gDirectory;
682 char** apointer = (char**) addobj;
683 TObject* obj = (TObject*) *apointer;
685 fAODEvent->AddObject(obj);
687 const Int_t kSplitlevel = 99; // default value in TTree::Branch()
688 const Int_t kBufsize = 32000; // default value in TTree::Branch()
690 if (!fTreeE->FindBranch(obj->GetName())) {
691 // Do the same as if we book via
692 // TTree::Branch(TCollection*)
693 fTreeE->Bronch(obj->GetName(), cname, fAODEvent->GetList()->GetObjectRef(obj),
694 kBufsize, kSplitlevel - 1);
699 //______________________________________________________________________________
700 Bool_t AliAODExtension::FinishEvent()
702 // Fill current event.
704 if (!IsFilteredAOD()) {
705 fAODEvent->MakeEntriesReferencable();
709 // Filtered AOD. Fill only if event is selected.
710 if (!fSelected) return kTRUE;
713 fSelected = kFALSE; // so that next event will not be selected unless demanded
717 //______________________________________________________________________________
718 Bool_t AliAODExtension::Init(Option_t *option)
721 if(!fAODEvent) fAODEvent = new AliAODEvent();
722 TDirectory *owd = gDirectory;
725 if (opt.Contains("proof")) {
727 // Merging via files. Need to access analysis manager via interpreter.
728 gROOT->ProcessLine(Form("AliAnalysisManager::GetAnalysisManager()->OpenProofFile(\"%s\", \"RECREATE\");", fName.Data()));
731 fFileE = new TFile(GetName(), "RECREATE");
733 fTreeE = new TTree("aodTree", "AliAOD tree");
734 fTreeE->Branch(fAODEvent->GetList());
740 //______________________________________________________________________________
741 void AliAODExtension::SetEvent(AliAODEvent *event)
743 // Connects to an external event
744 if (!IsFilteredAOD()) {
745 Error("SetEvent", "Not allowed to set external event for filtered AOD's");
748 // Use the copy constructor or assignment operator to synchronize with external event.
749 // AliAODEvent &other = *event;
750 // if (!fAODEvent) fAODEvent = new AliAODEvent(other);
751 // else if (fSelected) *fAODEvent = other;
755 //______________________________________________________________________________
756 Bool_t AliAODExtension::TerminateIO()
759 if (TObject::TestBit(kFilteredAOD))
760 printf("AOD Filter %s: events processed: %d passed: %d\n", GetName(), fNtotal, fNpassed);
762 printf("AOD extension %s: events processed: %d\n", GetName(), fNtotal);