Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CosmicSpray.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file CosmicSpray.cc
1 //CosmicSpray class
2 // Author: Daniel Lis
3 // Brief: Particel generator Class that sources a muon with a vertex and momentum that should mimic real life
4 // modified by Shuhang on 03/2022: now this class serves as a wrapper class that drives "EcoMug"
5 
6 //For the muon rate calculation, if using the default setting: time(second) = nevents * 1.434e-4, then scale the histogram by 1/time to get the rate.
7 
8 #include "CosmicSpray.h"
9 #include "EcoMug.h"
10 
11 #include "PHG4InEvent.h"
12 #include "PHG4Particle.h"
13 #include "PHG4Particlev2.h"
14 
16 #include <fun4all/SubsysReco.h>
17 
18 #include <phool/PHCompositeNode.h>
19 #include <phool/PHDataNode.h> // for PHDataNode
20 #include <phool/PHNode.h> // for PHNode
21 #include <phool/PHNodeIterator.h> // for PHNodeIterator
22 #include <phool/PHObject.h> // for PHObject
23 #include <phool/getClass.h>
24 #include <phool/PHRandomSeed.h>
25 
26 
27 #include <array> // for array
28 #include <cmath>
29 #include <iostream> // for operator<<, endl, basic_ostream
30 
31 bool CosmicSpray::InDetector(double x, double y, double z)
32 {
33  double gap = 5;
34  if (x > _x_max) return false;
35  if (x < -_x_max) return false;
36  if (z > _z_max + gap) return false;
37  if (z < -_z_max - gap) return false;
38  if (y > _y_fix + gap) return false;
39  if (y < -_y_fix - gap) return false;
40  return true;
41 }
42 
44  : SubsysReco(name)
45 {
46  _x_max = 264.71;
47  _x_min = 183.3;
48  _z_max = 304.91;
49  _z_min = -304.91;
50  _y_fix = _x_max;
51 
52  unsigned int seed = PHRandomSeed();
53  gen.SetSeed(seed);
54  gen.SetUseHSphere(); // half-spherical surface generation
55  gen.SetHSphereRadius(R / 100); // half-sphere radius
56  gen.SetHSphereCenterPosition({{0., 0., -_y_fix / 100}});
58  _R = R;
59  return;
60 }
61 
63 {
64  PHG4InEvent *inevent = findNode::getClass<PHG4InEvent>(topNode, "PHG4INEVENT");
65  if (!inevent)
66  {
67  PHNodeIterator iter(topNode);
68  PHCompositeNode *dstNode;
69  dstNode = dynamic_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", "DST"));
70 
71  inevent = new PHG4InEvent();
72  PHDataNode<PHObject> *newNode = new PHDataNode<PHObject>(inevent, "PHG4INEVENT", "PHObject");
73  dstNode->addNode(newNode);
74  }
76 }
77 
79 {
80  // set_vertex
81  if (Verbosity() > 0) std::cout << "Processing Event" << std::endl;
82  std::string pdgname = "mu-";
83  int pdgcode = 13;
84  int trackid = 0;
85  double gun_t = 0.0;
86  double gun_x = 0, gun_y = 0, gun_z = 0;
87  double gun_px = 0, gun_py = 0, gun_pz = 0;
88  bool GoodEvent = false;
89  while (!GoodEvent)
90  {
91  gen.Generate();
92  std::array<double, 3> muon_position = gen.GetGenerationPosition();
93  double muon_p = gen.GetGenerationMomentum();
94  _gun_e = sqrt(0.105658 * 0.105658 + muon_p * muon_p);
95  double tr = gen.GetGenerationTheta();
96  double pr = gen.GetGenerationPhi();
97  double muon_charge = gen.GetCharge();
98 
99  if (muon_charge > 0)
100  {
101  pdgcode = -13;
102  pdgname = "mu+";
103  }
104 
105  gun_px = muon_p * sin(tr) * sin(pr);
106  gun_py = -1 * fabs(muon_p * cos(tr));
107  gun_pz = muon_p * sin(tr) * cos(pr);
108 
109  gun_x = muon_position[1] * 100;
110  gun_y = muon_position[2] * 100;
111  gun_z = muon_position[0] * 100;
112 
113  //unit vectors of muon momentum
114  double upx = gun_px / muon_p;
115  double upy = gun_py / muon_p;
116  double upz = gun_pz / muon_p;
117 
118  // check if the muon goes in the detector
119 
120  double x1 = gun_x;
121  double y1 = gun_y;
122  double z1 = gun_z;
123 
124  double L = 0;
125 
126 
127  while (y1 > -_y_fix && L < _R)
128  {
129  L += 10;
130  x1 += upx * 10;
131  y1 += upy * 10;
132  z1 += upz * 10;
133 
134  if (InDetector(x1, y1, z1))
135  {
136  GoodEvent = true;
137  break;
138  }
139  }
140  }
141 
142  PHG4InEvent *inevent = findNode::getClass<PHG4InEvent>(topNode, "PHG4INEVENT");
143  int vtxindex = inevent->AddVtx(gun_x, gun_y, gun_z, gun_t);
144 
146  particle->set_track_id(trackid);
147  particle->set_vtx_id(vtxindex);
148  particle->set_parent_id(0);
149  particle->set_name(pdgname);
150  particle->set_pid(pdgcode);
151  particle->set_px(gun_px);
152  particle->set_py(gun_py);
153  particle->set_pz(gun_pz);
154  particle->set_e(_gun_e);
155  inevent->AddParticle(vtxindex, particle);
156  if (Verbosity() > 0)
157  {
158  std::cout << "Momentum: " << gun_px << " / " << gun_py << " / " << gun_pz << std::endl;
159  std::cout << "total mom: " << _gun_e << std::endl;
160  std::cout << "track_id: " << trackid << std::endl;
161  std::cout << "vtxindex: " << vtxindex << std::endl;
162  std::cout << "pdgname: " << pdgname << std::endl;
163  std::cout << "pdgcode: " << pdgcode << std::endl;
164  }
165  return 0;
166 }