Addition to the analysis framework (Andrei, Jan Fiete):
[u/mrichter/AliRoot.git] / STEER / STEER / AliMCGenHandler.cxx
CommitLineData
205cbea7 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
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 **************************************************************************/
15
16/* $Id: AliMCGenHandler.cxx 64675 2013-10-23 12:21:37Z hristov $ */
17
18//-------------------------------------------------------------------------
19// Class AliMCGenHandler
20// This class can be used with the analysis framework to generate event on
21// the fly and analyse them.
22//
23// Origin: Andrei Gheata, Jan Fiete Grosse-Oetringhaus
24//-------------------------------------------------------------------------
25
26#include "AliMCGenHandler.h"
27#include "AliMCEvent.h"
28#include "AliPDG.h"
29#include "AliHeader.h"
30#include "AliStack.h"
31#include "AliLog.h"
32#include "AliGenerator.h"
33#include "AliRunLoader.h"
34#include "AliRun.h"
35#include "AliAnalysisManager.h"
36
37ClassImp(AliMCGenHandler)
38
39AliMCGenHandler::AliMCGenHandler() :
40 AliInputEventHandler(),
41 fMCEvent(0),
42 fEventNumber(0),
43 fStack(0),
44 fHeader(0),
45 fGenerator(0),
46 fSeedMode(0)
47{
48 //
49 // Default constructor
50 //
51 // Be sure to add all particles to the PDG database
52 AliPDG::AddParticlesToPdgDataBase();
53}
54
55AliMCGenHandler::AliMCGenHandler(const char* name, const char* title) :
56 AliInputEventHandler(name, title),
57 fMCEvent(0),
58 fEventNumber(0),
59 fStack(0),
60 fHeader(0),
61 fGenerator(0),
62 fSeedMode(0)
63{
64 //
65 // Constructor
66 //
67 // Be sure to add all particles to the PDG database
68 AliPDG::AddParticlesToPdgDataBase();
69}
70
71AliMCGenHandler::~AliMCGenHandler()
72{
73 // Destructor
74 delete fMCEvent;
75 delete fGenerator;
76}
77
78Bool_t AliMCGenHandler::Init(Option_t* /*opt*/)
79{
80 // Initialize input
81 //
82
83 if (!fGenerator)
84 AliFatal("fGenerator needs to be set before");
85
86 if (fSeedMode == 0)
87 Printf("AliMCGenHandler::Init: Not setting any seed. Seed needs to be set externally!");
88 else if (fSeedMode == 1)
89 {
90 Printf("AliMCGenHandler::Init: Taking seed from current time");
91 gRandom->SetSeed(time(0));
92 }
93 else if (fSeedMode == 2)
94 {
95 Printf("AliMCGenHandler::Init: Taking seed from AliEn job id");
96 TString tmp(gSystem->Getenv("ALIEN_PROC_ID"));
97 UInt_t seed = tmp.Atoi();
98 if (tmp.Length() == 0 || seed == 0)
99 AliFatal(Form("Could not retrieve AliEn job id for seed. The variable ALIEN_PROC_ID contains %s", tmp.Data()));
100 gRandom->SetSeed(seed);
101 }
102 else
103 AliFatal(Form("Seed mode %d unknown", fSeedMode));
104
105 AliRunLoader* rl = AliRunLoader::Open("galice.root","FASTRUN","recreate");
106 rl->MakeTree("E");
107 gAlice->SetRunLoader(rl);
108 rl->MakeStack();
109 fStack = rl->Stack();
110 fHeader = rl->GetHeader();
111
112 fGenerator->SetStack(fStack);
113 fGenerator->Init();
114
115 fMCEvent = new AliMCEvent;
116
117 return kTRUE;
118}
119
120Bool_t AliMCGenHandler::BeginEvent(Long64_t /*entry*/)
121{
122 // Begin event
123
124 fHeader->Reset(0, fEventNumber++);
125
126 if (AliAnalysisManager::GetAnalysisManager()->GetDebugLevel() > 1)
127 Printf("AliMCGenHandler::BeginEvent: Generating Event number %lld", fEventNumber);
128
129 fGenerator->Generate();
130
131 fHeader->SetStack(fStack);
132 fHeader->SetNprimary(fStack->GetNprimary());
133 fHeader->SetNtrack(fStack->GetNtrack());
134
135 fMCEvent->ConnectHeaderAndStack(fHeader);
136
137 return kTRUE;
138}
139
140Bool_t AliMCGenHandler::FinishEvent()
141{
142 // Clean-up after each event
143
144 fMCEvent->Stack()->Reset();
145
146 return kTRUE;
147}