forked from davidrohr/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimaryGenerator.cxx
More file actions
217 lines (170 loc) · 6.04 KB
/
Copy pathPrimaryGenerator.cxx
File metadata and controls
217 lines (170 loc) · 6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright CERN and copyright holders of ALICE O2. This software is
// distributed under the terms of the GNU General Public License v3 (GPL
// Version 3), copied verbatim in the file "COPYING".
//
// See http://alice-o2.web.cern.ch/license for full licensing information.
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// \author R+Preghenella - June 2017
#include "Generators/PrimaryGenerator.h"
#include "Generators/InteractionDiamondParam.h"
#include "SimulationDataFormat/MCEventHeader.h"
#include "FairLogger.h"
#include "FairGenericStack.h"
#include "TFile.h"
#include "TTree.h"
#include "TDatabasePDG.h"
#include "TVirtualMC.h"
using o2::dataformats::MCEventHeader;
namespace o2
{
namespace eventgen
{
/*****************************************************************/
PrimaryGenerator::~PrimaryGenerator()
{
/** destructor **/
if (mEmbedFile && mEmbedFile->IsOpen()) {
mEmbedFile->Close();
delete mEmbedFile;
}
if (mEmbedEvent)
delete mEmbedEvent;
}
/*****************************************************************/
Bool_t PrimaryGenerator::Init()
{
/** init **/
LOG(INFO) << "Initialising primary generator";
/** embedding **/
if (mEmbedTree) {
LOG(INFO) << "Embedding into: " << mEmbedFile->GetName()
<< " (" << mEmbedEntries << " events)";
return FairPrimaryGenerator::Init();
}
/** normal generation **/
/** retrieve and set interaction diamond **/
auto& diamond = InteractionDiamondParam::Instance();
setInteractionDiamond(diamond.position, diamond.width);
/** base class init **/
return FairPrimaryGenerator::Init();
}
/*****************************************************************/
Bool_t PrimaryGenerator::GenerateEvent(FairGenericStack* pStack)
{
/** generate event **/
/** normal generation if no embedding **/
if (!mEmbedTree)
return FairPrimaryGenerator::GenerateEvent(pStack);
/** this is for embedding **/
/** setup interaction vertex **/
mEmbedTree->GetEntry(mEmbedIndex);
setInteractionVertex(mEmbedEvent);
/** generate event **/
if (!FairPrimaryGenerator::GenerateEvent(pStack))
return kFALSE;
/** add embedding info to event header **/
auto o2event = dynamic_cast<MCEventHeader*>(fEvent);
if (o2event) {
o2event->setEmbeddingFileName(mEmbedFile->GetName());
o2event->setEmbeddingEventIndex(mEmbedIndex);
}
/** increment embedding counter **/
mEmbedIndex++;
mEmbedIndex %= mEmbedEntries;
/** success **/
return kTRUE;
}
/*****************************************************************/
void PrimaryGenerator::AddTrack(Int_t pdgid, Double_t px, Double_t py, Double_t pz,
Double_t vx, Double_t vy, Double_t vz,
Int_t parent, Bool_t wanttracking,
Double_t e, Double_t tof,
Double_t weight, TMCProcess proc)
{
/** add track **/
/** check if particle exists in PDG database **/
if (!TDatabasePDG::Instance()->GetParticle(pdgid)) {
LOG(WARN) << "Skipping particle undefined in PDG: pdg = " << pdgid;
return;
}
/** success **/
FairPrimaryGenerator::AddTrack(pdgid, px, py, pz, vx, vy, vz, parent, wanttracking, e, tof, weight, proc);
}
/*****************************************************************/
void PrimaryGenerator::setInteractionDiamond(const Double_t* xyz, const Double_t* sigmaxyz)
{
/** set interaction diamond **/
LOG(INFO) << "Setting interaction diamond: position = {"
<< xyz[0] << "," << xyz[1] << "," << xyz[2] << "} cm";
LOG(INFO) << "Setting interaction diamond: width = {"
<< sigmaxyz[0] << "," << sigmaxyz[1] << "," << sigmaxyz[2] << "} cm";
SetBeam(xyz[0], xyz[1], sigmaxyz[0], sigmaxyz[1]);
SetTarget(xyz[2], sigmaxyz[2]);
auto const& param = InteractionDiamondParam::Instance();
SmearVertexXY(false);
SmearVertexZ(false);
SmearGausVertexXY(false);
SmearGausVertexZ(false);
if (param.distribution == o2::eventgen::EVertexDistribution::kFlat) {
SmearVertexXY(true);
SmearVertexZ(true);
} else if (param.distribution == o2::eventgen::EVertexDistribution::kGaus) {
SmearGausVertexXY(true);
SmearGausVertexZ(true);
} else {
LOG(ERROR) << "PrimaryGenerator: Unsupported vertex distribution";
}
}
/*****************************************************************/
void PrimaryGenerator::setInteractionVertex(const MCEventHeader* event)
{
/** set interaction vertex **/
Double_t xyz[3] = {event->GetX(), event->GetY(), event->GetZ()};
SetBeam(xyz[0], xyz[1], 0., 0.);
SetTarget(xyz[2], 0.);
SmearVertexXY(false);
SmearVertexZ(false);
SmearGausVertexXY(false);
SmearGausVertexZ(false);
}
/*****************************************************************/
Bool_t PrimaryGenerator::embedInto(TString fname)
{
/** embed into **/
/** check if a file is already open **/
if (mEmbedFile && mEmbedFile->IsOpen()) {
LOG(ERROR) << "Another embedding file is currently open";
return kFALSE;
}
/** open file **/
mEmbedFile = TFile::Open(fname);
if (!mEmbedFile || !mEmbedFile->IsOpen()) {
LOG(ERROR) << "Cannot open file for embedding: " << fname;
return kFALSE;
}
/** get tree **/
mEmbedTree = (TTree*)mEmbedFile->Get("o2sim");
if (!mEmbedTree) {
LOG(ERROR) << R"(Cannot find "o2sim" tree for embedding in )" << fname;
return kFALSE;
}
/** get entries **/
mEmbedEntries = mEmbedTree->GetEntries();
if (mEmbedEntries <= 0) {
LOG(ERROR) << "Invalid number of entries found in tree for embedding: " << mEmbedEntries;
return kFALSE;
}
/** connect MC event header **/
mEmbedEvent = new MCEventHeader;
mEmbedTree->SetBranchAddress("MCEventHeader.", &mEmbedEvent);
/** success **/
return kTRUE;
}
/*****************************************************************/
/*****************************************************************/
} /* namespace eventgen */
} /* namespace o2 */
ClassImp(o2::eventgen::PrimaryGenerator);