]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/STEER/AliMCGenHandler.cxx
results of prev. event reco were not reset in chain processing mode
[u/mrichter/AliRoot.git] / STEER / STEER / AliMCGenHandler.cxx
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
37 #include "TMacro.h"
38 #include "TInterpreter.h"
39
40 ClassImp(AliMCGenHandler)
41
42 AliMCGenHandler::AliMCGenHandler() :
43     AliInputEventHandler(),
44     fMCEvent(0),
45     fEventNumber(0),
46     fStack(0),
47     fHeader(0),
48     fGenerator(0),
49     fSeedMode(0),
50     fSeed(0),
51     fGeneratorMacroPath(),
52     fGeneratorMacroParameters(),
53     fGeneratorCustomization(0)
54 {
55   //
56   // Default constructor
57   //
58   // Be sure to add all particles to the PDG database
59   AliPDG::AddParticlesToPdgDataBase();
60 }
61
62 AliMCGenHandler::AliMCGenHandler(const char* name, const char* title) :
63     AliInputEventHandler(name, title),
64     fMCEvent(0),
65     fEventNumber(0),
66     fStack(0),
67     fHeader(0),
68     fGenerator(0),
69     fSeedMode(0),
70     fSeed(0),
71     fGeneratorMacroPath(),
72     fGeneratorMacroParameters(),
73     fGeneratorCustomization(0)
74 {
75   //
76   // Constructor
77   //
78   // Be sure to add all particles to the PDG database
79   AliPDG::AddParticlesToPdgDataBase();
80 }
81
82 AliMCGenHandler::~AliMCGenHandler()
83
84     // Destructor
85     delete fMCEvent;
86     delete fGenerator;
87 }
88
89 Bool_t AliMCGenHandler::Init(Option_t* /*opt*/)
90
91   // Initialize input
92     //
93     
94     if (!fGenerator) {
95       if (fGeneratorMacroPath.Length() == 0)
96         AliFatal("fGeneratorMacroPath empty!");
97       
98       TString macroPath;
99       macroPath.Form("$ALICE_ROOT/%s", fGeneratorMacroPath.Data());
100       macroPath = gSystem->ExpandPathName(macroPath.Data());
101
102       if (gSystem->AccessPathName(macroPath))
103           AliFatal(Form("Cannot find macro %s", macroPath.Data()));
104
105       TMacro m(macroPath);
106       Int_t error = 0;
107       Long64_t retval = m.Exec(fGeneratorMacroParameters, &error);
108       if (error != TInterpreter::kNoError)
109           AliFatal(Form("Macro interpretation %s failed", macroPath.Data()));
110
111       if (retval<0)
112         AliFatal(Form("The macro %s did not return a valid generator (1)", macroPath.Data()));
113
114       fGenerator = reinterpret_cast<AliGenerator*>(retval);
115       if (!fGenerator)
116         AliFatal(Form("The macro %s did not return a valid generator (2)", macroPath.Data()));
117
118       // customization from LEGO train
119       if (fGeneratorCustomization) {
120         fGeneratorCustomization->Exec(Form("(AliGenerator*) %p", fGenerator), &error);
121         if (error != TInterpreter::kNoError)
122           AliFatal("Execution of generator customization failed");
123       }
124     }
125     
126     if (fSeedMode == 0)
127       Printf("AliMCGenHandler::Init: Not setting any seed. Seed needs to be set externally!");
128     else
129     {
130       if (fSeedMode == 1)
131       {
132         Printf("AliMCGenHandler::Init: Using manually set seed");
133       }
134       else if (fSeedMode == 2)
135       {
136         Printf("AliMCGenHandler::Init: Taking seed from current time");
137         fSeed = time(0);
138       }
139       else if (fSeedMode == 3)
140       {
141         Printf("AliMCGenHandler::Init: Taking seed from AliEn job id");
142         TString tmp(gSystem->Getenv("ALIEN_PROC_ID"));
143         fSeed = tmp.Atoi();
144         if (tmp.Length() == 0 || fSeed == 0)
145           AliFatal(Form("Could not retrieve AliEn job id for seed. The variable ALIEN_PROC_ID contains %s", tmp.Data()));
146       }
147       else
148         AliFatal(Form("Seed mode %d unknown", fSeedMode));
149
150       Printf("AliMCGenHandler::Init: Using seed: %d", fSeed);
151       gRandom->SetSeed(fSeed);
152       fGenerator->SetSeed(fSeed);
153     }
154
155     AliRunLoader* rl = AliRunLoader::Open("galice.root","FASTRUN","recreate");
156     rl->MakeTree("E");
157     gAlice->SetRunLoader(rl);
158     rl->MakeStack();
159     fStack = rl->Stack();
160     fHeader = rl->GetHeader();
161     
162     fGenerator->SetStack(fStack);
163     fGenerator->Init();
164     
165     fMCEvent = new AliMCEvent;
166
167     return kTRUE;
168 }
169
170 Bool_t AliMCGenHandler::BeginEvent(Long64_t /*entry*/)
171
172     // Begin event
173
174     fHeader->Reset(0, fEventNumber++);
175     
176     if (AliAnalysisManager::GetAnalysisManager()->GetDebugLevel() > 1)
177       Printf("AliMCGenHandler::BeginEvent: Generating Event number %lld", fEventNumber);
178       
179     fGenerator->Generate();
180
181     fHeader->SetStack(fStack);
182     fHeader->SetNprimary(fStack->GetNprimary());
183     fHeader->SetNtrack(fStack->GetNtrack());  
184
185     fMCEvent->ConnectHeaderAndStack(fHeader);
186
187     return kTRUE;
188 }
189
190 Bool_t AliMCGenHandler::FinishEvent()
191 {
192     // Clean-up after each event
193    
194     fMCEvent->Stack()->Reset();
195
196     return kTRUE;
197 }