ATLAS Offline Software
Loading...
Searching...
No Matches
BeamHaloGeneratorAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
9
10#include "GaudiKernel/MsgStream.h"
11#include "AtlasHepMC/GenEvent.h"
12#include <cmath>
13
14//--------------------------------------------------------------------------
15
16BeamHaloGeneratorAlg::BeamHaloGeneratorAlg(const std::string& name, ISvcLocator* svcLocator)
17 : GenModule(name,svcLocator)
18{
19 for (int i=0;i<NPLOTS;i++) {
20 m_validationPlots[i] = nullptr;
21 }
22}
23
24//--------------------------------------------------------------------------
25
27 ATH_MSG_INFO("Initialising this BeamHaloGeneratorAlg: " << name());
28
29 ATH_MSG_INFO("================ Generator Settings =================");
30 ATH_MSG_INFO(" input type ------------------ " << m_inputTypeStr);
31 ATH_MSG_INFO(" input file ------------------ " << m_inputFile);
32 ATH_MSG_INFO(" interface plane ------------- " << m_interfacePlane << " (mm)");
33 ATH_MSG_INFO(" enable flip about z=0 ------- " << (m_enableFlip ? "True" : "False"));
34 ATH_MSG_INFO(" flip probability ----------- " << m_flipProbability);
35 ATH_MSG_INFO(" enable sampling ------------- " << (m_enableSampling ? "True" : "False"));
36 ATH_MSG_INFO(" binary buffer file ---------- " << m_bufferFileName);
37 std::vector<std::string>::iterator itr = m_generatorSettings.begin();
38 std::vector<std::string>::iterator itr_end = m_generatorSettings.end();
39 for(;itr!=itr_end;++itr) ATH_MSG_INFO((*itr));
40 ATH_MSG_INFO(" Produce monitoring plots ---- " << (m_doMonitoringPlots ? "True" : "False"));
41 ATH_MSG_INFO(" Random stream name ---------- " << m_randomStream);
42 ATH_MSG_INFO("=====================================================");
43
44 // Check the flip probability
45 if(m_flipProbability <= 0) {
46 ATH_MSG_INFO("Event flipping will be disabled.");
47 m_enableFlip = false;
48 }
49 else if(m_flipProbability > 1) {
50 ATH_MSG_WARNING("Flip probability " << m_flipProbability << " is out of range. Event flipping will be disabled.");
51 m_enableFlip = false;
52 }
53 else {
54 m_enableFlip = true;
55 }
56
57 // Retrieve pointer to THistSvc if monitoring plots are requested
60 ATH_CHECK(m_tHistSvc.retrieve());
61
62 // Create log10 bins for x-axis of E plots.
63 const Int_t nbins_E = 60;
64 double xmin = 1e-2;
65 double xmax = 3.5e3;
66 double logxmin = std::log10(xmin);
67 double logxmax = std::log10(xmax);
68 double binwidth = (logxmax-logxmin)/nbins_E;
69 Double_t xbins[nbins_E+1];
70 xbins[0] = xmin;
71 for (Int_t i=1;i<=nbins_E;i++) {
72 xbins[i] = xmin + std::pow(10,logxmin+i*binwidth);
73 }
74
75 // Create the monitoring plots
76 m_validationPlots[PRI_R] = new TH1F("primaryR",";Radius [m];Events/[m]",100,0., 0.2);
77 m_validationPlots[PRI_Z] = new TH1F("primaryZ",";z [m];Events/[m]",1100,0., 550.); // Beam gas and TCT
78 m_validationPlots[PRI_Z_TCT] = new TH1F("primaryZ_TCT",";z [m];Events/[m]",100,144., 149.); // TCT region
79 m_validationPlots[SP_R_ALL] = new TH1F("scoringPlaneR_all",";Radius [m];Particles/[m]",240,0., 24.);
80 m_validationPlots[SP_E_ALL] = new TH1F("scoringPlaneE_all",";Energy [GeV];Particles/[GeV]",nbins_E,xbins);
81 m_validationPlots[SP_PZ_ALL] = new TH1F("scoringPlanePz_all",";p_{z} [GeV];Particles/[GeV]",350,0., 3500.);
82 m_validationPlots[SP_PT_ALL] = new TH1F("scoringPlanePt_all",";p_{T} [GeV];Particles/[GeV]",500,0., 50.);
83 m_validationPlots[SP_R_PROTONS] = new TH1F("scoringPlaneR_protons",";Radius [m];Particles/[m]",240,0., 24.);
84 m_validationPlots[SP_E_PROTONS] = new TH1F("scoringPlaneE_protons",";Energy [GeV];Particles/[GeV]",nbins_E,xbins);
85 m_validationPlots[SP_PZ_PROTONS] = new TH1F("scoringPlanePz_protons",";p_{z} [GeV];Particles/[GeV]",350,0., 3500.);
86 m_validationPlots[SP_PT_PROTONS] = new TH1F("scoringPlanePt_protons",";p_{T} [GeV];Particles/[GeV]",500,0., 50.);
87 m_validationPlots[SP_R_MUONS] = new TH1F("scoringPlaneR_muons",";Radius [m];Particles/[m]",240,0., 24.);
88 m_validationPlots[SP_E_MUONS] = new TH1F("scoringPlaneE_muons",";Energy [GeV];Particles/[GeV]",nbins_E,xbins);
89 m_validationPlots[SP_PZ_MUONS] = new TH1F("scoringPlanePz_muons",";p_{z} [GeV];Particles/[GeV]",350,0., 3500.);
90 m_validationPlots[SP_PT_MUONS] = new TH1F("scoringPlanePt_muons",";p_{T} [GeV];Particles/[GeV]",500,0., 50.);
91
92 ATH_CHECK(m_tHistSvc->regHist("/BeamHalo/primaryR", m_validationPlots[PRI_R]));
93 ATH_CHECK(m_tHistSvc->regHist("/BeamHalo/primaryZ", m_validationPlots[PRI_Z]));
94 ATH_CHECK(m_tHistSvc->regHist("/BeamHalo/primaryZ_TCT", m_validationPlots[PRI_Z_TCT]));
95 ATH_CHECK(m_tHistSvc->regHist("/BeamHalo/scoringPlaneR_all", m_validationPlots[SP_R_ALL]));
96 ATH_CHECK(m_tHistSvc->regHist("/BeamHalo/scoringPlaneE_all", m_validationPlots[SP_E_ALL]));
97 ATH_CHECK(m_tHistSvc->regHist("/BeamHalo/scoringPlanePz_all", m_validationPlots[SP_PZ_ALL]));
98 ATH_CHECK(m_tHistSvc->regHist("/BeamHalo/scoringPlanePt_all", m_validationPlots[SP_PT_ALL]));
99 ATH_CHECK(m_tHistSvc->regHist("/BeamHalo/scoringPlaneR_protons", m_validationPlots[SP_R_PROTONS]));
100 ATH_CHECK(m_tHistSvc->regHist("/BeamHalo/scoringPlaneE_protons", m_validationPlots[SP_E_PROTONS]));
101 ATH_CHECK(m_tHistSvc->regHist("/BeamHalo/scoringPlanePz_protons", m_validationPlots[SP_PZ_PROTONS]));
102 ATH_CHECK(m_tHistSvc->regHist("/BeamHalo/scoringPlanePt_protons", m_validationPlots[SP_PT_PROTONS]));
103 ATH_CHECK(m_tHistSvc->regHist("/BeamHalo/scoringPlaneR_muons", m_validationPlots[SP_R_MUONS]));
104 ATH_CHECK(m_tHistSvc->regHist("/BeamHalo/scoringPlaneE_muons", m_validationPlots[SP_E_MUONS]));
105 ATH_CHECK(m_tHistSvc->regHist("/BeamHalo/scoringPlanePz_muons", m_validationPlots[SP_PZ_MUONS]));
106 ATH_CHECK(m_tHistSvc->regHist("/BeamHalo/scoringPlanePt_muons", m_validationPlots[SP_PT_MUONS]));
107 }
108
109 // Check the input type string
110 if (m_inputTypeStr == "MARS-NM") {
114 }
115 else if (m_inputTypeStr == "FLUKA-VT") {
119 }
120 else if (m_inputTypeStr == "FLUKA-RB") {
124 }
125 else {
127 ATH_MSG_FATAL("Input type " << m_inputTypeStr << " is not known. Available types are: MARS-NM or FLUKA-VT");
128 return StatusCode::FAILURE;
129 }
130
131 // Set the options using those specified to this algorithm
132 m_beamHaloGenerator->setInterfacePlane(m_interfacePlane);
133 m_beamHaloGenerator->setEnableFlip(m_enableFlip);
134 m_beamHaloGenerator->setFlipProbability(m_flipProbability);
135 m_beamHaloGenerator->setEnableSampling(m_enableSampling);
136 m_beamHaloGenerator->setBufferFileName(m_bufferFileName);
137 m_beamHaloGenerator->setDebugEnable(msgLvl(MSG::DEBUG));
138
139 // Initialise the generator.
140 if(m_beamHaloGenerator->genInitialize() != 0) return StatusCode::FAILURE;
141
142 return StatusCode::SUCCESS;
143}
144
145//---------------------------------------------------------------------------
146
148
149 const EventContext& ctx = Gaudi::Hive::currentContext();
150 CLHEP::HepRandomEngine* rndmEngine = this->getRandomEngine(m_randomStream, ctx);
151
152
153 // Clear the event ready for it to be filled with the next beam halo event.
154 m_evt.clear();
155
156 // Fill an event with particles from the converted ASCII input file.
157 // (If the end of file has already been reached this will return a
158 // non-zero value.)
159 if(m_beamHaloGenerator->fillEvt(&m_evt, rndmEngine) != 0) return StatusCode::FAILURE;
160
161 // Fill monitoring plots if requested
163 auto weightContainer = m_evt.weights();
164 if(weightContainer.size() != 5) {
165 ATH_MSG_WARNING("The number of weights for this event is not equal to 5.");
166 return StatusCode::SUCCESS;
167 }
168 double weight = weightContainer[0];
169 HepMC::FourVector primaryPosition(weightContainer[1],weightContainer[2],weightContainer[3],weightContainer[4]);
170 m_validationPlots[PRI_R]->Fill(primaryPosition.perp()/1000.0,weight);
171 m_validationPlots[PRI_Z]->Fill(weightContainer[3]/1000.0,weight);
172 m_validationPlots[PRI_Z_TCT]->Fill(std::fabs(weightContainer[3]/1000.0),weight);
173
174 double values[4];
175 int pdgId;
176 for (auto hepmc_part: m_evt) {
177 auto prodVertex = hepmc_part->production_vertex();
178 if(!prodVertex) continue;
179
180 // Store the values for use in the if conditions that follow
181 values[0] = prodVertex->position().perp()/1000.;
182 values[1] = hepmc_part->momentum().e()/1000.;
183 values[2] = hepmc_part->momentum().pz()/1000.;
184 values[3] = hepmc_part->momentum().perp()/1000.;
185
186 pdgId = hepmc_part->pdg_id(); if(pdgId<0) pdgId = -pdgId;
187 m_validationPlots[SP_R_ALL]->Fill(values[0],weight);
188 m_validationPlots[SP_E_ALL]->Fill(values[1],weight);
189 m_validationPlots[SP_PZ_ALL]->Fill(values[2],weight);
190 m_validationPlots[SP_PT_ALL]->Fill(values[3],weight);
191 if(pdgId == 2212) {
192 m_validationPlots[SP_R_PROTONS]->Fill(values[0],weight);
193 m_validationPlots[SP_E_PROTONS]->Fill(values[1],weight);
194 m_validationPlots[SP_PZ_PROTONS]->Fill(values[2],weight);
195 m_validationPlots[SP_PT_PROTONS]->Fill(values[3],weight);
196 }
197 else if(pdgId == 13) {
198 m_validationPlots[SP_R_MUONS]->Fill(values[0],weight);
199 m_validationPlots[SP_E_MUONS]->Fill(values[1],weight);
200 m_validationPlots[SP_PZ_MUONS]->Fill(values[2],weight);
201 m_validationPlots[SP_PT_MUONS]->Fill(values[3],weight);
202 }
203 }
204 }
205
206 return StatusCode::SUCCESS;
207}
208
209//---------------------------------------------------------------------------
210
212 *event = m_evt;
213 return StatusCode::SUCCESS;
214}
215
216//---------------------------------------------------------------------------
217
219 if(m_beamHaloGenerator->genFinalize() != 0) return StatusCode::FAILURE;
220 return StatusCode::SUCCESS;
221}
222
223//---------------------------------------------------------------------------
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
bool msgLvl(const MSG::Level lvl) const
virtual StatusCode genInitialize()
For initializing the generator, if required.
BooleanProperty m_enableFlip
Flag for flipping event.
ServiceHandle< ITHistSvc > m_tHistSvc
A pointer to the THist service for validation plots.
DoubleProperty m_interfacePlane
The position of the interface plane in mm.
virtual StatusCode genFinalize()
For finalising the generator, if required.
StringProperty m_inputFile
Input file name.
BooleanProperty m_doMonitoringPlots
A flag to allow monitoring plots to be turned on or off.
StringProperty m_inputTypeStr
Input file type and therefore associated beam halo generator that should be used.
virtual StatusCode fillEvt(HepMC::GenEvent *evt)
Fill the GenEvent pointer with the contents of the GenEvent cache.
TH1F * m_validationPlots[NPLOTS]
An array of TH1F pointers for validation plots.
StringProperty m_bufferFileName
The name of the binary buffer file, needed for sampling from a converted file.
virtual StatusCode callGenerator()
Read one event from the selected input and convert it into GenEvent format.
FloatProperty m_flipProbability
Flip probability.
StringProperty m_randomStream
Name of the random number stream.
BeamHaloGenerator * m_beamHaloGenerator
A pointer to the beam halo generator.
StringArrayProperty m_generatorSettings
A vector of strings defining generator settings.
HepMC::GenEvent m_evt
An empty GenEvent to cache the generate output between callGenerator and fillEvt.
BooleanProperty m_enableSampling
Flag to enable or disable sampling.
BeamHaloGeneratorAlg(const std::string &name, ISvcLocator *svcLocator)
A class to provide conversion from a FLUKA format ASCII input record into HepMC format,...
GenModule(const std::string &name, ISvcLocator *pSvcLocator)
Constructor.
Definition GenModule.cxx:14
CLHEP::HepRandomEngine * getRandomEngine(const std::string &streamName, const EventContext &ctx) const
Definition GenModule.cxx:34
A class to provide conversion from a MARS format ASCII input record into HepMC format,...
double xmax
Definition listroot.cxx:61
double xmin
Definition listroot.cxx:60
bool binwidth
Definition listroot.cxx:58
HepMC3::FourVector FourVector
HepMC3::GenEvent GenEvent
Definition GenEvent.h:39