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() :
55 fNeedsHeaderReplication(kFALSE),
56 fNeedsTracksBranchReplication(kFALSE),
57 fNeedsVerticesBranchReplication(kFALSE),
58 fNeedsV0sBranchReplication(kFALSE),
59 fNeedsTrackletsBranchReplication(kFALSE),
60 fNeedsPMDClustersBranchReplication(kFALSE),
61 fNeedsJetsBranchReplication(kFALSE),
62 fNeedsFMDClustersBranchReplication(kFALSE),
63 fNeedsCaloClustersBranchReplication(kFALSE),
64 fNeedsMCParticlesBranchReplication(kFALSE),
65 fNeedsDimuonsBranchReplication(kFALSE),
66 fAODIsReplicated(kFALSE),
75 // default constructor
78 //______________________________________________________________________________
79 AliAODHandler::AliAODHandler(const char* name, const char* title):
80 AliVEventHandler(name, title),
83 fNeedsHeaderReplication(kFALSE),
84 fNeedsTracksBranchReplication(kFALSE),
85 fNeedsVerticesBranchReplication(kFALSE),
86 fNeedsV0sBranchReplication(kFALSE),
87 fNeedsTrackletsBranchReplication(kFALSE),
88 fNeedsPMDClustersBranchReplication(kFALSE),
89 fNeedsJetsBranchReplication(kFALSE),
90 fNeedsFMDClustersBranchReplication(kFALSE),
91 fNeedsCaloClustersBranchReplication(kFALSE),
92 fNeedsMCParticlesBranchReplication(kFALSE),
93 fNeedsDimuonsBranchReplication(kFALSE),
94 fAODIsReplicated(kFALSE),
103 // Normal constructor.
106 //______________________________________________________________________________
107 AliAODHandler::~AliAODHandler()
110 if (fAODEvent) delete fAODEvent;
112 // is already handled in TerminateIO
117 if (fTreeA) delete fTreeA;
118 if (fExtensions) {fExtensions->Delete(); delete fExtensions;}
119 if (fFilters) {fFilters->Delete(); delete fFilters;}
122 //______________________________________________________________________________
123 Bool_t AliAODHandler::Init(Option_t* opt)
127 // Create the AODevent object
128 Bool_t createStdAOD = fIsStandard || fFillAOD;
129 if(!fAODEvent && createStdAOD){
130 fAODEvent = new AliAODEvent();
131 if (fIsStandard) fAODEvent->CreateStdContent();
134 // File opening according to execution mode
138 TDirectory *owd = gDirectory;
139 if (option.Contains("proof")) {
141 // Merging via files. Need to access analysis manager via interpreter.
142 gROOT->ProcessLine(Form("AliAnalysisDataContainer *c_common_out = AliAnalysisManager::GetAnalysisManager()->GetCommonOutputContainer();"));
143 gROOT->ProcessLine(Form("AliAnalysisManager::GetAnalysisManager()->OpenProofFile(c_common_out, \"RECREATE\");"));
147 fFileA = new TFile(fFileName.Data(), "RECREATE");
153 TIter next(fExtensions);
154 AliAODExtension *ext;
155 while ((ext=(AliAODExtension*)next())) ext->Init(option);
158 TIter nextf(fFilters);
159 AliAODExtension *filteredAOD;
160 while ((filteredAOD=(AliAODExtension*)nextf())) {
161 filteredAOD->SetEvent(fAODEvent);
162 filteredAOD->Init(option);
168 //______________________________________________________________________________
169 void AliAODHandler::StoreMCParticles(){
172 // Remap the labels from ESD stack and store
173 // the AODMCParticles, makes only sense if we have
174 // the mcparticles branch
175 // has to be done here since we cannot know in advance
176 // which particles are needed (e.g. by the tracks etc.)
178 // Particles have been selected by AliMCEventhanlder->SelectParticle()
179 // To use the MCEventhandler here we need to set it from the outside
180 // can vanish when Handler go to the ANALYSISalice library
182 // The Branch booking for mcParticles and mcHeader has to happen
183 // in an external task for now since the AODHandler does not have access
184 // the AnalysisManager. For the same reason the pointer t o the MCEventH
185 // has to passed to the AOD Handler by this task
186 // (doing this in the steering macro would not work on PROOF)
188 if (!fAODEvent) return;
189 TClonesArray *mcarray = (TClonesArray*)fAODEvent->FindListObject(AliAODMCParticle::StdBranchName());
193 AliAODMCHeader *mcHeader = (AliAODMCHeader*)fAODEvent->FindListObject(AliAODMCHeader::StdBranchName());
198 // Get the MC Infos.. Handler needs to be set before
199 // while adding the branch
200 // This needs to be done, not to depend on the AnalysisManager
202 if(!fMCEventH)return;
203 if(!fMCEventH->MCEvent())return;
204 AliStack *pStack = fMCEventH->MCEvent()->Stack();
207 fMCEventH->CreateLabelMap();
210 // Get the Event Header
213 AliHeader* header = fMCEventH->MCEvent()->Header();
215 AliGenEventHeader* genHeader = 0;
216 if (header) genHeader = header->GenEventHeader();
219 genHeader->PrimaryVertex(vtxMC);
220 mcHeader->SetVertex(vtxMC[0],vtxMC[1],vtxMC[2]);
222 // we search the MCEventHeaders first
223 // Two cases, cocktail or not...
224 AliGenCocktailEventHeader* genCocktailHeader = dynamic_cast<AliGenCocktailEventHeader*>(genHeader);
225 if(genCocktailHeader){
226 // we have a coktail header
227 mcHeader->AddGeneratorName(genHeader->GetName());
228 // Loop from the back so that the first one sets the process type
229 TList* headerList = genCocktailHeader->GetHeaders();
230 for(int i = headerList->GetEntries()-1;i>=0;--i){
231 AliGenEventHeader *headerEntry = dynamic_cast<AliGenEventHeader*>(headerList->At(i));
232 SetMCHeaderInfo(mcHeader,headerEntry);
236 // No Cocktail just take the first one
237 SetMCHeaderInfo(mcHeader,genHeader);
245 // Store the AliAODParticlesMC
246 AliMCEvent* mcEvent = fMCEventH->MCEvent();
248 Int_t np = mcEvent->GetNumberOfTracks();
249 Int_t nprim = mcEvent->GetNumberOfPrimaries();
253 TClonesArray& l = *mcarray;
255 for(int i = 0; i < np; ++i){
256 if(fMCEventH->IsParticleSelected(i)){
258 AliMCParticle* mcpart = (AliMCParticle*) mcEvent->GetTrack(i);
259 if(i<nprim)flag |= AliAODMCParticle::kPrimary;
261 if(mcEvent->IsPhysicalPrimary(i))flag |= AliAODMCParticle::kPhysicalPrim;
263 if(fMCEventH->GetNewLabel(i)!=j){
264 AliError(Form("MISMATCH New label %d j: %d",fMCEventH->GetNewLabel(i),j));
267 AliAODMCParticle mcpart_tmp(mcpart,i,flag);
269 mcpart_tmp.SetStatus(mcpart->Particle()->GetStatusCode());
271 Int_t d0 = mcpart_tmp.GetDaughter(0);
272 Int_t d1 = mcpart_tmp.GetDaughter(1);
273 Int_t m = mcpart_tmp.GetMother();
275 // other than for the track labels, negative values mean
276 // no daughter/mother so preserve it
279 // no first daughter -> no second daughter
280 // nothing to be done
281 // second condition not needed just for sanity check at the end
282 mcpart_tmp.SetDaughter(0,d0);
283 mcpart_tmp.SetDaughter(1,d1);
284 } else if(d1 < 0 && d0 >= 0) {
286 // second condition not needed just for sanity check at the end
287 if(fMCEventH->IsParticleSelected(d0)){
288 mcpart_tmp.SetDaughter(0,fMCEventH->GetNewLabel(d0));
290 mcpart_tmp.SetDaughter(0,-1);
292 mcpart_tmp.SetDaughter(1,d1);
294 else if (d0 > 0 && d1 > 0 ){
295 // we have two or more daughters loop on the stack to see if they are
299 for(int id = d0; id<=d1;++id){
300 if(fMCEventH->IsParticleSelected(id)){
303 d0_tmp = fMCEventH->GetNewLabel(id);
304 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
306 else d1_tmp = fMCEventH->GetNewLabel(id);
309 mcpart_tmp.SetDaughter(0,d0_tmp);
310 mcpart_tmp.SetDaughter(1,d1_tmp);
312 AliError(Form("Unxpected indices %d %d",d0,d1));
316 mcpart_tmp.SetMother(m);
318 if(fMCEventH->IsParticleSelected(m))mcpart_tmp.SetMother(fMCEventH->GetNewLabel(m));
319 else AliError(Form("PROBLEM Mother not selected %d \n", m));
322 new (l[j++]) AliAODMCParticle(mcpart_tmp);
326 AliInfo(Form("AliAODHandler::StoreMCParticles: Selected %d (Primaries %d / total %d) after validation \n",
329 // Set the labels in the AOD output...
333 TClonesArray* tracks = fAODEvent->GetTracks();
335 for(int it = 0; it < fAODEvent->GetNTracks();++it){
336 AliAODTrack *track = fAODEvent->GetTrack(it);
339 Int_t label = track->GetLabel();
340 if(label<0){ // preserve the sign for later usage
345 if (label >= AliMCEvent::BgLabelOffset()) label = mcEvent->BgLabelToIndex(label);
346 if(label > np || track->GetLabel() == 0){
347 AliWarning(Form("Wrong ESD track label %5d (%5d)",track->GetLabel(), label));
349 if(fMCEventH->GetNewLabel(label) == 0){
350 AliWarning(Form("New label not found for %5d (%5d)",track->GetLabel(), label));
352 track->SetLabel(sign*fMCEventH->GetNewLabel(label));
357 TClonesArray *clusters = fAODEvent->GetCaloClusters();
359 for (Int_t iClust = 0;iClust < fAODEvent->GetNCaloClusters(); ++iClust) {
360 AliAODCaloCluster * cluster = fAODEvent->GetCaloCluster(iClust);
361 UInt_t nLabel = cluster->GetNLabel();
362 // Ugly but do not want to fragment memory by creating
363 // new Int_t (nLabel)
364 Int_t* labels = const_cast<Int_t*>(cluster->GetLabels());
366 for(UInt_t i = 0;i < nLabel;++i){
367 labels[i] = fMCEventH->GetNewLabel(cluster->GetLabel(i));
370 // cluster->SetLabels(labels,nLabel);
375 AliAODTracklets *tracklets = fAODEvent->GetTracklets();
377 for(int it = 0;it < tracklets->GetNumberOfTracklets();++it){
378 int label0 = tracklets->GetLabel(it,0);
379 int label1 = tracklets->GetLabel(it,1);
380 if(label0>=0)label0 = fMCEventH->GetNewLabel(label0);
381 if(label1>=0)label1 = fMCEventH->GetNewLabel(label1);
382 tracklets->SetLabel(it,0,label0);
383 tracklets->SetLabel(it,1,label1);
389 //______________________________________________________________________________
390 Bool_t AliAODHandler::FinishEvent()
392 // Fill data structures
394 fAODEvent->MakeEntriesReferencable();
395 // StoreMCParticles();
399 TIter next(fExtensions);
400 AliAODExtension *ext;
401 while ((ext=(AliAODExtension*)next())) ext->FinishEvent();
404 TIter nextf(fFilters);
405 AliAODExtension *ext;
406 while ((ext=(AliAODExtension*)nextf())) {
411 if (fIsStandard) fAODEvent->ResetStd();
412 // Reset AOD replication flag
413 fAODIsReplicated = kFALSE;
417 //______________________________________________________________________________
418 Bool_t AliAODHandler::Terminate()
421 AddAODtoTreeUserInfo();
423 TIter next(fExtensions);
424 AliAODExtension *ext;
425 while ((ext=(AliAODExtension*)next())) ext->GetTree()->GetUserInfo()->Add(ext->GetAOD());
430 //______________________________________________________________________________
431 Bool_t AliAODHandler::TerminateIO()
439 // When closing the file, the tree is also deleted.
443 TIter next(fExtensions);
444 AliAODExtension *ext;
445 while ((ext=(AliAODExtension*)next())) ext->TerminateIO();
448 TIter nextf(fFilters);
449 AliAODExtension *ext;
450 while ((ext=(AliAODExtension*)nextf())) ext->TerminateIO();
455 //______________________________________________________________________________
456 void AliAODHandler::CreateTree(Int_t flag)
458 // Creates the AOD Tree
459 fTreeA = new TTree("aodTree", "AliAOD tree");
460 fTreeA->Branch(fAODEvent->GetList());
462 if (flag == 0) fTreeA->SetDirectory(0);
465 //______________________________________________________________________________
466 void AliAODHandler::FillTree()
473 //______________________________________________________________________________
474 void AliAODHandler::AddAODtoTreeUserInfo()
476 // Add aod event to tree user info
477 if (fTreeA) fTreeA->GetUserInfo()->Add(fAODEvent);
478 // Now the tree owns our fAODEvent...
482 //______________________________________________________________________________
483 void AliAODHandler::AddBranch(const char* cname, void* addobj, const char* filename)
485 // Add a new branch to the aod. Added optional filename parameter if the
486 // branch should be written to a separate file.
487 if (strlen(filename)) {
488 AliAODExtension *ext = AddExtension(filename);
489 ext->AddBranch(cname, addobj);
492 TDirectory *owd = gDirectory;
496 char** apointer = (char**) addobj;
497 TObject* obj = (TObject*) *apointer;
499 fAODEvent->AddObject(obj);
501 const Int_t kSplitlevel = 99; // default value in TTree::Branch()
502 const Int_t kBufsize = 32000; // default value in TTree::Branch()
504 if (!fTreeA->FindBranch(obj->GetName())) {
505 // Do the same as if we book via
506 // TTree::Branch(TCollection*)
508 fTreeA->Bronch(obj->GetName(), cname, fAODEvent->GetList()->GetObjectRef(obj),
509 kBufsize, kSplitlevel - 1);
510 // fTreeA->Branch(obj->GetName(), cname, addobj);
515 //______________________________________________________________________________
516 AliAODExtension *AliAODHandler::AddExtension(const char *filename, const char *title)
518 // Add an AOD extension with some branches in a different file.
519 TString fname(filename);
520 if (!fname.EndsWith(".root")) fname += ".root";
522 fExtensions = new TObjArray();
523 fExtensions->SetOwner();
525 AliAODExtension *ext = (AliAODExtension*)fExtensions->FindObject(fname);
527 ext = new AliAODExtension(fname, title);
528 fExtensions->Add(ext);
533 //______________________________________________________________________________
534 AliAODExtension *AliAODHandler::GetExtension(const char *filename) const
536 // Getter for AOD extensions via file name.
537 if (!fExtensions) return NULL;
538 return (AliAODExtension*)fExtensions->FindObject(filename);
541 //______________________________________________________________________________
542 AliAODExtension *AliAODHandler::AddFilteredAOD(const char *filename, const char *filtername)
544 // Add an AOD extension that can write only AOD events that pass a user filter.
546 fFilters = new TObjArray();
547 fFilters->SetOwner();
549 AliAODExtension *filter = (AliAODExtension*)fFilters->FindObject(filename);
551 filter = new AliAODExtension(filename, filtername, kTRUE);
552 fFilters->Add(filter);
557 //______________________________________________________________________________
558 AliAODExtension *AliAODHandler::GetFilteredAOD(const char *filename) const
560 // Getter for AOD filters via file name.
561 if (!fFilters) return NULL;
562 return (AliAODExtension*)fFilters->FindObject(filename);
565 //______________________________________________________________________________
566 void AliAODHandler::SetOutputFileName(const char* fname)
572 //______________________________________________________________________________
573 const char *AliAODHandler::GetOutputFileName()
576 return fFileName.Data();
579 //______________________________________________________________________________
580 void AliAODHandler::SetMCHeaderInfo(AliAODMCHeader *mcHeader,AliGenEventHeader *genHeader){
583 // Utility function to cover different cases for the AliGenEventHeader
584 // Needed since different ProcessType and ImpactParamter are not
585 // in the base class...
586 // We don't encode process types for event cocktails yet
587 // could be done e.g. by adding offsets depnding on the generator
589 mcHeader->AddGeneratorName(genHeader->GetName());
590 if(!genHeader)return;
591 AliGenPythiaEventHeader *pythiaGenHeader = dynamic_cast<AliGenPythiaEventHeader*>(genHeader);
592 if (pythiaGenHeader) {
593 mcHeader->SetEventType(pythiaGenHeader->ProcessType());
594 mcHeader->SetPtHard(pythiaGenHeader->GetPtHard());
598 AliGenDPMjetEventHeader* dpmJetGenHeader = dynamic_cast<AliGenDPMjetEventHeader*>(genHeader);
600 if (dpmJetGenHeader){
601 mcHeader->SetEventType(dpmJetGenHeader->ProcessType());
605 AliGenHijingEventHeader* hijingGenHeader = dynamic_cast<AliGenHijingEventHeader*>(genHeader);
607 mcHeader->SetImpactParameter(hijingGenHeader->ImpactParameter());
611 AliWarning(Form("MC Eventheader not known: %s",genHeader->GetName()));
615 ClassImp(AliAODExtension)
617 //-------------------------------------------------------------------------
618 // Support class for AOD extensions. This is created by the user analysis
619 // that requires a separate file for some AOD branches. The name of the
620 // AliAODExtension object is the file name where the AOD branches will be
622 //-------------------------------------------------------------------------
624 //______________________________________________________________________________
625 AliAODExtension::AliAODExtension(const char* name, const char* title, Bool_t isfilter)
636 TObject::SetBit(kFilteredAOD);
637 printf("####### Added AOD filter %s\n", name);
638 } else printf("####### Added AOD extension %s\n", name);
641 //______________________________________________________________________________
642 AliAODExtension::~AliAODExtension()
646 // is already handled in TerminateIO
652 if (fTreeE) delete fTreeE;
655 //______________________________________________________________________________
656 void AliAODExtension::AddBranch(const char* cname, void* addobj)
658 // Add a new branch to the aod
659 if (IsFilteredAOD()) {
660 Error("AddBranch", "Not allowed to add branched to filtered AOD's.");
665 gROOT->ProcessLine(Form("TString s_tmp; AliAnalysisManager::GetAnalysisManager()->GetAnalysisTypeString(s_tmp); sprintf((char*)0x%lx, \"%%s\", s_tmp.Data());", type));
668 TDirectory *owd = gDirectory;
672 char** apointer = (char**) addobj;
673 TObject* obj = (TObject*) *apointer;
675 fAODEvent->AddObject(obj);
677 const Int_t kSplitlevel = 99; // default value in TTree::Branch()
678 const Int_t kBufsize = 32000; // default value in TTree::Branch()
680 if (!fTreeE->FindBranch(obj->GetName())) {
681 // Do the same as if we book via
682 // TTree::Branch(TCollection*)
684 fTreeE->Bronch(obj->GetName(), cname, fAODEvent->GetList()->GetObjectRef(obj),
685 kBufsize, kSplitlevel - 1);
686 // fTreeA->Branch(obj->GetName(), cname, addobj);
691 //______________________________________________________________________________
692 Bool_t AliAODExtension::FinishEvent()
694 // Fill current event.
696 if (!IsFilteredAOD()) {
697 fAODEvent->MakeEntriesReferencable();
701 // Filtered AOD. Fill only if event is selected.
702 if (!fSelected) return kTRUE;
705 fSelected = kFALSE; // so that next event will not be selected unless demanded
709 //______________________________________________________________________________
710 Bool_t AliAODExtension::Init(Option_t *option)
713 if(!fAODEvent) fAODEvent = new AliAODEvent();
714 TDirectory *owd = gDirectory;
717 if (opt.Contains("proof")) {
719 // Merging via files. Need to access analysis manager via interpreter.
720 gROOT->ProcessLine(Form("AliAnalysisManager::GetAnalysisManager()->OpenProofFile(\"%s\", \"RECREATE\");", fName.Data()));
723 fFileE = new TFile(GetName(), "RECREATE");
725 fTreeE = new TTree("aodTree", "AliAOD tree");
726 fTreeE->Branch(fAODEvent->GetList());
732 //______________________________________________________________________________
733 void AliAODExtension::SetEvent(AliAODEvent *event)
735 // Connects to an external event
736 if (!IsFilteredAOD()) {
737 Error("SetEvent", "Not allowed to set external event for filtered AOD's");
740 // Use the copy constructor or assignment operator to synchronize with external event.
741 // AliAODEvent &other = *event;
742 // if (!fAODEvent) fAODEvent = new AliAODEvent(other);
743 // else if (fSelected) *fAODEvent = other;
747 //______________________________________________________________________________
748 Bool_t AliAODExtension::TerminateIO()
751 if (TObject::TestBit(kFilteredAOD))
752 printf("AOD Filter %s: events processed: %d passed: %d\n", GetName(), fNtotal, fNpassed);
754 printf("AOD extension %s: events processed: %d\n", GetName(), fNtotal);