EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
particlegun.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file particlegun.cxx
1 #include <iomanip>
2 
3 #include <TDatabasePDG.h>
4 #include <TLorentzVector.h>
5 #include <TH1.h>
6 #include <TH2.h>
7 #include <TCanvas.h>
8 #include <TLegend.h>
9 #include <TStyle.h>
10 
11 #include "eicsmeardetectors.hh"
12 
16 #include "eicsmear/smear/EventS.h"
17 #include "eicsmear/smear/Smear.h"
19 
20 const double deg_to_rad = 0.01745329251; // pi/180
21 
22 using std::setw;
23 
30 };
31 
32 struct EicSmearStep {
33  TLorentzVector not_smeared_lorentz;
34  TLorentzVector smeared_lorentz;
36 };
37 
38 // Some statistics about the smearing process
40  long int total_particles;
41  long int null_particles;
42  long int zero_e_smear_p;
43  long int smear_e_zero_p;
44  long int smear_e_smear_p;
45  long int zero_e_zero_p;
46 
47  std::vector<EicSmearStep> steps;
48 
54 
55  TH2D* smear_DelP;
56 };
57 
59 EicSmearStep DoSmearStep(int pdg, TLorentzVector& input_vect, Smear::Detector& detector) {
60  static int particle_index = 0;
62 
63  step.not_smeared_lorentz = input_vect;
64 
65  // Create not smeared particle
66  erhic::ParticleMC not_smeared_prt;
67  not_smeared_prt.SetId(pdg); // PDG particle code
68  not_smeared_prt.SetIndex(particle_index++); // Particle index in event
69  not_smeared_prt.SetStatus(1); // Particle status code: like in PYTHIA, Beagle, etc
70  not_smeared_prt.Set4Vector(input_vect);
71 
72  // Smear the particle
73  Smear::ParticleMCS * smeared_prt;
74  smeared_prt = detector.Smear(not_smeared_prt);
75 
76  if(!smeared_prt) {
77  step.result = null_particle;
78  return step;
79  }
80  step.smeared_lorentz = smeared_prt->Get4Vector();
81 
82  // Check odds of eic-smear smearing
83  double in_p = input_vect.P();
84  double sm_p = smeared_prt->GetP();
85  double in_e = input_vect.E();
86  double sm_e = smeared_prt->GetE();
87 
88  // Eic smear return non zero p
89  bool zero_p = TMath::Abs(in_p)>0.001 && TMath::Abs(sm_p)<0.00001;
90  bool zero_e = TMath::Abs(in_e)>0.001 && TMath::Abs(sm_e)<0.00001;
91 
92  if(zero_p && zero_e) {
93  step.result = zero_e_zero_p; // we don't take such particle
94  }
95  else if (zero_p) {
96  step.result = smear_e_zero_p; // we don't take such particle
97  }
98  else if (zero_e) {
99  step.result = zero_e_smear_p; // we don't take such particle
100  }
101  else {
102  step.result = smear_e_smear_p;
103  }
104  return step;
105 }
106 
107 
108 // This function sends
110 
111  using namespace std;
112 
113  // Get the inputs needed for this factory.
114  TDatabasePDG* db = TDatabasePDG::Instance();
115 
116  TParticlePDG* pdg_particle = db->GetParticle(pdg);
117  EicSmearStatistics stat;
118  stat.total_particles=0;
119  stat.null_particles=0;
120  stat.zero_e_smear_p=0;
121  stat.smear_e_zero_p=0;
122  stat.smear_e_smear_p=0;
123  stat.zero_e_zero_p=0;
124 
125  stat.null_particles_eta = new TH1D("null_particles_eta", "Unsmeared particles;#eta;counts", 200, -5, 5 );
126  stat.zero_e_smear_p_eta = new TH1D("zero_e_smear_p_eta", "only p smeared;#eta;counts", 100, -5, 5 );
127  stat.smear_e_zero_p_eta = new TH1D("smear_e_zero_p_eta", "only e smeared;#eta;counts", 100, -5, 5 );
128  stat.zero_e_zero_p_eta = new TH1D("zero_e_zero_p_eta", "Unsmeared particles;#eta;counts", 100, -5, 5 );
129  stat.smear_e_smear_p_eta = new TH1D("smear_e_smear_p_eta", "smeared e, smeared p;#eta;counts", 100, -5, 5 );
130 
131  stat.smear_DelP = new TH2D("smear_DelP","#Delta p/p;p;#Delta p/p", 100, 0, 20, 100, -0.1,0.1);
132 
133  int angleinc = 1;
134  for(int mom=1; mom < 20; mom+=2) {
135  for(int angle_deg=angleinc; angle_deg < 180; angle_deg+=angleinc) { // theta
136  // 4 vector
137  TLorentzVector input_vect(0, 0, mom, sqrt ( mom*mom + pow(pdg_particle->Mass(),2)) );
138  input_vect.RotateX(angle_deg*deg_to_rad);
139  // input_vect.Print();
140 
141  EicSmearStep step = DoSmearStep(pdg, input_vect, detector);
142 
143  // Update statistics
144  switch(step.result) {
145  case null_particle:
146  stat.null_particles++;
147  stat.null_particles_eta->Fill(input_vect.Eta());
148  break;
149  case zero_e_smear_p:
150  stat.zero_e_smear_p++;
151  stat.zero_e_smear_p_eta->Fill(input_vect.Eta());
152  stat.smear_DelP->Fill(input_vect.P(), (input_vect.P() - step.smeared_lorentz.P()) / input_vect.P());
153  break;
154  case smear_e_zero_p:
155  stat.smear_e_zero_p++;
156  stat.smear_e_zero_p_eta->Fill(input_vect.Eta());
157  break;
158  case zero_e_zero_p:
159  stat.zero_e_zero_p++;
160  stat.zero_e_zero_p_eta->Fill(input_vect.Eta());
161  break;
162  case smear_e_smear_p:
163  stat.smear_e_smear_p++;
164  stat.smear_e_smear_p_eta->Fill(input_vect.Eta());
165  stat.smear_DelP->Fill(input_vect.P(), (input_vect.P() - step.smeared_lorentz.P()) / input_vect.P());
166  break;
167  }
168 
169  stat.steps.push_back(step);
170  }
171  }
172  return stat;
173 }
174 
175 
177  using namespace std;
178  cout<<"SmearingFactory statistics:\n";
179  cout << " total_particles = " << setw(8) << stat.total_particles << " \n";
180  cout << " null_particles = " << setw(8) << stat.null_particles << " (not smeared)\n";
181  cout << " zero_e_zero_p = " << setw(8) << stat.zero_e_zero_p << " (not smeared)\n";
182  cout << " zero_e_smear_p = " << setw(8) << stat.zero_e_smear_p << " (partly smeared)\n";
183  cout << " smear_e_zero_p = " << setw(8) << stat.smear_e_zero_p << " (partly smeared)\n";
184  cout << " smear_e_smear_p = " << setw(8) << stat.smear_e_smear_p << " (smeared)\n";
185 }
186 
187 
188 
189 int main() {
190  int pid = 211; // pi+
191  // int pid = 11; // e-
192  // int pid = 22; // gamma
193  // int pid = 2112; // n
194  // int pid = 2212; // p
195  // int pid = 13; // mu
196  // int pid = 12; // neutrino
197 
198  std::string detstring = "Matrix";
199  // detstring = "HandBook";
200  // detstring = "Perfect";
201  // detstring = "BeAST";
202  // detstring = "JLEIC";
203  // detstring = "ePhenix";
204  // detstring = "ZEUS";
205  // detstring = "TOF";
206  for (auto & c: detstring) c = toupper(c);
207 
208  Smear::Detector detector;
209  if ( TString(detstring).Contains("MATRIX") && TString(detstring).Contains("FF")){
210  const int beam_mom_nn=100;
211  detector = BuildByName(detstring, beam_mom_nn);
212  } else {
213  detector = BuildByName(detstring);
214  }
215 
216  if ( detector.GetNDevices() == 0 ) {
217  std::cerr << "Detector sepcified as " << detstring
218  << " not recognized or empty." << std::endl;
219  return -1;
220  }
221 
222  EicSmearStatistics stat = Process( pid, detector);
223  PrintSmearStats(stat);
224 
225  gStyle->SetOptStat(0);
226  gStyle->SetHistLineWidth(2);
227  float lMargin = 0.12;
228  float bMargin = 0.12;
229  gStyle->SetLabelSize(.05, "XY");
230  gStyle->SetTitleSize(.05, "XY");
231  gStyle->SetTitleOffset(1.1,"x"); //X-axis title offset from axis
232  gStyle->SetTitleOffset(1.1,"y"); //Y-axis title offset from axis
233 
234  new TCanvas;
235  TString title = detstring;
236  title += ", pid="; title +=pid;
237  TLegend * leg = new TLegend( 0.65, 0.65, 0.95, 0.95, title);
238 
239  double ymax = stat.smear_e_smear_p_eta->GetMaximum();
240  ymax = std::max ( ymax, stat.zero_e_smear_p_eta->GetMaximum() );
241  ymax = std::max ( ymax, stat.smear_e_zero_p_eta->GetMaximum() );
242  ymax = std::max ( ymax, stat.null_particles_eta->GetMaximum() );
243 
244  stat.smear_e_smear_p_eta->SetAxisRange( 0, ymax*1.1, "y");
245  stat.smear_e_smear_p_eta->Draw("AXIS");
246 
247  stat.smear_e_smear_p_eta->SetLineColor(kGreen+1);
248  stat.smear_e_smear_p_eta->Draw("same");
249 
250  stat.zero_e_smear_p_eta->SetLineColor(kBlue);
251  stat.zero_e_smear_p_eta->Draw("same");
252 
253  stat.smear_e_zero_p_eta->SetLineColor(kBlack);
254  stat.smear_e_zero_p_eta->Draw("same");
255 
256  stat.null_particles_eta->SetLineColor(kRed);
257  stat.null_particles_eta->Draw("same");
258 
259  leg->AddEntry( stat.zero_e_smear_p_eta, "P smeared", "l");
260  leg->AddEntry( stat.smear_e_zero_p_eta, "E smeared", "l");
261  leg->AddEntry( stat.smear_e_smear_p_eta, "Both smeared", "l");
262  leg->AddEntry( stat.null_particles_eta, "NONE smeared", "l");
263  leg->Draw("same");
264 
265  TString outname=detstring;
266  outname += "_"; outname+=pid;
267  outname += "_eta.png";
268  gPad->SaveAs(outname);
269  gPad->SaveAs("etaplot.png"); // for quick looks at the most recent one
270 
271  new TCanvas;
272  stat.smear_DelP->SetTitle(title);
273  stat.smear_DelP->Draw("colz");
274  outname=detstring;
275  outname += "_"; outname+=pid;
276  outname += "_DelP.png";
277  gPad->SaveAs(outname);
278  gPad->SaveAs("DelPplot.png"); // for quick looks at the most recent one
279 
280  return 0;
281 }