CbmRoot
CbmEventGenerator.cxx
Go to the documentation of this file.
1 
6 #include "CbmEventGenerator.h"
7 
8 #include "CbmBeam.h"
9 #include <FairGenericStack.h>
10 #include <FairLogger.h>
11 #include <FairMCEventHeader.h>
12 #include <TMath.h>
13 #include <TRandom.h>
14 #include <TVector3.h>
15 #include <cassert>
16 
17 
18 // ----- Constructor ---------------------------------------------------
20  : FairPrimaryGenerator()
21  , fBeamProfile()
22  , fTarget()
23  , fForceVertexInTarget(kTRUE)
24  , fForceVertexAtZ(kFALSE)
25  , fVertexZ(0.) {
26  // Mother class members
27  fName = "EventGenerator";
28  fBeamAngle = kTRUE;
29 }
30 // -------------------------------------------------------------------------
31 
32 
33 // ----- Destructor ----------------------------------------------------
35 // -------------------------------------------------------------------------
36 
37 
38 // ----- Force vertex at a given z position ----------------------------
39 void CbmEventGenerator::ForceVertexAtZ(Double_t zVertex) {
40  fForceVertexAtZ = kTRUE;
41  fForceVertexInTarget = kFALSE;
42  fVertexZ = zVertex;
43 }
44 // -------------------------------------------------------------------------
45 
46 
47 // ----- Generate the input event --------------------------------------
48 Bool_t CbmEventGenerator::GenerateEvent(FairGenericStack* stack) {
49 
50  std::cout << std::endl;
51  LOG(info) << GetName() << ": Generate event " << ++fEventNr;
52 
53  // An MCEventHeader must be present
54  assert(fEvent);
55 
56  // Set the stack
57  assert(stack);
58  fStack = stack;
59 
60  // Reset MCEventHeader and track counter
61  fEvent->Reset();
62  fNTracks = 0;
63 
64  // Generate the event vertex and the beam angle
65  MakeVertex();
66 
67  // Generate the event plane angle
68  if (fEventPlane) {
69  MakeEventPlane();
70  LOG(info) << GetName() << ": Rotate event by " << fPhi << " rad";
71  }
72 
73  // Call the registered generator classes
74  fListIter->Reset();
75  TObject* item = nullptr;
76  while ((item = fListIter->Next())) {
77  FairGenerator* generator = dynamic_cast<FairGenerator*>(item);
78  assert(generator);
79  fMCIndexOffset = fNTracks; // Number of tracks before call of generator
80  if (!generator->ReadEvent(this)) {
81  LOG(info) << GetName() << ": ReadEvent failed for generator "
82  << generator->GetName();
83  return kFALSE;
84  }
85  }
86 
87  // Update event header
88  if (!fEvent->IsSet()) fEvent->SetEventID(fEventNr);
89  fEvent->SetNPrim(fNTracks);
90  fEvent->SetVertex(fVertex);
91  fEvent->SetRotX(fBeamAngleX);
92  fEvent->SetRotY(fBeamAngleY);
93  Double_t phiGen = fEvent->GetRotZ(); // from concrete generators
94  fEvent->SetRotZ(phiGen + fPhi); // total event plane angle
95 
96  // Event info to screen
97  LOG(info) << GetName() << ": Event ID " << fEvent->GetEventID() << ", tracks "
98  << fNTracks << ", vertex (" << fVertex.X() << ", " << fVertex.Y()
99  << ", " << fVertex.Z() << ") cm";
100  LOG(info) << GetName() << ": Beam angle (" << fBeamAngleX << ", "
101  << fBeamAngleY << ") rad, event plane angle " << fPhi
102  << " rad (total " << fEvent->GetRotZ() << " rad)";
103 
104  // Update global track counter (who knows what it's good for ...)
105  fTotPrim += fNTracks;
106 
107  return kTRUE;
108 }
109 // -------------------------------------------------------------------------
110 
111 
112 // ----- Generate event vertex -----------------------------------------
114 
115  if (fForceVertexAtZ)
116  MakeVertexAtZ();
117  else if (fForceVertexInTarget && fTarget)
119  else
121 }
122 // -------------------------------------------------------------------------
123 
124 
125 // ----- Generate event vertex in the beam focal plane -----------------
127 
128  std::unique_ptr<CbmBeam> beam;
129  beam = fBeamProfile.GenerateBeam();
130  TVector3 point(0., 0., fVertexZ);
131  TVector3 norm(0, 0., 1.);
132  fVertex = beam->ExtrapolateToPlane(point, norm);
133  fBeamAngleX = beam->GetThetaX();
134  fBeamAngleY = beam->GetThetaY();
135  fBeamDirection = beam->GetDirection();
136 }
137 // -------------------------------------------------------------------------
138 
139 
140 // ----- Generate event vertex in the beam focal plane -----------------
142 
143  std::unique_ptr<CbmBeam> beam;
144  beam = fBeamProfile.GenerateBeam();
145  fVertex = beam->GetPosition();
146  fBeamAngleX = beam->GetThetaX();
147  fBeamAngleY = beam->GetThetaY();
148  fBeamDirection = beam->GetDirection();
149 }
150 // -------------------------------------------------------------------------
151 
152 
153 // ----- Generate event vertex -----------------------------------------
155 
156  assert(fTarget);
157 
158  // Target surface centres and normal vector
159  TVector3 surf1 = fTarget->GetSurfaceCentreUp();
160  TVector3 surf2 = fTarget->GetSurfaceCentreDown();
161  TVector3 norm = fTarget->GetNormal();
162 
163  // Declare some vectors
164  TVector3 point1; // Intersection of beam with first target surface
165  TVector3 point2; // Intersection of beam with second target surface
166 
167  // It is possible that a generated beam trajectory misses the target.
168  // In that case, the beam will be re-sampled until it hits the target.
169  Bool_t isInTarget = kFALSE;
170  Int_t nSamples = 0;
171  std::unique_ptr<CbmBeam> beam;
172  while (!isInTarget) {
173 
174  // Abort if too many beam samples
175  if (nSamples > 1000.)
176  LOG(fatal) << GetName() << ": Aborting after " << nSamples
177  << " beam samplings. Adjust beam and target.";
178 
179  // Sample a beam trajectory
180  beam = fBeamProfile.GenerateBeam();
181  nSamples++;
182 
183  // Intersections of beam trajectory with target surfaces
184  point1 = beam->ExtrapolateToPlane(surf1, norm);
185  point2 = beam->ExtrapolateToPlane(surf2, norm);
186 
187  // Check whether both intersections are inside the target
188  // (i.e., the beam crosses both target surfaces)
189  Bool_t check1 = ((point1 - surf1).Mag() < 0.5 * fTarget->GetDiameter());
190  Bool_t check2 = ((point2 - surf2).Mag() < 0.5 * fTarget->GetDiameter());
191  isInTarget = check1 && check2;
192 
193  } //? Beam not in target
194 
195  LOG(debug) << beam->ToString() << ", generated after " << nSamples
196  << (nSamples > 1 ? " samplings: " : " sampling");
197 
198  // Sample a point between entry and exit of beam in target
199  Double_t scale = 0.5;
200  if (fSmearVertexZ) scale = gRandom->Uniform();
201  fVertex = point1 + scale * (point2 - point1);
202 
203  // Set the beam angles (member variables of base class)
204  fBeamAngleX = beam->GetThetaX();
205  fBeamAngleY = beam->GetThetaY();
206  fBeamDirection = beam->GetDirection();
207 }
208 // -------------------------------------------------------------------------
209 
210 
211 // ----- Info ----------------------------------------------------------
212 void CbmEventGenerator::Print(Option_t*) const {
213 
214  LOG(info) << fBeamProfile.ToString();
215  if (fTarget) LOG(info) << fTarget->ToString();
216  LOG(info) << "Vertex smearing along beam " << (fSmearVertexZ ? "ON" : "OFF");
217  if (fEventPlane)
218  LOG(info) << "Random event plane angle between " << fPhiMin << " and "
219  << fPhiMax << " rad";
220  else
221  LOG(info) << "Fixed event plane angle = 0";
222  LOG(info) << "Number of generators " << fGenList->GetEntries();
223  for (Int_t iGen = 0; iGen < fGenList->GetEntries(); iGen++) {
224  fGenList->At(iGen)->Print();
225  }
226 }
227 // -------------------------------------------------------------------------
228 
229 
230 // ----- Set beam angle ------------------------------------------------
231 void CbmEventGenerator::SetBeamAngle(Double_t meanThetaX,
232  Double_t meanThetaY,
233  Double_t sigmaThetaX,
234  Double_t sigmaThetaY) {
235  fBeamProfile.SetAngle(meanThetaX, meanThetaY, sigmaThetaX, sigmaThetaY);
236 }
237 // -------------------------------------------------------------------------
238 
239 
240 // ----- Set beam position ---------------------------------------------
242  Double_t meanY,
243  Double_t sigmaX,
244  Double_t sigmaY,
245  Double_t zF) {
246  fBeamProfile.SetPosition(meanX, meanY, sigmaX, sigmaY, zF);
247 }
248 // -------------------------------------------------------------------------
249 
250 
CbmBeamProfile::SetPosition
void SetPosition(Double_t x0, Double_t y0, Double_t sigmaX=-1., Double_t sigmaY=-1., Double_t zF=0.)
Set the parameters for the beam position distribution.
Definition: CbmBeamProfile.cxx:120
CbmEventGenerator::MakeVertexInTarget
virtual void MakeVertexInTarget()
Generate event vertex position in the target.
Definition: CbmEventGenerator.cxx:154
CbmEventGenerator::fBeamProfile
CbmBeamProfile fBeamProfile
Beam properties.
Definition: CbmEventGenerator.h:139
CbmEventGenerator::fVertexZ
Double_t fVertexZ
forced z coordinate of event vertex
Definition: CbmEventGenerator.h:143
CbmBeamProfile::GenerateBeam
std::unique_ptr< CbmBeam > GenerateBeam()
Generate a beam trajectory.
Definition: CbmBeamProfile.cxx:80
CbmBeamProfile::SetAngle
void SetAngle(Double_t x0, Double_t y0, Double_t sigmaX=-1., Double_t sigmaY=-1.)
Set the parameters for the beam angle distribution.
Definition: CbmBeamProfile.cxx:107
CbmEventGenerator
CbmEventGenerator.
Definition: CbmEventGenerator.h:34
CbmBeamProfile::ToString
std::string ToString() const
Info to string.
Definition: CbmBeamProfile.cxx:135
CbmEventGenerator::fTarget
std::shared_ptr< const CbmTarget > fTarget
Definition: CbmEventGenerator.h:140
CbmEventGenerator::SetBeamAngle
void SetBeamAngle(Double_t meanThetaX, Double_t meanThetaY, Double_t sigmaThetaX=-1., Double_t sigmaThetaY=-1.)
Set the beam angle in the focal plane.
Definition: CbmEventGenerator.cxx:231
CbmEventGenerator::SetBeamPosition
void SetBeamPosition(Double_t meanX, Double_t meanY, Double_t sigmaX=-1., Double_t sigmaY=-1., Double_t zF=0.)
Set the beam position in the focal plane.
Definition: CbmEventGenerator.cxx:241
CbmEventGenerator::Print
virtual void Print(Option_t *opt="") const
Log output.
Definition: CbmEventGenerator.cxx:212
CbmBeam.h
CbmEventGenerator::GenerateEvent
virtual Bool_t GenerateEvent(FairGenericStack *stack)
Generate the input event.
Definition: CbmEventGenerator.cxx:48
CbmEventGenerator::ForceVertexAtZ
void ForceVertexAtZ(Double_t zVertex)
Force event vertex to be at a given z.
Definition: CbmEventGenerator.cxx:39
CbmEventGenerator::MakeVertexAtZ
void MakeVertexAtZ()
Generate event vertex position at a given z.
Definition: CbmEventGenerator.cxx:126
ClassImp
ClassImp(CbmConverterManager) InitStatus CbmConverterManager
Definition: CbmConverterManager.cxx:12
CbmEventGenerator::CbmEventGenerator
CbmEventGenerator()
Default constructor
Definition: CbmEventGenerator.cxx:19
CbmEventGenerator::fForceVertexInTarget
Bool_t fForceVertexInTarget
Target properties.
Definition: CbmEventGenerator.h:141
CbmEventGenerator::fForceVertexAtZ
Bool_t fForceVertexAtZ
If set, vertex must be at given z.
Definition: CbmEventGenerator.h:142
CbmEventGenerator::~CbmEventGenerator
virtual ~CbmEventGenerator()
Destructor
Definition: CbmEventGenerator.cxx:34
CbmEventGenerator::MakeVertex
virtual void MakeVertex()
Generate event vertex position.
Definition: CbmEventGenerator.cxx:113
CbmEventGenerator::MakeVertexInFocalPlane
virtual void MakeVertexInFocalPlane()
Generate event vertex position in the beam focal plane.
Definition: CbmEventGenerator.cxx:141
CbmEventGenerator.h