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("AliAnalysisManager::GetAnalysisManager()->OpenProofFile(\"%s\", \"RECREATE\");", fFileName.Data()));
134 gROOT->ProcessLine(Form("AliAnalysisManager::GetAnalysisManager()->GetCommonOutputContainer()->SetFile((TFile*)0x%lx);", gFile));
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);
318 Int_t label = TMath::Abs(track->GetLabel());
319 if (label >= AliMCEvent::BgLabelOffset()) label = mcEvent->BgLabelToIndex(label);
322 if(label > np || track->GetLabel() == 0){
323 AliWarning(Form("Wrong ESD track label %5d (%5d)",track->GetLabel(), label));
325 if(fMCEventH->GetNewLabel(label) == 0){
326 AliWarning(Form("New label not found for %5d (%5d)",track->GetLabel(), label));
328 track->SetLabel(fMCEventH->GetNewLabel(label));
333 TClonesArray *clusters = fAODEvent->GetCaloClusters();
335 for (Int_t iClust = 0;iClust < fAODEvent->GetNCaloClusters(); ++iClust) {
336 AliAODCaloCluster * cluster = fAODEvent->GetCaloCluster(iClust);
337 UInt_t nLabel = cluster->GetNLabel();
338 // Ugly but do not want to fragment memory by creating
339 // new Int_t (nLabel)
340 Int_t* labels = const_cast<Int_t*>(cluster->GetLabels());
342 for(UInt_t i = 0;i < nLabel;++i){
343 labels[i] = fMCEventH->GetNewLabel(cluster->GetLabel(i));
346 // cluster->SetLabels(labels,nLabel);
351 AliAODTracklets *tracklets = fAODEvent->GetTracklets();
353 for(int it = 0;it < tracklets->GetNumberOfTracklets();++it){
354 int label0 = tracklets->GetLabel(it,0);
355 int label1 = tracklets->GetLabel(it,1);
356 if(label0>=0)label0 = fMCEventH->GetNewLabel(label0);
357 if(label1>=0)label1 = fMCEventH->GetNewLabel(label1);
358 tracklets->SetLabel(it,0,label0);
359 tracklets->SetLabel(it,1,label1);
365 //______________________________________________________________________________
366 Bool_t AliAODHandler::FinishEvent()
368 // Fill data structures
370 fAODEvent->MakeEntriesReferencable();
374 TIter next(fExtensions);
375 AliAODExtension *ext;
376 while ((ext=(AliAODExtension*)next())) {
377 ext->GetAOD()->MakeEntriesReferencable();
378 ext->GetTree()->Fill();
383 if (fIsStandard) fAODEvent->ResetStd();
384 // Reset AOD replication flag
385 fAODIsReplicated = kFALSE;
389 //______________________________________________________________________________
390 Bool_t AliAODHandler::Terminate()
393 AddAODtoTreeUserInfo();
395 TIter next(fExtensions);
396 AliAODExtension *ext;
397 while ((ext=(AliAODExtension*)next())) ext->GetTree()->GetUserInfo()->Add(ext->GetAOD());
402 //______________________________________________________________________________
403 Bool_t AliAODHandler::TerminateIO()
412 TIter next(fExtensions);
413 AliAODExtension *ext;
414 while ((ext=(AliAODExtension*)next())) ext->TerminateIO();
419 //______________________________________________________________________________
420 void AliAODHandler::CreateTree(Int_t flag)
422 // Creates the AOD Tree
423 fTreeA = new TTree("aodTree", "AliAOD tree");
424 fTreeA->Branch(fAODEvent->GetList());
425 if (flag == 0) fTreeA->SetDirectory(0);
428 //______________________________________________________________________________
429 void AliAODHandler::FillTree()
435 //______________________________________________________________________________
436 void AliAODHandler::AddAODtoTreeUserInfo()
438 // Add aod event to tree user info
439 fTreeA->GetUserInfo()->Add(fAODEvent);
442 //______________________________________________________________________________
443 void AliAODHandler::AddBranch(const char* cname, void* addobj, const char* filename)
445 // Add a new branch to the aod. Added optional filename parameter if the
446 // branch should be written to a separate file.
447 if (strlen(filename)) {
448 AliAODExtension *ext = AddExtension(filename);
449 ext->AddBranch(cname, addobj);
452 TDirectory *owd = gDirectory;
456 char** apointer = (char**) addobj;
457 TObject* obj = (TObject*) *apointer;
459 fAODEvent->AddObject(obj);
461 const Int_t kSplitlevel = 99; // default value in TTree::Branch()
462 const Int_t kBufsize = 32000; // default value in TTree::Branch()
464 if (!fTreeA->FindBranch(obj->GetName())) {
465 // Do the same as if we book via
466 // TTree::Branch(TCollection*)
468 fTreeA->Bronch(obj->GetName(), cname, fAODEvent->GetList()->GetObjectRef(obj),
469 kBufsize, kSplitlevel - 1);
470 // fTreeA->Branch(obj->GetName(), cname, addobj);
475 //______________________________________________________________________________
476 AliAODExtension *AliAODHandler::AddExtension(const char *filename, const char *title)
478 // Add an AOD extension with some branches in a different file.
479 TString fname(filename);
480 if (!fname.EndsWith(".root")) fname += ".root";
482 fExtensions = new TObjArray();
483 fExtensions->SetOwner();
485 AliAODExtension *ext = (AliAODExtension*)fExtensions->FindObject(fname);
487 ext = new AliAODExtension(fname, title);
488 fExtensions->Add(ext);
493 //______________________________________________________________________________
494 void AliAODHandler::SetOutputFileName(const char* fname)
500 //______________________________________________________________________________
501 const char *AliAODHandler::GetOutputFileName()
504 return fFileName.Data();
507 //______________________________________________________________________________
508 void AliAODHandler::SetMCHeaderInfo(AliAODMCHeader *mcHeader,AliGenEventHeader *genHeader){
511 // Utility function to cover different cases for the AliGenEventHeader
512 // Needed since different ProcessType and ImpactParamter are not
513 // in the base class...
514 // We don't encode process types for event cocktails yet
515 // could be done e.g. by adding offsets depnding on the generator
517 mcHeader->AddGeneratorName(genHeader->GetName());
518 if(!genHeader)return;
519 AliGenPythiaEventHeader *pythiaGenHeader = dynamic_cast<AliGenPythiaEventHeader*>(genHeader);
520 if (pythiaGenHeader) {
521 mcHeader->SetEventType(pythiaGenHeader->ProcessType());
522 mcHeader->SetPtHard(pythiaGenHeader->GetPtHard());
526 AliGenDPMjetEventHeader* dpmJetGenHeader = dynamic_cast<AliGenDPMjetEventHeader*>(genHeader);
528 if (dpmJetGenHeader){
529 mcHeader->SetEventType(dpmJetGenHeader->ProcessType());
533 AliGenHijingEventHeader* hijingGenHeader = dynamic_cast<AliGenHijingEventHeader*>(genHeader);
535 mcHeader->SetImpactParameter(hijingGenHeader->ImpactParameter());
539 AliWarning(Form("MC Eventheader not known: %s",genHeader->GetName()));
543 ClassImp(AliAODExtension)
545 //-------------------------------------------------------------------------
546 // Support class for AOD extensions. This is created by the user analysis
547 // that requires a separate file for some AOD branches. The name of the
548 // AliAODExtension object is the file name where the AOD branches will be
550 //-------------------------------------------------------------------------
552 //______________________________________________________________________________
553 AliAODExtension::~AliAODExtension()
558 // is already handled in TerminateIO
565 //______________________________________________________________________________
566 void AliAODExtension::AddBranch(const char* cname, void* addobj)
568 // Add a new branch to the aod
571 gROOT->ProcessLine(Form("TString s_tmp; AliAnalysisManager::GetAnalysisManager()->GetAnalysisTypeString(s_tmp); sprintf((char*)0x%lx, \"%%s\", s_tmp.Data());", type));
574 TDirectory *owd = gDirectory;
578 char** apointer = (char**) addobj;
579 TObject* obj = (TObject*) *apointer;
581 fAODEvent->AddObject(obj);
583 const Int_t kSplitlevel = 99; // default value in TTree::Branch()
584 const Int_t kBufsize = 32000; // default value in TTree::Branch()
586 if (!fTreeE->FindBranch(obj->GetName())) {
587 // Do the same as if we book via
588 // TTree::Branch(TCollection*)
590 fTreeE->Bronch(obj->GetName(), cname, fAODEvent->GetList()->GetObjectRef(obj),
591 kBufsize, kSplitlevel - 1);
592 // fTreeA->Branch(obj->GetName(), cname, addobj);
597 //______________________________________________________________________________
598 Bool_t AliAODExtension::Init(Option_t *option)
601 if(!fAODEvent) fAODEvent = new AliAODEvent();
602 TDirectory *owd = gDirectory;
605 if (opt.Contains("proof")) {
607 // Merging via files. Need to access analysis manager via interpreter.
608 gROOT->ProcessLine(Form("AliAnalysisManager::GetAnalysisManager()->OpenProofFile(\"%s\", \"RECREATE\");", fName.Data()));
611 fFileE = new TFile(GetName(), "RECREATE");
613 fTreeE = new TTree("aodTree", "AliAOD tree");
614 fTreeE->Branch(fAODEvent->GetList());
619 //______________________________________________________________________________
620 Bool_t AliAODExtension::TerminateIO()