1 /**************************************************************************
2 * Copyright(c) 1998-1999, 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 **************************************************************************/
16 /* $Id: AliMCGenHandler.cxx 64675 2013-10-23 12:21:37Z hristov $ */
18 //-------------------------------------------------------------------------
19 // Class AliMCGenHandler
20 // This class can be used with the analysis framework to generate event on
21 // the fly and analyse them.
23 // Origin: Andrei Gheata, Jan Fiete Grosse-Oetringhaus
24 //-------------------------------------------------------------------------
26 #include "AliMCGenHandler.h"
27 #include "AliMCEvent.h"
29 #include "AliHeader.h"
32 #include "AliGenerator.h"
33 #include "AliRunLoader.h"
35 #include "AliAnalysisManager.h"
38 #include "TInterpreter.h"
40 ClassImp(AliMCGenHandler)
42 AliMCGenHandler::AliMCGenHandler() :
43 AliInputEventHandler(),
51 fGeneratorMacroPath(),
52 fGeneratorMacroParameters(),
53 fGeneratorCustomization(0)
56 // Default constructor
58 // Be sure to add all particles to the PDG database
59 AliPDG::AddParticlesToPdgDataBase();
62 AliMCGenHandler::AliMCGenHandler(const char* name, const char* title) :
63 AliInputEventHandler(name, title),
71 fGeneratorMacroPath(),
72 fGeneratorMacroParameters(),
73 fGeneratorCustomization(0)
78 // Be sure to add all particles to the PDG database
79 AliPDG::AddParticlesToPdgDataBase();
82 AliMCGenHandler::~AliMCGenHandler()
89 Bool_t AliMCGenHandler::Init(Option_t* /*opt*/)
95 if (fGeneratorMacroPath.Length() == 0)
96 AliFatal("fGeneratorMacroPath empty!");
99 macroPath.Form("$ALICE_ROOT/%s", fGeneratorMacroPath.Data());
100 macroPath = gSystem->ExpandPathName(macroPath.Data());
102 if (gSystem->AccessPathName(macroPath))
103 AliFatal(Form("Cannot find macro %s", macroPath.Data()));
107 Long64_t retval = m.Exec(fGeneratorMacroParameters, &error);
108 if (error != TInterpreter::kNoError)
109 AliFatal(Form("Macro interpretation %s failed", macroPath.Data()));
112 AliFatal(Form("The macro %s did not return a valid generator (1)", macroPath.Data()));
114 fGenerator = reinterpret_cast<AliGenerator*>(retval);
116 AliFatal(Form("The macro %s did not return a valid generator (2)", macroPath.Data()));
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");
127 Printf("AliMCGenHandler::Init: Not setting any seed. Seed needs to be set externally!");
132 Printf("AliMCGenHandler::Init: Using manually set seed");
134 else if (fSeedMode == 2)
136 Printf("AliMCGenHandler::Init: Taking seed from current time");
139 else if (fSeedMode == 3)
141 Printf("AliMCGenHandler::Init: Taking seed from AliEn job id");
142 TString tmp(gSystem->Getenv("ALIEN_PROC_ID"));
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()));
148 AliFatal(Form("Seed mode %d unknown", fSeedMode));
150 Printf("AliMCGenHandler::Init: Using seed: %d", fSeed);
151 gRandom->SetSeed(fSeed);
152 fGenerator->SetSeed(fSeed);
155 AliRunLoader* rl = AliRunLoader::Open("galice.root","FASTRUN","recreate");
157 gAlice->SetRunLoader(rl);
159 fStack = rl->Stack();
160 fHeader = rl->GetHeader();
162 fGenerator->SetStack(fStack);
165 fMCEvent = new AliMCEvent;
170 Bool_t AliMCGenHandler::BeginEvent(Long64_t /*entry*/)
174 fHeader->Reset(0, fEventNumber++);
176 if (AliAnalysisManager::GetAnalysisManager()->GetDebugLevel() > 1)
177 Printf("AliMCGenHandler::BeginEvent: Generating Event number %lld", fEventNumber);
179 fGenerator->Generate();
181 fHeader->SetStack(fStack);
182 fHeader->SetNprimary(fStack->GetNprimary());
183 fHeader->SetNtrack(fStack->GetNtrack());
185 fMCEvent->ConnectHeaderAndStack(fHeader);
190 Bool_t AliMCGenHandler::FinishEvent()
192 // Clean-up after each event
194 fMCEvent->Stack()->Reset();