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 fAODIsReplicated(kFALSE),
73 // default constructor
76 //______________________________________________________________________________
77 AliAODHandler::AliAODHandler(const char* name, const char* title):
78 AliVEventHandler(name, title),
81 fNeedsHeaderReplication(kFALSE),
82 fNeedsTracksBranchReplication(kFALSE),
83 fNeedsVerticesBranchReplication(kFALSE),
84 fNeedsV0sBranchReplication(kFALSE),
85 fNeedsTrackletsBranchReplication(kFALSE),
86 fNeedsPMDClustersBranchReplication(kFALSE),
87 fNeedsJetsBranchReplication(kFALSE),
88 fNeedsFMDClustersBranchReplication(kFALSE),
89 fNeedsCaloClustersBranchReplication(kFALSE),
90 fNeedsMCParticlesBranchReplication(kFALSE),
91 fAODIsReplicated(kFALSE),
101 //______________________________________________________________________________
102 AliAODHandler::~AliAODHandler()
107 // is already handled in TerminateIO
112 if (fExtensions) delete fExtensions;
115 //______________________________________________________________________________
116 Bool_t AliAODHandler::Init(Option_t* opt)
120 // Create the AODevent object
122 fAODEvent = new AliAODEvent();
123 if (fIsStandard) fAODEvent->CreateStdContent();
126 // File opening according to execution mode
129 TDirectory *owd = gDirectory;
130 if (option.Contains("proof")) {
132 // Merging via files. Need to access analysis manager via interpreter.
133 gROOT->ProcessLine(Form("AliAnalysisDataContainer *c_common_out = AliAnalysisManager::GetAnalysisManager()->GetCommonOutputContainer();"));
134 gROOT->ProcessLine(Form("AliAnalysisManager::GetAnalysisManager()->OpenProofFile(c_common_out, \"RECREATE\");"));
138 fFileA = new TFile(fFileName.Data(), "RECREATE");
143 TIter next(fExtensions);
144 AliAODExtension *ext;
145 while ((ext=(AliAODExtension*)next())) ext->Init(option);
150 //______________________________________________________________________________
151 void AliAODHandler::StoreMCParticles(){
154 // Remap the labels from ESD stack and store
155 // the AODMCParticles, makes only sense if we have
156 // the mcparticles branch
157 // has to be done here since we cannot know in advance
158 // which particles are needed (e.g. by the tracks etc.)
160 // Particles have been selected by AliMCEventhanlder->SelectParticle()
161 // To use the MCEventhandler here we need to set it from the outside
162 // can vanish when Handler go to the ANALYSISalice library
164 // The Branch booking for mcParticles and mcHeader has to happen
165 // in an external task for now since the AODHandler does not have access
166 // the AnalysisManager. For the same reason the pointer t o the MCEventH
167 // has to passed to the AOD Handler by this task
168 // (doing this in the steering macro would not work on PROOF)
170 TClonesArray *mcarray = (TClonesArray*)fAODEvent->FindListObject(AliAODMCParticle::StdBranchName());
174 AliAODMCHeader *mcHeader = (AliAODMCHeader*)fAODEvent->FindListObject(AliAODMCHeader::StdBranchName());
179 // Get the MC Infos.. Handler needs to be set before
180 // while adding the branch
181 // This needs to be done, not to depend on the AnalysisManager
183 if(!fMCEventH)return;
184 if(!fMCEventH->MCEvent())return;
185 AliStack *pStack = fMCEventH->MCEvent()->Stack();
188 fMCEventH->CreateLabelMap();
191 // Get the Event Header
194 AliHeader* header = fMCEventH->MCEvent()->Header();
196 AliGenEventHeader* genHeader = 0;
197 if (header) genHeader = header->GenEventHeader();
200 genHeader->PrimaryVertex(vtxMC);
201 mcHeader->SetVertex(vtxMC[0],vtxMC[1],vtxMC[2]);
203 // we search the MCEventHeaders first
204 // Two cases, cocktail or not...
205 AliGenCocktailEventHeader* genCocktailHeader = dynamic_cast<AliGenCocktailEventHeader*>(genHeader);
206 if(genCocktailHeader){
207 // we have a coktail header
208 mcHeader->AddGeneratorName(genHeader->GetName());
209 // Loop from the back so that the first one sets the process type
210 TList* headerList = genCocktailHeader->GetHeaders();
211 for(int i = headerList->GetEntries()-1;i>=0;--i){
212 AliGenEventHeader *headerEntry = dynamic_cast<AliGenEventHeader*>(headerList->At(i));
213 SetMCHeaderInfo(mcHeader,headerEntry);
217 // No Cocktail just take the first one
218 SetMCHeaderInfo(mcHeader,genHeader);
226 // Store the AliAODParticlesMC
227 AliMCEvent* mcEvent = fMCEventH->MCEvent();
229 Int_t np = mcEvent->GetNumberOfTracks();
230 Int_t nprim = mcEvent->GetNumberOfPrimaries();
234 TClonesArray& l = *mcarray;
236 for(int i = 0; i < np; ++i){
237 if(fMCEventH->IsParticleSelected(i)){
239 AliMCParticle* mcpart = (AliMCParticle*) mcEvent->GetTrack(i);
240 if(i<nprim)flag |= AliAODMCParticle::kPrimary;
242 if(mcEvent->IsPhysicalPrimary(i))flag |= AliAODMCParticle::kPhysicalPrim;
244 if(fMCEventH->GetNewLabel(i)!=j){
245 AliError(Form("MISMATCH New label %d j: %d",fMCEventH->GetNewLabel(i),j));
248 AliAODMCParticle mcpart_tmp(mcpart,i,flag);
251 Int_t d0 = mcpart_tmp.GetDaughter(0);
252 Int_t d1 = mcpart_tmp.GetDaughter(1);
253 Int_t m = mcpart_tmp.GetMother();
255 // other than for the track labels, negative values mean
256 // no daughter/mother so preserve it
259 // no first daughter -> no second daughter
260 // nothing to be done
261 // second condition not needed just for sanity check at the end
262 mcpart_tmp.SetDaughter(0,d0);
263 mcpart_tmp.SetDaughter(1,d1);
264 } else if(d1 < 0 && d0 >= 0) {
266 // second condition not needed just for sanity check at the end
267 if(fMCEventH->IsParticleSelected(d0)){
268 mcpart_tmp.SetDaughter(0,fMCEventH->GetNewLabel(d0));
270 mcpart_tmp.SetDaughter(0,-1);
272 mcpart_tmp.SetDaughter(1,d1);
274 else if (d0 > 0 && d1 > 0 ){
275 // we have two or more daughters loop on the stack to see if they are
279 for(int id = d0; id<=d1;++id){
280 if(fMCEventH->IsParticleSelected(id)){
283 d0_tmp = fMCEventH->GetNewLabel(id);
284 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
286 else d1_tmp = fMCEventH->GetNewLabel(id);
289 mcpart_tmp.SetDaughter(0,d0_tmp);
290 mcpart_tmp.SetDaughter(1,d1_tmp);
292 AliError(Form("Unxpected indices %d %d",d0,d1));
296 mcpart_tmp.SetMother(m);
298 if(fMCEventH->IsParticleSelected(m))mcpart_tmp.SetMother(fMCEventH->GetNewLabel(m));
299 else AliError(Form("PROBLEM Mother not selected %d \n", m));
302 new (l[j++]) AliAODMCParticle(mcpart_tmp);
306 AliInfo(Form("AliAODHandler::StoreMCParticles: Selected %d (Primaries %d / total %d) after validation \n",
309 // Set the labels in the AOD output...
313 TClonesArray* tracks = fAODEvent->GetTracks();
315 for(int it = 0; it < fAODEvent->GetNTracks();++it){
316 AliAODTrack *track = fAODEvent->GetTrack(it);
319 Int_t label = track->GetLabel();
320 if(label<0){ // preserve the sign for later usage
325 if (label >= AliMCEvent::BgLabelOffset()) label = mcEvent->BgLabelToIndex(label);
326 if(label > np || track->GetLabel() == 0){
327 AliWarning(Form("Wrong ESD track label %5d (%5d)",track->GetLabel(), label));
329 if(fMCEventH->GetNewLabel(label) == 0){
330 AliWarning(Form("New label not found for %5d (%5d)",track->GetLabel(), label));
332 track->SetLabel(sign*fMCEventH->GetNewLabel(label));
337 TClonesArray *clusters = fAODEvent->GetCaloClusters();
339 for (Int_t iClust = 0;iClust < fAODEvent->GetNCaloClusters(); ++iClust) {
340 AliAODCaloCluster * cluster = fAODEvent->GetCaloCluster(iClust);
341 UInt_t nLabel = cluster->GetNLabel();
342 // Ugly but do not want to fragment memory by creating
343 // new Int_t (nLabel)
344 Int_t* labels = const_cast<Int_t*>(cluster->GetLabels());
346 for(UInt_t i = 0;i < nLabel;++i){
347 labels[i] = fMCEventH->GetNewLabel(cluster->GetLabel(i));
350 // cluster->SetLabels(labels,nLabel);
355 AliAODTracklets *tracklets = fAODEvent->GetTracklets();
357 for(int it = 0;it < tracklets->GetNumberOfTracklets();++it){
358 int label0 = tracklets->GetLabel(it,0);
359 int label1 = tracklets->GetLabel(it,1);
360 if(label0>=0)label0 = fMCEventH->GetNewLabel(label0);
361 if(label1>=0)label1 = fMCEventH->GetNewLabel(label1);
362 tracklets->SetLabel(it,0,label0);
363 tracklets->SetLabel(it,1,label1);
369 //______________________________________________________________________________
370 Bool_t AliAODHandler::FinishEvent()
372 // Fill data structures
374 fAODEvent->MakeEntriesReferencable();
375 // StoreMCParticles();
378 TIter next(fExtensions);
379 AliAODExtension *ext;
380 while ((ext=(AliAODExtension*)next())) {
381 ext->GetAOD()->MakeEntriesReferencable();
382 ext->GetTree()->Fill();
387 if (fIsStandard) fAODEvent->ResetStd();
388 // Reset AOD replication flag
389 fAODIsReplicated = kFALSE;
393 //______________________________________________________________________________
394 Bool_t AliAODHandler::Terminate()
397 AddAODtoTreeUserInfo();
399 TIter next(fExtensions);
400 AliAODExtension *ext;
401 while ((ext=(AliAODExtension*)next())) ext->GetTree()->GetUserInfo()->Add(ext->GetAOD());
406 //______________________________________________________________________________
407 Bool_t AliAODHandler::TerminateIO()
416 TIter next(fExtensions);
417 AliAODExtension *ext;
418 while ((ext=(AliAODExtension*)next())) ext->TerminateIO();
423 //______________________________________________________________________________
424 void AliAODHandler::CreateTree(Int_t flag)
426 // Creates the AOD Tree
427 fTreeA = new TTree("aodTree", "AliAOD tree");
428 fTreeA->Branch(fAODEvent->GetList());
429 if (flag == 0) fTreeA->SetDirectory(0);
432 //______________________________________________________________________________
433 void AliAODHandler::FillTree()
439 //______________________________________________________________________________
440 void AliAODHandler::AddAODtoTreeUserInfo()
442 // Add aod event to tree user info
443 fTreeA->GetUserInfo()->Add(fAODEvent);
446 //______________________________________________________________________________
447 void AliAODHandler::AddBranch(const char* cname, void* addobj, const char* filename)
449 // Add a new branch to the aod. Added optional filename parameter if the
450 // branch should be written to a separate file.
451 if (strlen(filename)) {
452 AliAODExtension *ext = AddExtension(filename);
453 ext->AddBranch(cname, addobj);
456 TDirectory *owd = gDirectory;
460 char** apointer = (char**) addobj;
461 TObject* obj = (TObject*) *apointer;
463 fAODEvent->AddObject(obj);
465 const Int_t kSplitlevel = 99; // default value in TTree::Branch()
466 const Int_t kBufsize = 32000; // default value in TTree::Branch()
468 if (!fTreeA->FindBranch(obj->GetName())) {
469 // Do the same as if we book via
470 // TTree::Branch(TCollection*)
472 fTreeA->Bronch(obj->GetName(), cname, fAODEvent->GetList()->GetObjectRef(obj),
473 kBufsize, kSplitlevel - 1);
474 // fTreeA->Branch(obj->GetName(), cname, addobj);
479 //______________________________________________________________________________
480 AliAODExtension *AliAODHandler::AddExtension(const char *filename, const char *title)
482 // Add an AOD extension with some branches in a different file.
483 TString fname(filename);
484 if (!fname.EndsWith(".root")) fname += ".root";
486 fExtensions = new TObjArray();
487 fExtensions->SetOwner();
489 AliAODExtension *ext = (AliAODExtension*)fExtensions->FindObject(fname);
491 ext = new AliAODExtension(fname, title);
492 fExtensions->Add(ext);
497 //______________________________________________________________________________
498 void AliAODHandler::SetOutputFileName(const char* fname)
504 //______________________________________________________________________________
505 const char *AliAODHandler::GetOutputFileName()
508 return fFileName.Data();
511 //______________________________________________________________________________
512 void AliAODHandler::SetMCHeaderInfo(AliAODMCHeader *mcHeader,AliGenEventHeader *genHeader){
515 // Utility function to cover different cases for the AliGenEventHeader
516 // Needed since different ProcessType and ImpactParamter are not
517 // in the base class...
518 // We don't encode process types for event cocktails yet
519 // could be done e.g. by adding offsets depnding on the generator
521 mcHeader->AddGeneratorName(genHeader->GetName());
522 if(!genHeader)return;
523 AliGenPythiaEventHeader *pythiaGenHeader = dynamic_cast<AliGenPythiaEventHeader*>(genHeader);
524 if (pythiaGenHeader) {
525 mcHeader->SetEventType(pythiaGenHeader->ProcessType());
526 mcHeader->SetPtHard(pythiaGenHeader->GetPtHard());
530 AliGenDPMjetEventHeader* dpmJetGenHeader = dynamic_cast<AliGenDPMjetEventHeader*>(genHeader);
532 if (dpmJetGenHeader){
533 mcHeader->SetEventType(dpmJetGenHeader->ProcessType());
537 AliGenHijingEventHeader* hijingGenHeader = dynamic_cast<AliGenHijingEventHeader*>(genHeader);
539 mcHeader->SetImpactParameter(hijingGenHeader->ImpactParameter());
543 AliWarning(Form("MC Eventheader not known: %s",genHeader->GetName()));
547 ClassImp(AliAODExtension)
549 //-------------------------------------------------------------------------
550 // Support class for AOD extensions. This is created by the user analysis
551 // that requires a separate file for some AOD branches. The name of the
552 // AliAODExtension object is the file name where the AOD branches will be
554 //-------------------------------------------------------------------------
556 //______________________________________________________________________________
557 AliAODExtension::~AliAODExtension()
562 // is already handled in TerminateIO
569 //______________________________________________________________________________
570 void AliAODExtension::AddBranch(const char* cname, void* addobj)
572 // Add a new branch to the aod
575 gROOT->ProcessLine(Form("TString s_tmp; AliAnalysisManager::GetAnalysisManager()->GetAnalysisTypeString(s_tmp); sprintf((char*)0x%lx, \"%%s\", s_tmp.Data());", type));
578 TDirectory *owd = gDirectory;
582 char** apointer = (char**) addobj;
583 TObject* obj = (TObject*) *apointer;
585 fAODEvent->AddObject(obj);
587 const Int_t kSplitlevel = 99; // default value in TTree::Branch()
588 const Int_t kBufsize = 32000; // default value in TTree::Branch()
590 if (!fTreeE->FindBranch(obj->GetName())) {
591 // Do the same as if we book via
592 // TTree::Branch(TCollection*)
594 fTreeE->Bronch(obj->GetName(), cname, fAODEvent->GetList()->GetObjectRef(obj),
595 kBufsize, kSplitlevel - 1);
596 // fTreeA->Branch(obj->GetName(), cname, addobj);
601 //______________________________________________________________________________
602 Bool_t AliAODExtension::Init(Option_t *option)
605 if(!fAODEvent) fAODEvent = new AliAODEvent();
606 TDirectory *owd = gDirectory;
609 if (opt.Contains("proof")) {
611 // Merging via files. Need to access analysis manager via interpreter.
612 gROOT->ProcessLine(Form("AliAnalysisManager::GetAnalysisManager()->OpenProofFile(\"%s\", \"RECREATE\");", fName.Data()));
615 fFileE = new TFile(GetName(), "RECREATE");
617 fTreeE = new TTree("aodTree", "AliAOD tree");
618 fTreeE->Branch(fAODEvent->GetList());
623 //______________________________________________________________________________
624 Bool_t AliAODExtension::TerminateIO()