]> git.uio.no Git - u/mrichter/AliRoot.git/blame_incremental - STEER/AliAODHandler.cxx
Temp.protection against undefined beam type in Bfield init
[u/mrichter/AliRoot.git] / STEER / AliAODHandler.cxx
... / ...
CommitLineData
1
2/**************************************************************************
3 * Copyright(c) 1998-2007, ALICE Experiment at CERN, All rights reserved. *
4 * *
5 * Author: The ALICE Off-line Project. *
6 * Contributors are mentioned in the code where appropriate. *
7 * *
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 **************************************************************************/
16
17/* $Id$ */
18
19//-------------------------------------------------------------------------
20// Implementation of the Virtual Event Handler Interface for AOD
21// Author: Andreas Morsch, CERN
22//-------------------------------------------------------------------------
23
24
25#include <TTree.h>
26#include <TFile.h>
27#include <TString.h>
28#include <TList.h>
29#include <TROOT.h>
30
31#include "AliLog.h"
32#include "AliAODHandler.h"
33#include "AliAODEvent.h"
34#include "AliAODTracklets.h"
35#include "AliStack.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"
45
46
47
48ClassImp(AliAODHandler)
49
50//______________________________________________________________________________
51AliAODHandler::AliAODHandler() :
52 AliVEventHandler(),
53 fIsStandard(kTRUE),
54 fFillAOD(kTRUE),
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),
65 fAODEvent(NULL),
66 fMCEventH(NULL),
67 fTreeA(NULL),
68 fFileA(NULL),
69 fFileName(""),
70 fExtensions(NULL)
71{
72 // default constructor
73}
74
75//______________________________________________________________________________
76AliAODHandler::AliAODHandler(const char* name, const char* title):
77 AliVEventHandler(name, title),
78 fIsStandard(kTRUE),
79 fFillAOD(kTRUE),
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),
90 fAODEvent(NULL),
91 fMCEventH(NULL),
92 fTreeA(NULL),
93 fFileA(NULL),
94 fFileName(""),
95 fExtensions(NULL)
96{
97}
98
99//______________________________________________________________________________
100AliAODHandler::~AliAODHandler()
101{
102 // Destructor.
103 delete fAODEvent;
104 if(fFileA){
105 // is already handled in TerminateIO
106 fFileA->Close();
107 delete fFileA;
108 }
109 delete fTreeA;
110 if (fExtensions) delete fExtensions;
111}
112
113//______________________________________________________________________________
114Bool_t AliAODHandler::Init(Option_t* opt)
115{
116 // Initialize IO
117 //
118 // Create the AODevent object
119 if(!fAODEvent){
120 fAODEvent = new AliAODEvent();
121 if (fIsStandard) fAODEvent->CreateStdContent();
122 }
123 //
124 // File opening according to execution mode
125 TString option(opt);
126 option.ToLower();
127 TDirectory *owd = gDirectory;
128 if (option.Contains("proof")) {
129 // 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));
133 fFileA = gFile;
134 } else {
135 // local and grid
136 fFileA = new TFile(fFileName.Data(), "RECREATE");
137 }
138 CreateTree(1);
139 owd->cd();
140 if (fExtensions) {
141 TIter next(fExtensions);
142 AliAODExtension *ext;
143 while ((ext=(AliAODExtension*)next())) ext->Init(option);
144 }
145 return kTRUE;
146}
147
148//______________________________________________________________________________
149void AliAODHandler::StoreMCParticles(){
150
151 //
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.)
157 //
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
161 //
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)
167
168 TClonesArray *mcarray = (TClonesArray*)fAODEvent->FindListObject(AliAODMCParticle::StdBranchName());
169 if(!mcarray)return;
170 mcarray->Delete();
171
172 AliAODMCHeader *mcHeader = (AliAODMCHeader*)fAODEvent->FindListObject(AliAODMCHeader::StdBranchName());
173 if(!mcHeader)return;
174
175 mcHeader->Reset();
176
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
180
181 if(!fMCEventH)return;
182 if(!fMCEventH->MCEvent())return;
183 AliStack *pStack = fMCEventH->MCEvent()->Stack();
184 if(!pStack)return;
185
186 fMCEventH->CreateLabelMap();
187
188 //
189 // Get the Event Header
190 //
191
192 AliHeader* header = fMCEventH->MCEvent()->Header();
193 if (!header)return;
194
195 // get the MC vertex
196 AliGenEventHeader* genHeader = header->GenEventHeader();
197 if (!genHeader) return;
198
199 TArrayF vtxMC(3);
200 genHeader->PrimaryVertex(vtxMC);
201 mcHeader->SetVertex(vtxMC[0],vtxMC[1],vtxMC[2]);
202
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);
214 }
215 }
216 else{
217 // No Cocktail just take the first one
218 SetMCHeaderInfo(mcHeader,genHeader);
219 }
220
221
222
223
224 // Store the AliAODParticlesMC
225 AliMCEvent* mcEvent = fMCEventH->MCEvent();
226
227 Int_t np = mcEvent->GetNumberOfTracks();
228 Int_t nprim = mcEvent->GetNumberOfPrimaries();
229
230
231 Int_t j = 0;
232 TClonesArray& l = *mcarray;
233
234 for(int i = 0; i < np; ++i){
235 if(fMCEventH->IsParticleSelected(i)){
236 Int_t flag = 0;
237 AliMCParticle* mcpart = (AliMCParticle*) mcEvent->GetTrack(i);
238 if(i<nprim)flag |= AliAODMCParticle::kPrimary;
239
240 if(mcEvent->IsPhysicalPrimary(i))flag |= AliAODMCParticle::kPhysicalPrim;
241
242 if(fMCEventH->GetNewLabel(i)!=j){
243 AliError(Form("MISMATCH New label %d j: %d",fMCEventH->GetNewLabel(i),j));
244 }
245
246 AliAODMCParticle mcpart_tmp(mcpart,i,flag);
247
248 //
249 Int_t d0 = mcpart_tmp.GetDaughter(0);
250 Int_t d1 = mcpart_tmp.GetDaughter(1);
251 Int_t m = mcpart_tmp.GetMother();
252
253 // other than for the track labels, negative values mean
254 // no daughter/mother so preserve it
255
256 if(d0<0 && d1<0){
257 // no first daughter -> no second daughter
258 // nothing to be done
259 // second condition not needed just for sanity check at the end
260 mcpart_tmp.SetDaughter(0,d0);
261 mcpart_tmp.SetDaughter(1,d1);
262 } else if(d1 < 0 && d0 >= 0) {
263 // Only one daughter
264 // second condition not needed just for sanity check at the end
265 if(fMCEventH->IsParticleSelected(d0)){
266 mcpart_tmp.SetDaughter(0,fMCEventH->GetNewLabel(d0));
267 } else {
268 mcpart_tmp.SetDaughter(0,-1);
269 }
270 mcpart_tmp.SetDaughter(1,d1);
271 }
272 else if (d0 > 0 && d1 > 0 ){
273 // we have two or more daughters loop on the stack to see if they are
274 // selected
275 Int_t d0_tmp = -1;
276 Int_t d1_tmp = -1;
277 for(int id = d0; id<=d1;++id){
278 if(fMCEventH->IsParticleSelected(id)){
279 if(d0_tmp==-1){
280 // first time
281 d0_tmp = fMCEventH->GetNewLabel(id);
282 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
283 }
284 else d1_tmp = fMCEventH->GetNewLabel(id);
285 }
286 }
287 mcpart_tmp.SetDaughter(0,d0_tmp);
288 mcpart_tmp.SetDaughter(1,d1_tmp);
289 } else {
290 AliError(Form("Unxpected indices %d %d",d0,d1));
291 }
292
293 if(m<0){
294 mcpart_tmp.SetMother(m);
295 } else {
296 if(fMCEventH->IsParticleSelected(m))mcpart_tmp.SetMother(fMCEventH->GetNewLabel(m));
297 else AliError(Form("PROBLEM Mother not selected %d \n", m));
298 }
299
300 new (l[j++]) AliAODMCParticle(mcpart_tmp);
301
302 }
303 }
304 AliInfo(Form("AliAODHandler::StoreMCParticles: Selected %d (Primaries %d / total %d) after validation \n",
305 j,nprim,np));
306
307 // Set the labels in the AOD output...
308 // Remapping
309
310 // AODTracks
311 TClonesArray* tracks = fAODEvent->GetTracks();
312 if(tracks){
313 for(int it = 0; it < fAODEvent->GetNTracks();++it){
314 AliAODTrack *track = fAODEvent->GetTrack(it);
315
316 Int_t label = TMath::Abs(track->GetLabel());
317 if (label >= AliMCEvent::BgLabelOffset()) label = mcEvent->BgLabelToIndex(label);
318
319
320 if(label > np || track->GetLabel() == 0){
321 AliWarning(Form("Wrong ESD track label %5d (%5d)",track->GetLabel(), label));
322 }
323 if(fMCEventH->GetNewLabel(label) == 0){
324 AliWarning(Form("New label not found for %5d (%5d)",track->GetLabel(), label));
325 }
326 track->SetLabel(fMCEventH->GetNewLabel(label));
327 }
328 }
329
330 // AOD calo cluster
331 TClonesArray *clusters = fAODEvent->GetCaloClusters();
332 if(clusters){
333 for (Int_t iClust = 0;iClust < fAODEvent->GetNCaloClusters(); ++iClust) {
334 AliAODCaloCluster * cluster = fAODEvent->GetCaloCluster(iClust);
335 UInt_t nLabel = cluster->GetNLabel();
336 // Ugly but do not want to fragment memory by creating
337 // new Int_t (nLabel)
338 Int_t* labels = const_cast<Int_t*>(cluster->GetLabels());
339 if (labels){
340 for(UInt_t i = 0;i < nLabel;++i){
341 labels[i] = fMCEventH->GetNewLabel(cluster->GetLabel(i));
342 }
343 }
344 // cluster->SetLabels(labels,nLabel);
345 }// iClust
346 }// clusters
347
348 // AOD tracklets
349 AliAODTracklets *tracklets = fAODEvent->GetTracklets();
350 if(tracklets){
351 for(int it = 0;it < tracklets->GetNumberOfTracklets();++it){
352 int label0 = tracklets->GetLabel(it,0);
353 int label1 = tracklets->GetLabel(it,1);
354 if(label0>=0)label0 = fMCEventH->GetNewLabel(label0);
355 if(label1>=0)label1 = fMCEventH->GetNewLabel(label1);
356 tracklets->SetLabel(it,0,label0);
357 tracklets->SetLabel(it,1,label1);
358 }
359 }
360
361}
362
363//______________________________________________________________________________
364Bool_t AliAODHandler::FinishEvent()
365{
366 // Fill data structures
367 if(fFillAOD){
368 fAODEvent->MakeEntriesReferencable();
369 StoreMCParticles();
370 FillTree();
371 if (fExtensions) {
372 TIter next(fExtensions);
373 AliAODExtension *ext;
374 while ((ext=(AliAODExtension*)next())) {
375 ext->GetAOD()->MakeEntriesReferencable();
376 ext->GetTree()->Fill();
377 }
378 }
379 }
380
381 if (fIsStandard) fAODEvent->ResetStd();
382 // Reset AOD replication flag
383 fAODIsReplicated = kFALSE;
384 return kTRUE;
385}
386
387//______________________________________________________________________________
388Bool_t AliAODHandler::Terminate()
389{
390 // Terminate
391 AddAODtoTreeUserInfo();
392 if (fExtensions) {
393 TIter next(fExtensions);
394 AliAODExtension *ext;
395 while ((ext=(AliAODExtension*)next())) ext->GetTree()->GetUserInfo()->Add(ext->GetAOD());
396 }
397 return kTRUE;
398}
399
400//______________________________________________________________________________
401Bool_t AliAODHandler::TerminateIO()
402{
403 // Terminate IO
404 if (fFileA) {
405 fFileA->Close();
406 delete fFileA;
407 fFileA = 0;
408 }
409 if (fExtensions) {
410 TIter next(fExtensions);
411 AliAODExtension *ext;
412 while ((ext=(AliAODExtension*)next())) ext->TerminateIO();
413 }
414 return kTRUE;
415}
416
417//______________________________________________________________________________
418void AliAODHandler::CreateTree(Int_t flag)
419{
420 // Creates the AOD Tree
421 fTreeA = new TTree("aodTree", "AliAOD tree");
422 fTreeA->Branch(fAODEvent->GetList());
423 if (flag == 0) fTreeA->SetDirectory(0);
424}
425
426//______________________________________________________________________________
427void AliAODHandler::FillTree()
428{
429 // Fill the AOD Tree
430 fTreeA->Fill();
431}
432
433//______________________________________________________________________________
434void AliAODHandler::AddAODtoTreeUserInfo()
435{
436 // Add aod event to tree user info
437 fTreeA->GetUserInfo()->Add(fAODEvent);
438}
439
440//______________________________________________________________________________
441void AliAODHandler::AddBranch(const char* cname, void* addobj, const char* filename)
442{
443 // Add a new branch to the aod. Added optional filename parameter if the
444 // branch should be written to a separate file.
445 if (strlen(filename)) {
446 AliAODExtension *ext = AddExtension(filename);
447 ext->AddBranch(cname, addobj);
448 return;
449 }
450 TDirectory *owd = gDirectory;
451 if (fFileA) {
452 fFileA->cd();
453 }
454 char** apointer = (char**) addobj;
455 TObject* obj = (TObject*) *apointer;
456
457 fAODEvent->AddObject(obj);
458
459 const Int_t kSplitlevel = 99; // default value in TTree::Branch()
460 const Int_t kBufsize = 32000; // default value in TTree::Branch()
461
462 if (!fTreeA->FindBranch(obj->GetName())) {
463 // Do the same as if we book via
464 // TTree::Branch(TCollection*)
465
466 fTreeA->Bronch(obj->GetName(), cname, fAODEvent->GetList()->GetObjectRef(obj),
467 kBufsize, kSplitlevel - 1);
468 // fTreeA->Branch(obj->GetName(), cname, addobj);
469 }
470 owd->cd();
471}
472
473//______________________________________________________________________________
474AliAODExtension *AliAODHandler::AddExtension(const char *filename, const char *title)
475{
476// Add an AOD extension with some branches in a different file.
477 TString fname(filename);
478 if (!fname.EndsWith(".root")) fname += ".root";
479 if (!fExtensions) {
480 fExtensions = new TObjArray();
481 fExtensions->SetOwner();
482 }
483 AliAODExtension *ext = (AliAODExtension*)fExtensions->FindObject(fname);
484 if (!ext) {
485 ext = new AliAODExtension(fname, title);
486 fExtensions->Add(ext);
487 }
488 return ext;
489}
490
491//______________________________________________________________________________
492void AliAODHandler::SetOutputFileName(const char* fname)
493{
494// Set file name.
495 fFileName = fname;
496}
497
498//______________________________________________________________________________
499const char *AliAODHandler::GetOutputFileName()
500{
501// Get file name.
502 return fFileName.Data();
503}
504
505//______________________________________________________________________________
506void AliAODHandler::SetMCHeaderInfo(AliAODMCHeader *mcHeader,AliGenEventHeader *genHeader){
507
508
509 // Utility function to cover different cases for the AliGenEventHeader
510 // Needed since different ProcessType and ImpactParamter are not
511 // in the base class...
512 // We don't encode process types for event cocktails yet
513 // could be done e.g. by adding offsets depnding on the generator
514
515 mcHeader->AddGeneratorName(genHeader->GetName());
516 if(!genHeader)return;
517 AliGenPythiaEventHeader *pythiaGenHeader = dynamic_cast<AliGenPythiaEventHeader*>(genHeader);
518 if (pythiaGenHeader) {
519 mcHeader->SetEventType(pythiaGenHeader->ProcessType());
520 mcHeader->SetPtHard(pythiaGenHeader->GetPtHard());
521 return;
522 }
523
524 AliGenDPMjetEventHeader* dpmJetGenHeader = dynamic_cast<AliGenDPMjetEventHeader*>(genHeader);
525
526 if (dpmJetGenHeader){
527 mcHeader->SetEventType(dpmJetGenHeader->ProcessType());
528 return;
529 }
530
531 AliGenHijingEventHeader* hijingGenHeader = dynamic_cast<AliGenHijingEventHeader*>(genHeader);
532 if(hijingGenHeader){
533 mcHeader->SetImpactParameter(hijingGenHeader->ImpactParameter());
534 return;
535 }
536
537 AliWarning(Form("MC Eventheader not known: %s",genHeader->GetName()));
538
539}
540
541ClassImp(AliAODExtension)
542
543//-------------------------------------------------------------------------
544// Support class for AOD extensions. This is created by the user analysis
545// that requires a separate file for some AOD branches. The name of the
546// AliAODExtension object is the file name where the AOD branches will be
547// stored.
548//-------------------------------------------------------------------------
549
550//______________________________________________________________________________
551AliAODExtension::~AliAODExtension()
552{
553// Destructor.
554 delete fAODEvent;
555 if(fFileE){
556 // is already handled in TerminateIO
557 fFileE->Close();
558 delete fFileE;
559 }
560 delete fTreeE;
561}
562
563//______________________________________________________________________________
564void AliAODExtension::AddBranch(const char* cname, void* addobj)
565{
566 // Add a new branch to the aod
567 if (!fAODEvent) {
568 char type[20];
569 gROOT->ProcessLine(Form("TString s_tmp; AliAnalysisManager::GetAnalysisManager()->GetAnalysisTypeString(s_tmp); sprintf((char*)0x%lx, \"%%s\", s_tmp.Data());", type));
570 Init(type);
571 }
572 TDirectory *owd = gDirectory;
573 if (fFileE) {
574 fFileE->cd();
575 }
576 char** apointer = (char**) addobj;
577 TObject* obj = (TObject*) *apointer;
578
579 fAODEvent->AddObject(obj);
580
581 const Int_t kSplitlevel = 99; // default value in TTree::Branch()
582 const Int_t kBufsize = 32000; // default value in TTree::Branch()
583
584 if (!fTreeE->FindBranch(obj->GetName())) {
585 // Do the same as if we book via
586 // TTree::Branch(TCollection*)
587
588 fTreeE->Bronch(obj->GetName(), cname, fAODEvent->GetList()->GetObjectRef(obj),
589 kBufsize, kSplitlevel - 1);
590 // fTreeA->Branch(obj->GetName(), cname, addobj);
591 }
592 owd->cd();
593}
594
595//______________________________________________________________________________
596Bool_t AliAODExtension::Init(Option_t *option)
597{
598// Initialize IO.
599 if(!fAODEvent) fAODEvent = new AliAODEvent();
600 TDirectory *owd = gDirectory;
601 TString opt(option);
602 opt.ToLower();
603 if (opt.Contains("proof")) {
604 // proof
605 // Merging via files. Need to access analysis manager via interpreter.
606 gROOT->ProcessLine(Form("AliAnalysisManager::GetAnalysisManager()->OpenProofFile(\"%s\", \"RECREATE\");", fName.Data()));
607 fFileE = gFile;
608 } else {
609 fFileE = new TFile(GetName(), "RECREATE");
610 }
611 fTreeE = new TTree("aodTree", "AliAOD tree");
612 fTreeE->Branch(fAODEvent->GetList());
613 owd->cd();
614 return kTRUE;
615}
616
617//______________________________________________________________________________
618Bool_t AliAODExtension::TerminateIO()
619{
620 // Terminate IO
621 if (fFileE) {
622 fFileE->Write();
623 fFileE->Close();
624 delete fFileE;
625 fFileE = 0;
626 }
627 return kTRUE;
628}