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 fAODIsReplicated(kFALSE),
72 // default constructor
75 //______________________________________________________________________________
76 AliAODHandler::AliAODHandler(const char* name, const char* title):
77 AliVEventHandler(name, title),
80 fNeedsHeaderReplication(kFALSE),
81 fNeedsTracksBranchReplication(kFALSE),
82 fNeedsVerticesBranchReplication(kFALSE),
83 fNeedsV0sBranchReplication(kFALSE),
84 fNeedsTrackletsBranchReplication(kFALSE),
85 fNeedsPMDClustersBranchReplication(kFALSE),
86 fNeedsJetsBranchReplication(kFALSE),
87 fNeedsFMDClustersBranchReplication(kFALSE),
88 fNeedsCaloClustersBranchReplication(kFALSE),
89 fAODIsReplicated(kFALSE),
99 //______________________________________________________________________________
100 AliAODHandler::~AliAODHandler()
105 // is already handled in TerminateIO
110 if (fExtensions) delete fExtensions;
113 //______________________________________________________________________________
114 Bool_t AliAODHandler::Init(Option_t* opt)
118 // Create the AODevent object
120 fAODEvent = new AliAODEvent();
121 if (fIsStandard) fAODEvent->CreateStdContent();
124 // File opening according to execution mode
127 TDirectory *owd = gDirectory;
128 if (option.Contains("proof")) {
130 // Merging via files. Need to access analysis manager via interpreter.
131 gROOT->ProcessLine(Form("AliAnalysisManager::GetAnalysisManager()->OpenProofFile(\"%s\", \"RECREATE\");", fFileName.Data()));
132 gROOT->ProcessLine(Form("AliAnalysisManager::GetAnalysisManager()->GetCommonOutputContainer()->SetFile((TFile*)0x%lx);", gFile));
136 fFileA = new TFile(fFileName.Data(), "RECREATE");
141 TIter next(fExtensions);
142 AliAODExtension *ext;
143 while ((ext=(AliAODExtension*)next())) ext->Init(option);
148 //______________________________________________________________________________
149 void AliAODHandler::StoreMCParticles(){
152 // Remap the labels from ESD stack and store
153 // the AODMCParticles, makes only sense if we have
154 // the mcparticles branch
155 // has to be done here since we cannot know in advance
156 // which particles are needed (e.g. by the tracks etc.)
158 // Particles have been selected by AliMCEventhanlder->SelectParticle()
159 // To use the MCEventhandler here we need to set it from the outside
160 // can vanish when Handler go to the ANALYSISalice library
162 // The Branch booking for mcParticles and mcHeader has to happen
163 // in an external task for now since the AODHandler does not have access
164 // the AnalysisManager. For the same reason the pointer t o the MCEventH
165 // has to passed to the AOD Handler by this task
166 // (doing this in the steering macro would not work on PROOF)
168 TClonesArray *mcarray = (TClonesArray*)fAODEvent->FindListObject(AliAODMCParticle::StdBranchName());
172 AliAODMCHeader *mcHeader = (AliAODMCHeader*)fAODEvent->FindListObject(AliAODMCHeader::StdBranchName());
177 // Get the MC Infos.. Handler needs to be set before
178 // while adding the branch
179 // This needs to be done, not to depend on the AnalysisManager
181 if(!fMCEventH)return;
182 if(!fMCEventH->MCEvent())return;
183 AliStack *pStack = fMCEventH->MCEvent()->Stack();
186 fMCEventH->CreateLabelMap();
189 // Get the Event Header
192 AliHeader* header = fMCEventH->MCEvent()->Header();
196 AliGenEventHeader* genHeader = header->GenEventHeader();
198 genHeader->PrimaryVertex(vtxMC);
199 mcHeader->SetVertex(vtxMC[0],vtxMC[1],vtxMC[2]);
201 // we search the MCEventHeaders first
202 // Two cases, cocktail or not...
203 AliGenCocktailEventHeader* genCocktailHeader = dynamic_cast<AliGenCocktailEventHeader*>(genHeader);
204 if(genCocktailHeader){
205 // we have a coktail header
206 mcHeader->AddGeneratorName(genHeader->GetName());
207 // Loop from the back so that the first one sets the process type
208 TList* headerList = genCocktailHeader->GetHeaders();
209 for(int i = headerList->GetEntries()-1;i>=0;--i){
210 AliGenEventHeader *headerEntry = dynamic_cast<AliGenEventHeader*>(headerList->At(i));
211 SetMCHeaderInfo(mcHeader,headerEntry);
215 // No Cocktail just take the first one
216 SetMCHeaderInfo(mcHeader,genHeader);
222 // Store the AliAODParticlesMC
223 AliMCEvent* mcEvent = fMCEventH->MCEvent();
225 Int_t np = mcEvent->GetNumberOfTracks();
226 Int_t nprim = mcEvent->GetNumberOfPrimaries();
230 TClonesArray& l = *mcarray;
232 for(int i = 0; i < np; ++i){
233 if(fMCEventH->IsParticleSelected(i)){
235 AliMCParticle* mcpart = mcEvent->GetTrack(i);
236 if(i<nprim)flag |= AliAODMCParticle::kPrimary;
238 if(mcEvent->IsPhysicalPrimary(i))flag |= AliAODMCParticle::kPhysicalPrim;
240 if(fMCEventH->GetNewLabel(i)!=j){
241 AliError(Form("MISMATCH New label %d j: %d",fMCEventH->GetNewLabel(i),j));
244 AliAODMCParticle mcpart_tmp(mcpart,i,flag);
247 Int_t d0 = mcpart_tmp.GetDaughter(0);
248 Int_t d1 = mcpart_tmp.GetDaughter(1);
249 Int_t m = mcpart_tmp.GetMother();
251 // other than for the track labels, negative values mean
252 // no daughter/mother so preserve it
255 // no first daughter -> no second daughter
256 // nothing to be done
257 // second condition not needed just for sanity check at the end
258 mcpart_tmp.SetDaughter(0,d0);
259 mcpart_tmp.SetDaughter(1,d1);
260 } else if(d1 < 0 && d0 >= 0) {
262 // second condition not needed just for sanity check at the end
263 if(fMCEventH->IsParticleSelected(d0)){
264 mcpart_tmp.SetDaughter(0,fMCEventH->GetNewLabel(d0));
266 mcpart_tmp.SetDaughter(0,-1);
268 mcpart_tmp.SetDaughter(1,d1);
270 else if (d0 > 0 && d1 > 0 ){
271 // we have two or more daughters loop on the stack to see if they are
275 for(int id = d0; id<=d1;++id){
276 if(fMCEventH->IsParticleSelected(id)){
279 d0_tmp = fMCEventH->GetNewLabel(id);
280 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
282 else d1_tmp = fMCEventH->GetNewLabel(id);
285 mcpart_tmp.SetDaughter(0,d0_tmp);
286 mcpart_tmp.SetDaughter(1,d1_tmp);
288 AliError(Form("Unxpected indices %d %d",d0,d1));
292 mcpart_tmp.SetMother(m);
294 if(fMCEventH->IsParticleSelected(m))mcpart_tmp.SetMother(fMCEventH->GetNewLabel(m));
295 else AliError(Form("PROBLEM Mother not selected %d \n", m));
298 new (l[j++]) AliAODMCParticle(mcpart_tmp);
302 AliInfo(Form("AliAODHandler::StoreMCParticles: Selected %d (Primaries %d / total %d) after validation \n",
305 // Set the labels in the AOD output...
309 TClonesArray* tracks = fAODEvent->GetTracks();
311 for(int it = 0; it < fAODEvent->GetNTracks();++it){
312 AliAODTrack *track = fAODEvent->GetTrack(it);
314 Int_t label = TMath::Abs(track->GetLabel());
315 if (label >= AliMCEvent::BgLabelOffset()) label = mcEvent->BgLabelToIndex(label);
318 if(label > np || track->GetLabel() == 0){
319 AliWarning(Form("Wrong ESD track label %5d (%5d)",track->GetLabel(), label));
321 if(fMCEventH->GetNewLabel(label) == 0){
322 AliWarning(Form("New label not found for %5d (%5d)",track->GetLabel(), label));
324 track->SetLabel(fMCEventH->GetNewLabel(label));
329 TClonesArray *clusters = fAODEvent->GetCaloClusters();
331 for (Int_t iClust = 0;iClust < fAODEvent->GetNCaloClusters(); ++iClust) {
332 AliAODCaloCluster * cluster = fAODEvent->GetCaloCluster(iClust);
333 UInt_t nLabel = cluster->GetNLabel();
334 // Ugly but do not want to fragment memory by creating
335 // new Int_t (nLabel)
336 Int_t* labels = const_cast<Int_t*>(cluster->GetLabels());
338 for(UInt_t i = 0;i < nLabel;++i){
339 labels[i] = fMCEventH->GetNewLabel(cluster->GetLabel(i));
342 // cluster->SetLabels(labels,nLabel);
347 AliAODTracklets *tracklets = fAODEvent->GetTracklets();
349 for(int it = 0;it < tracklets->GetNumberOfTracklets();++it){
350 int label0 = tracklets->GetLabel(it,0);
351 int label1 = tracklets->GetLabel(it,1);
352 if(label0>=0)label0 = fMCEventH->GetNewLabel(label0);
353 if(label1>=0)label1 = fMCEventH->GetNewLabel(label1);
354 tracklets->SetLabel(it,0,label0);
355 tracklets->SetLabel(it,1,label1);
361 //______________________________________________________________________________
362 Bool_t AliAODHandler::FinishEvent()
364 // Fill data structures
366 fAODEvent->MakeEntriesReferencable();
370 TIter next(fExtensions);
371 AliAODExtension *ext;
372 while ((ext=(AliAODExtension*)next())) {
373 ext->GetAOD()->MakeEntriesReferencable();
374 ext->GetTree()->Fill();
379 if (fIsStandard) fAODEvent->ResetStd();
380 // Reset AOD replication flag
381 fAODIsReplicated = kFALSE;
385 //______________________________________________________________________________
386 Bool_t AliAODHandler::Terminate()
389 AddAODtoTreeUserInfo();
391 TIter next(fExtensions);
392 AliAODExtension *ext;
393 while ((ext=(AliAODExtension*)next())) ext->GetTree()->GetUserInfo()->Add(ext->GetAOD());
398 //______________________________________________________________________________
399 Bool_t AliAODHandler::TerminateIO()
408 TIter next(fExtensions);
409 AliAODExtension *ext;
410 while ((ext=(AliAODExtension*)next())) ext->TerminateIO();
415 //______________________________________________________________________________
416 void AliAODHandler::CreateTree(Int_t flag)
418 // Creates the AOD Tree
419 fTreeA = new TTree("aodTree", "AliAOD tree");
420 fTreeA->Branch(fAODEvent->GetList());
421 if (flag == 0) fTreeA->SetDirectory(0);
424 //______________________________________________________________________________
425 void AliAODHandler::FillTree()
431 //______________________________________________________________________________
432 void AliAODHandler::AddAODtoTreeUserInfo()
434 // Add aod event to tree user info
435 fTreeA->GetUserInfo()->Add(fAODEvent);
438 //______________________________________________________________________________
439 void AliAODHandler::AddBranch(const char* cname, void* addobj, const char* filename)
441 // Add a new branch to the aod. Added optional filename parameter if the
442 // branch should be written to a separate file.
443 if (strlen(filename)) {
444 AliAODExtension *ext = AddExtension(filename);
445 ext->AddBranch(cname, addobj);
448 TDirectory *owd = gDirectory;
452 char** apointer = (char**) addobj;
453 TObject* obj = (TObject*) *apointer;
455 fAODEvent->AddObject(obj);
457 const Int_t kSplitlevel = 99; // default value in TTree::Branch()
458 const Int_t kBufsize = 32000; // default value in TTree::Branch()
460 if (!fTreeA->FindBranch(obj->GetName())) {
461 // Do the same as if we book via
462 // TTree::Branch(TCollection*)
464 fTreeA->Bronch(obj->GetName(), cname, fAODEvent->GetList()->GetObjectRef(obj),
465 kBufsize, kSplitlevel - 1);
466 // fTreeA->Branch(obj->GetName(), cname, addobj);
471 //______________________________________________________________________________
472 AliAODExtension *AliAODHandler::AddExtension(const char *filename, const char *title)
474 // Add an AOD extension with some branches in a different file.
475 TString fname(filename);
476 if (!fname.EndsWith(".root")) fname += ".root";
478 fExtensions = new TObjArray();
479 fExtensions->SetOwner();
481 AliAODExtension *ext = (AliAODExtension*)fExtensions->FindObject(fname);
483 ext = new AliAODExtension(fname, title);
484 fExtensions->Add(ext);
489 //______________________________________________________________________________
490 void AliAODHandler::SetOutputFileName(const char* fname)
496 //______________________________________________________________________________
497 const char *AliAODHandler::GetOutputFileName()
500 return fFileName.Data();
503 //______________________________________________________________________________
504 void AliAODHandler::SetMCHeaderInfo(AliAODMCHeader *mcHeader,AliGenEventHeader *genHeader){
507 // Utility function to cover different cases for the AliGenEventHeader
508 // Needed since different ProcessType and ImpactParamter are not
509 // in the base class...
510 // We don't encode process types for event cocktails yet
511 // could be done e.g. by adding offsets depnding on the generator
513 mcHeader->AddGeneratorName(genHeader->GetName());
514 if(!genHeader)return;
515 AliGenPythiaEventHeader *pythiaGenHeader = dynamic_cast<AliGenPythiaEventHeader*>(genHeader);
516 if (pythiaGenHeader) {
517 mcHeader->SetEventType(pythiaGenHeader->ProcessType());
518 mcHeader->SetPtHard(pythiaGenHeader->GetPtHard());
522 AliGenDPMjetEventHeader* dpmJetGenHeader = dynamic_cast<AliGenDPMjetEventHeader*>(genHeader);
524 if (dpmJetGenHeader){
525 mcHeader->SetEventType(dpmJetGenHeader->ProcessType());
529 AliGenHijingEventHeader* hijingGenHeader = dynamic_cast<AliGenHijingEventHeader*>(genHeader);
531 mcHeader->SetImpactParameter(hijingGenHeader->ImpactParameter());
535 AliWarning(Form("MC Eventheader not known: %s",genHeader->GetName()));
539 ClassImp(AliAODExtension)
541 //-------------------------------------------------------------------------
542 // Support class for AOD extensions. This is created by the user analysis
543 // that requires a separate file for some AOD branches. The name of the
544 // AliAODExtension object is the file name where the AOD branches will be
546 //-------------------------------------------------------------------------
548 //______________________________________________________________________________
549 AliAODExtension::~AliAODExtension()
554 // is already handled in TerminateIO
561 //______________________________________________________________________________
562 void AliAODExtension::AddBranch(const char* cname, void* addobj)
564 // Add a new branch to the aod
567 gROOT->ProcessLine(Form("TString s_tmp; AliAnalysisManager::GetAnalysisManager()->GetAnalysisTypeString(s_tmp); sprintf((char*)0x%lx, \"%%s\", s_tmp.Data());", type));
570 TDirectory *owd = gDirectory;
574 char** apointer = (char**) addobj;
575 TObject* obj = (TObject*) *apointer;
577 fAODEvent->AddObject(obj);
579 const Int_t kSplitlevel = 99; // default value in TTree::Branch()
580 const Int_t kBufsize = 32000; // default value in TTree::Branch()
582 if (!fTreeE->FindBranch(obj->GetName())) {
583 // Do the same as if we book via
584 // TTree::Branch(TCollection*)
586 fTreeE->Bronch(obj->GetName(), cname, fAODEvent->GetList()->GetObjectRef(obj),
587 kBufsize, kSplitlevel - 1);
588 // fTreeA->Branch(obj->GetName(), cname, addobj);
593 //______________________________________________________________________________
594 Bool_t AliAODExtension::Init(Option_t *option)
597 if(!fAODEvent) fAODEvent = new AliAODEvent();
598 TDirectory *owd = gDirectory;
601 if (opt.Contains("proof")) {
603 // Merging via files. Need to access analysis manager via interpreter.
604 gROOT->ProcessLine(Form("AliAnalysisManager::GetAnalysisManager()->OpenProofFile(\"%s\", \"RECREATE\");", fName.Data()));
607 fFileE = new TFile(GetName(), "RECREATE");
609 fTreeE = new TTree("aodTree", "AliAOD tree");
610 fTreeE->Branch(fAODEvent->GetList());
615 //______________________________________________________________________________
616 Bool_t AliAODExtension::TerminateIO()