1 /**************************************************************************
2 * Copyright(c) 1998-2007, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
18 //-------------------------------------------------------------------------
19 // Implementation of the Virtual Event Handler Interface for AOD
20 // Author: Andreas Morsch, CERN
21 //-------------------------------------------------------------------------
30 #include "AliAODHandler.h"
31 #include "AliAODEvent.h"
32 #include "AliAODTracklets.h"
34 #include "AliAODMCParticle.h"
35 #include "AliAODMCHeader.h"
36 #include "AliMCEventHandler.h"
37 #include "AliMCEvent.h"
38 #include "AliGenEventHeader.h"
39 #include "AliGenHijingEventHeader.h"
40 #include "AliGenDPMjetEventHeader.h"
41 #include "AliGenPythiaEventHeader.h"
42 #include "AliGenCocktailEventHeader.h"
46 ClassImp(AliAODHandler)
48 //______________________________________________________________________________
49 AliAODHandler::AliAODHandler() :
53 fNeedsHeaderReplication(kFALSE),
54 fNeedsTracksBranchReplication(kFALSE),
55 fNeedsVerticesBranchReplication(kFALSE),
56 fNeedsV0sBranchReplication(kFALSE),
57 fNeedsTrackletsBranchReplication(kFALSE),
58 fNeedsPMDClustersBranchReplication(kFALSE),
59 fNeedsJetsBranchReplication(kFALSE),
60 fNeedsFMDClustersBranchReplication(kFALSE),
61 fNeedsCaloClustersBranchReplication(kFALSE),
62 fAODIsReplicated(kFALSE),
69 // default constructor
72 //______________________________________________________________________________
73 AliAODHandler::AliAODHandler(const char* name, const char* title):
74 AliVEventHandler(name, title),
77 fNeedsHeaderReplication(kFALSE),
78 fNeedsTracksBranchReplication(kFALSE),
79 fNeedsVerticesBranchReplication(kFALSE),
80 fNeedsV0sBranchReplication(kFALSE),
81 fNeedsTrackletsBranchReplication(kFALSE),
82 fNeedsPMDClustersBranchReplication(kFALSE),
83 fNeedsJetsBranchReplication(kFALSE),
84 fNeedsFMDClustersBranchReplication(kFALSE),
85 fNeedsCaloClustersBranchReplication(kFALSE),
86 fAODIsReplicated(kFALSE),
95 //______________________________________________________________________________
96 AliAODHandler::~AliAODHandler()
100 // is already handled in TerminateIO
108 //______________________________________________________________________________
109 Bool_t AliAODHandler::Init(Option_t* opt)
113 // Create the AODevent object
115 fAODEvent = new AliAODEvent();
116 if (fIsStandard) fAODEvent->CreateStdContent();
119 // File opening according to execution mode
122 if (option.Contains("proof")) {
124 if (option.Contains("special")) {
125 // File for tree already opened on slave -> merging via files
134 TDirectory *owd = gDirectory;
135 fFileA = new TFile(fFileName.Data(), "RECREATE");
143 void AliAODHandler::StoreMCParticles(){
146 // Remap the labels from ESD stack and store
147 // the AODMCParticles, makes only sense if we have
148 // the mcparticles branch
149 // has to be done here since we cannot know in advance
150 // which particles are needed (e.g. by the tracks etc.)
152 // Particles have been selected by AliMCEventhanlder->SelectParticle()
153 // To use the MCEventhandler here we need to set it from the outside
154 // can vanish when Handler go to the ANALYSISalice library
156 // The Branch booking for mcParticles and mcHeader has to happen
157 // in an external task for now since the AODHandler does not have access
158 // the AnalysisManager. For the same reason the pointer t o the MCEventH
159 // has to passed to the AOD Handler by this task
160 // (doing this in the steering macro would not work on PROOF)
162 TClonesArray *mcarray = (TClonesArray*)fAODEvent->FindListObject(AliAODMCParticle::StdBranchName());
166 AliAODMCHeader *mcHeader = (AliAODMCHeader*)fAODEvent->FindListObject(AliAODMCHeader::StdBranchName());
171 // Get the MC Infos.. Handler needs to be set before
172 // while adding the branch
173 // This needs to be done, not to depend on the AnalysisManager
175 if(!fMCEventH)return;
176 if(!fMCEventH->MCEvent())return;
177 AliStack *pStack = fMCEventH->MCEvent()->Stack();
180 fMCEventH->CreateLabelMap();
183 // Get the Event Header
186 AliHeader* header = fMCEventH->MCEvent()->Header();
190 AliGenEventHeader* genHeader = header->GenEventHeader();
192 genHeader->PrimaryVertex(vtxMC);
193 mcHeader->SetVertex(vtxMC[0],vtxMC[1],vtxMC[2]);
195 // we search the MCEventHeaders first
196 // Two cases, cocktail or not...
197 AliGenCocktailEventHeader* genCocktailHeader = dynamic_cast<AliGenCocktailEventHeader*>(genHeader);
198 if(genCocktailHeader){
199 // we have a coktail header
200 mcHeader->AddGeneratorName(genHeader->GetName());
201 // Loop from the back so that the first one sets the process type
202 TList* headerList = genCocktailHeader->GetHeaders();
203 for(int i = headerList->GetEntries()-1;i>=0;--i){
204 AliGenEventHeader *headerEntry = dynamic_cast<AliGenEventHeader*>(headerList->At(i));
205 SetMCHeaderInfo(mcHeader,headerEntry);
209 // No Cocktail just take the first one
210 SetMCHeaderInfo(mcHeader,genHeader);
216 // Store the AliAODParticlesMC
218 Int_t np = pStack->GetNtrack();
219 Int_t nprim = pStack->GetNprimary();
223 TClonesArray& l = *mcarray;
225 for(int i = 0;i < np;++i){
226 if(fMCEventH->IsParticleSelected(i)){
229 TParticle *part = pStack->Particle(i);
230 if(i<nprim)flag |= AliAODMCParticle::kPrimary;
231 if(pStack->IsPhysicalPrimary(i))flag |= AliAODMCParticle::kPhysicalPrim;
233 if(fMCEventH->GetNewLabel(i)!=j){
234 AliError(Form("MISMATCH New label %d j: %d",fMCEventH->GetNewLabel(i),j));
236 AliAODMCParticle mcpart_tmp(part,i,flag);
239 Int_t d0 = mcpart_tmp.GetDaughter(0);
240 Int_t d1 = mcpart_tmp.GetDaughter(1);
241 Int_t m = mcpart_tmp.GetMother();
243 // other than for the track labels, negative values mean
244 // no daughter/mother so preserve it
247 // no first daughter -> no second daughter
248 // nothing to be done
249 // second condition not needed just for sanity check at the end
250 mcpart_tmp.SetDaughter(0,d0);
251 mcpart_tmp.SetDaughter(1,d1);
253 else if(d1 < 0 && d0 >= 0){
255 // second condition not needed just for sanity check at the end
256 if(fMCEventH->IsParticleSelected(d0)){
257 mcpart_tmp.SetDaughter(0,fMCEventH->GetNewLabel(d0));
260 mcpart_tmp.SetDaughter(0,-1);
262 mcpart_tmp.SetDaughter(1,d1);
264 else if (d0 > 0 && d1 > 0 ){
265 // we have two or more daughters loop on the stack to see if they are
269 for(int id = d0; id<=d1;++id){
270 if(fMCEventH->IsParticleSelected(id)){
273 d0_tmp = fMCEventH->GetNewLabel(id);
274 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
276 else d1_tmp = fMCEventH->GetNewLabel(id);
279 mcpart_tmp.SetDaughter(0,d0_tmp);
280 mcpart_tmp.SetDaughter(1,d1_tmp);
283 AliError(Form("Unxpected indices %d %d",d0,d1));
287 mcpart_tmp.SetMother(m);
290 if(fMCEventH->IsParticleSelected(m))mcpart_tmp.SetMother(fMCEventH->GetNewLabel(m));
291 else AliError("PROBLEM Mother not selected");
294 new (l[j++]) AliAODMCParticle(mcpart_tmp);
298 AliInfo(Form("AliAODHandler::StoreMCParticles: Selected %d (Primaries %d / total %d) after validation",
301 // Set the labels in the AOD output...
305 TClonesArray* tracks = fAODEvent->GetTracks();
307 for(int it = 0; it < fAODEvent->GetNTracks();++it){
308 AliAODTrack *track = fAODEvent->GetTrack(it);
310 if(TMath::Abs(track->GetLabel())>np||track->GetLabel()==0){
311 AliWarning(Form("Wrong ESD track label %d",track->GetLabel()));
313 if(fMCEventH->GetNewLabel(track->GetLabel())==0){
314 AliWarning(Form("New label not found for %d",track->GetLabel()));
316 track->SetLabel(fMCEventH->GetNewLabel(track->GetLabel()));
321 TClonesArray *clusters = fAODEvent->GetCaloClusters();
323 for (Int_t iClust = 0;iClust < fAODEvent->GetNCaloClusters(); ++iClust) {
324 AliAODCaloCluster * cluster = fAODEvent->GetCaloCluster(iClust);
325 UInt_t nLabel = cluster->GetNLabel();
326 // Ugly but do not want to fragment memory by creating
327 // new Int_t (nLabel)
328 Int_t* labels = const_cast<Int_t*>(cluster->GetLabels());
330 for(UInt_t i = 0;i < nLabel;++i){
331 labels[i] = fMCEventH->GetNewLabel(cluster->GetLabel(i));
334 // cluster->SetLabels(labels,nLabel);
339 AliAODTracklets *tracklets = fAODEvent->GetTracklets();
341 for(int it = 0;it < tracklets->GetNumberOfTracklets();++it){
342 int label0 = tracklets->GetLabel(it,0);
343 int label1 = tracklets->GetLabel(it,1);
344 if(label0>=0)label0 = fMCEventH->GetNewLabel(label0);
345 if(label1>=0)label1 = fMCEventH->GetNewLabel(label1);
346 tracklets->SetLabel(it,0,label0);
347 tracklets->SetLabel(it,1,label1);
353 Bool_t AliAODHandler::FinishEvent()
355 // Fill data structures
357 fAODEvent->MakeEntriesReferencable();
362 if (fIsStandard) fAODEvent->ResetStd();
363 // Reset AOD replication flag
364 fAODIsReplicated = kFALSE;
368 //______________________________________________________________________________
369 Bool_t AliAODHandler::Terminate()
372 AddAODtoTreeUserInfo();
376 //______________________________________________________________________________
377 Bool_t AliAODHandler::TerminateIO()
387 //______________________________________________________________________________
388 void AliAODHandler::CreateTree(Int_t flag)
390 // Creates the AOD Tree
391 fTreeA = new TTree("aodTree", "AliAOD tree");
392 fTreeA->Branch(fAODEvent->GetList());
393 if (flag == 0) fTreeA->SetDirectory(0);
396 //______________________________________________________________________________
397 void AliAODHandler::FillTree()
403 //______________________________________________________________________________
404 void AliAODHandler::AddAODtoTreeUserInfo()
406 // Add aod event to tree user info
407 fTreeA->GetUserInfo()->Add(fAODEvent);
410 //______________________________________________________________________________
411 void AliAODHandler::AddBranch(const char* cname, void* addobj)
413 // Add a new branch to the aod
414 TDirectory *owd = gDirectory;
418 char** apointer = (char**) addobj;
419 TObject* obj = (TObject*) *apointer;
421 fAODEvent->AddObject(obj);
423 const Int_t kSplitlevel = 99; // default value in TTree::Branch()
424 const Int_t kBufsize = 32000; // default value in TTree::Branch()
426 if (!fTreeA->FindBranch(obj->GetName())) {
427 // Do the same as if we book via
428 // TTree::Branch(TCollection*)
430 fTreeA->Bronch(obj->GetName(), cname, fAODEvent->GetList()->GetObjectRef(obj),
431 kBufsize, kSplitlevel - 1);
432 // fTreeA->Branch(obj->GetName(), cname, addobj);
437 //______________________________________________________________________________
438 void AliAODHandler::SetOutputFileName(const char* fname)
444 //______________________________________________________________________________
445 const char *AliAODHandler::GetOutputFileName()
448 return fFileName.Data();
451 void AliAODHandler::SetMCHeaderInfo(AliAODMCHeader *mcHeader,AliGenEventHeader *genHeader){
454 // Utility function to cover different cases for the AliGenEventHeader
455 // Needed since different ProcessType and ImpactParamter are not
456 // in the base class...
457 // We don't encode process types for event cocktails yet
458 // coul be done e.g. by adding offsets depnding on the generator
461 mcHeader->AddGeneratorName(genHeader->GetName());
465 if(!genHeader)return;
466 AliGenPythiaEventHeader *pythiaGenHeader = dynamic_cast<AliGenPythiaEventHeader*>(genHeader);
467 if (pythiaGenHeader) {
468 mcHeader->SetEventType(pythiaGenHeader->ProcessType());
472 AliGenDPMjetEventHeader* dpmJetGenHeader = dynamic_cast<AliGenDPMjetEventHeader*>(genHeader);
474 if (dpmJetGenHeader){
475 mcHeader->SetEventType(dpmJetGenHeader->ProcessType());
479 AliGenHijingEventHeader* hijingGenHeader = dynamic_cast<AliGenHijingEventHeader*>(genHeader);
481 mcHeader->SetImpactParameter(hijingGenHeader->ImpactParameter());
485 AliWarning(Form("MC Eventheader not known: %s",genHeader->GetName()));