Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Proto4SampleFrac.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Proto4SampleFrac.C
1 #include "Proto4SampleFrac.h"
2 
3 #include <g4main/PHG4Hit.h>
5 #include <g4main/PHG4Particle.h>
7 #include <g4main/PHG4VtxPoint.h>
8 
9 #include <calobase/RawTowerContainer.h>
10 #include <calobase/RawTowerGeomContainer.h>
11 #include <calobase/RawTower.h>
12 #include <calobase/RawClusterContainer.h>
13 #include <calobase/RawCluster.h>
14 
15 #include <g4eval/CaloEvalStack.h>
18 #include <g4eval/CaloTruthEval.h>
19 #include <g4eval/SvtxEvalStack.h>
20 
21 #include <fun4all/SubsysReco.h>
22 #include <fun4all/Fun4AllServer.h>
23 #include <fun4all/PHTFileServer.h>
25 
26 #include <phool/getClass.h>
27 #include <phool/PHCompositeNode.h>
28 
29 #include <TString.h>
30 #include <TFile.h>
31 #include <TH1F.h>
32 #include <TH2F.h>
33 #include <TH3F.h>
34 #include <TVector3.h>
35 #include <TLorentzVector.h>
36 #include <TMath.h>
37 
38 #include <exception>
39 #include <stdexcept>
40 #include <iostream>
41 #include <vector>
42 #include <set>
43 #include <algorithm>
44 #include <cassert>
45 #include <cmath>
46 
47 using namespace std;
48 
50  : SubsysReco("Proto4SampleFrac_" + calo_name),
51  _is_sim(true),
52  _filename(filename),
53  _calo_name(calo_name),
54  _calo_hit_container(nullptr), _calo_abs_hit_container(nullptr), _truth_container(nullptr)
55 {
56  Verbosity(1);
57 }
58 
60 {
61 }
62 
64 {
66  Fun4AllHistoManager *hm = se->getHistoManager("Proto4SampleFrac_HISTOS");
67 
68  if (not hm)
69  {
70  cout
71  << "Proto4SampleFrac::get_HistoManager - Making Fun4AllHistoManager Proto4SampleFrac_HISTOS"
72  << endl;
73  hm = new Fun4AllHistoManager("Proto4SampleFrac_HISTOS");
74  se->registerHistoManager(hm);
75  }
76 
77  assert(hm);
78 
79  return hm;
80 }
81 
82 
84 {
85  return "h_QAG4Sim_" + string(_calo_name);
86 }
87 
89 {
90  if (Verbosity())
91  cout << "Proto4SampleFrac::InitRun" << endl;
92 
93  PHNodeIterator iter(topNode);
94  PHCompositeNode *dstNode = static_cast<PHCompositeNode *>(iter.findFirst(
95  "PHCompositeNode", "DST"));
96  assert(dstNode);
97 
98  if (!dstNode)
99  {
100  std::cerr << PHWHERE << "DST Node missing, doing nothing." << std::endl;
101  throw runtime_error(
102  "Failed to find DST node in EmcRawTowerBuilder::CreateNodes");
103  }
104 
105  _calo_hit_container = findNode::getClass<PHG4HitContainer>(topNode,
106  "G4HIT_" + _calo_name);
107  if (!_calo_hit_container)
108  {
109  cout << "Proto4SampleFrac::InitRun - Fatal Error - "
110  << "unable to find DST node " << "G4HIT_" + _calo_name << endl;
112  }
113 
114  _calo_abs_hit_container = findNode::getClass<PHG4HitContainer>(topNode,
115  "G4HIT_ABSORBER_" + _calo_name);
117  {
118  cout << "Proto4SampleFrac::InitRun - Fatal Error - "
119  << "unable to find DST node " << "G4HIT_ABSORBER_" + _calo_name
120  << endl;
122  }
123 
124  _truth_container = findNode::getClass<PHG4TruthInfoContainer>(topNode,
125  "G4TruthInfo");
126  if (!_truth_container)
127  {
128  cout << "Proto4SampleFrac::InitRun - Fatal Error - "
129  << "unable to find DST node " << "G4TruthInfo" << endl;
131  }
132 
134 }
135 
137 {
138  cout << "Proto4SampleFrac::End - write to " << _filename << endl;
140 
142  assert(hm);
143  for (unsigned int i = 0; i < hm->nHistos(); i++)
144  hm->getHisto(i)->Write();
145 
146  // if (_T_EMCalTrk)
147  // _T_EMCalTrk->Write();
148 
150 }
151 
153 {
154  cout << "Proto4SampleFrac::get_HistoManager - Making PHTFileServer "
155  << _filename << endl;
156  PHTFileServer::get().open(_filename, "RECREATE");
157 
159  assert(hm);
160 
161  TH1D * h_info = new TH1D(TString(get_histo_prefix()) + "_Normalization", //
162  TString(_calo_name) + " Normalization;Items;Count", 10, .5, 10.5);
163  int i = 1;
164  h_info->GetXaxis()->SetBinLabel(i++, "Event");
165  h_info->GetXaxis()->SetBinLabel(i++, "G4Hit Active");
166  h_info->GetXaxis()->SetBinLabel(i++, "G4Hit Absor.");
167  h_info->GetXaxis()->LabelsOption("v");
168  hm->registerHisto(h_info);
169 
170  hm->registerHisto(
171  new TH2F(TString(get_histo_prefix()) + "_G4Hit_RZ", //
172  TString(_calo_name) + " RZ projection;Z (cm);R (cm)", 1200, -300, 300,
173  600, 0, 300));
174 
175  hm->registerHisto(
176  new TH2F(TString(get_histo_prefix()) + "_G4Hit_XY_cal", //
177  TString(_calo_name) + " XY projection;X (cm);Y (cm)", 3000, 50, 350,
178  2000, -100, 100));
179  // TString(_calo_name) + " XY projection;X (cm);Y (cm)", 1200, -300, 300,
180  // 1200, -300, 300));
181 
182  hm->registerHisto(
183  new TH2F(TString(get_histo_prefix()) + "_G4Hit_XY_abs", //
184  TString(_calo_name) + " XY projection;X (cm);Y (cm)", 3000, 50, 350,
185  2000, -100, 100));
186 
187  hm->registerHisto(
188  new TH2F(TString(get_histo_prefix()) + "_G4Hit_Mat_XY_cal", //
189  TString(_calo_name) + " XY projection;X (cm);Y (cm)", 3000, 50, 350,
190  2000, -100, 100));
191 
192  hm->registerHisto(
193  new TH2F(TString(get_histo_prefix()) + "_G4Hit_Mat_XY_abs", //
194  TString(_calo_name) + " XY projection;X (cm);Y (cm)", 3000, 50, 350,
195  2000, -100, 100));
196 
197  hm->registerHisto(
198  new TH2F(TString(get_histo_prefix()) + "_G4Hit_LateralTruthProjection", //
199  TString(_calo_name)
200  + " shower lateral projection (last primary);Polar direction (cm);Azimuthal direction (cm)",
201  200, -30, 30, 200, -30, 30));
202 
203  hm->registerHisto(new TH1F(TString(get_histo_prefix()) + "_G4Hit_SF", //
204  TString(_calo_name) + " sampling fraction;Sampling fraction", 1000, 0, 1.0));
205 
206  hm->registerHisto(
207  new TH1F(TString(get_histo_prefix()) + "_G4Hit_VSF", //
208  TString(_calo_name)
209  + " visible sampling fraction;Visible sampling fraction", 1000, 0, 1.0));
210 
211  TH1F * h =
212  new TH1F(TString(get_histo_prefix()) + "_G4Hit_HitTime", //
213  TString(_calo_name)
214  + " hit time (edep weighting);Hit time - T0 (ns);Geant4 energy density",
215  1000, 0.5, 10000);
216  hm->registerHisto(h);
217 
218  hm->registerHisto(
219  new TH1F(TString(get_histo_prefix()) + "_G4Hit_FractionTruthEnergy", //
220  TString(_calo_name)
221  + " fraction truth energy ;G4 energy (active + absorber) / total truth energy",
222  1000, 0, 1));
223 
224  hm->registerHisto(
225  new TH1F(TString(get_histo_prefix()) + "_G4Hit_FractionEMVisibleEnergy", //
226  TString(_calo_name)
227  + " fraction visible energy from EM; visible energy from e^{#pm} / total visible energy",
228  100, 0, 1));
229 
231 }
232 
234 {
235  if (Verbosity() > 2) cout << "Proto4SampleFrac::process_event() entered" << endl;
236 
237  TH1F *h = nullptr;
238 
240  assert(hm);
241 
242  TH1D* h_info = dynamic_cast<TH1D*>(hm->getHisto(get_histo_prefix() + "_Normalization"));
243  assert(h_info);
244 
245  // get primary
247  PHG4TruthInfoContainer::ConstRange primary_range =
249  double total_primary_energy = 1e-9; //make it zero energy epsilon samll so it can be used for denominator
250  for (PHG4TruthInfoContainer::ConstIterator particle_iter = primary_range.first;
251  particle_iter != primary_range.second; ++particle_iter)
252  {
253  const PHG4Particle *particle = particle_iter->second;
254  assert(particle);
255  total_primary_energy += particle->get_e();
256  }
257 
258  assert(not _truth_container->GetMap().empty());
259  const PHG4Particle * last_primary = _truth_container->GetMap().rbegin()->second;
260  assert(last_primary);
261 
262  if (Verbosity() > 2)
263  {
264  cout
265  << "QAG4SimulationCalorimeter::process_event_G4Hit() handle this truth particle"
266  << endl;
267  last_primary->identify();
268  }
269  const PHG4VtxPoint* primary_vtx = //
270  _truth_container->GetPrimaryVtx(last_primary->get_vtx_id());
271  assert(primary_vtx);
272  if (Verbosity() > 2)
273  {
274  cout
275  << "QAG4SimulationCalorimeter::process_event_G4Hit() handle this vertex"
276  << endl;
277  primary_vtx->identify();
278  }
279 
280  const double t0 = primary_vtx->get_t();
281  const TVector3 vertex(primary_vtx->get_x(), primary_vtx->get_y(),
282  primary_vtx->get_z());
283 
284  // projection axis
285  TVector3 axis_proj(last_primary->get_px(), last_primary->get_py(),
286  last_primary->get_pz());
287  if (axis_proj.Mag() == 0)
288  axis_proj.SetXYZ(0, 0, 1);
289  axis_proj = axis_proj.Unit();
290 
291  // azimuthal direction axis
292  TVector3 axis_azimuth = axis_proj.Cross(TVector3(0, 0, 1));
293  if (axis_azimuth.Mag() == 0)
294  axis_azimuth.SetXYZ(1, 0, 0);
295  axis_azimuth = axis_azimuth.Unit();
296 
297  // polar direction axis
298  TVector3 axis_polar = axis_proj.Cross(axis_azimuth);
299  assert(axis_polar.Mag() > 0);
300  axis_polar = axis_polar.Unit();
301 
302  double e_calo = 0.0; // active energy deposition
303  double ev_calo = 0.0; // visible energy
304  double ea_calo = 0.0; // absorber energy
305  double ev_calo_em = 0.0; // EM visible energy
306 
308  {
309  TH2F * hrz = dynamic_cast<TH2F*>(hm->getHisto(
310  get_histo_prefix() + "_G4Hit_RZ"));
311  assert(hrz);
312 
313  TH2F * hxy_cal = dynamic_cast<TH2F*>(hm->getHisto(
314  get_histo_prefix() + "_G4Hit_XY_cal"));
315  assert(hxy_cal);
316 
317  TH2F * hmat_xy_cal = dynamic_cast<TH2F*>(hm->getHisto(
318  get_histo_prefix() + "_G4Hit_Mat_XY_cal"));
319  assert(hmat_xy_cal);
320 
321  TH1F * ht = dynamic_cast<TH1F*>(hm->getHisto(
322  get_histo_prefix() + "_G4Hit_HitTime"));
323  assert(ht);
324 
325  TH2F * hlat = dynamic_cast<TH2F*>(hm->getHisto(
326  get_histo_prefix() + "_G4Hit_LateralTruthProjection"));
327  assert(hlat);
328 
329  h_info->Fill("G4Hit Active", _calo_hit_container->size());
330  PHG4HitContainer::ConstRange calo_hit_range =
332  for (PHG4HitContainer::ConstIterator hit_iter = calo_hit_range.first;
333  hit_iter != calo_hit_range.second; hit_iter++)
334  {
335 
336  PHG4Hit *this_hit = hit_iter->second;
337  assert(this_hit);
338 
339  e_calo += this_hit->get_edep();
340  ev_calo += this_hit->get_light_yield();
341 
342  // EM visible energy that is only associated with electron energy deposition
344  this_hit->get_trkid());
345  if (!particle)
346  {
347  cout <<__PRETTY_FUNCTION__<<" - Error - this PHG4hit missing particle: "; this_hit -> identify();
348  }
349  assert(particle);
350  if (abs(particle->get_pid()) == 11)
351  ev_calo_em += this_hit->get_light_yield();
352 
353  const TVector3 hit(this_hit->get_avg_x(), this_hit->get_avg_y(),
354  this_hit->get_avg_z());
355 
356  hrz->Fill(hit.Z(), hit.Perp(), this_hit->get_edep());
357  hxy_cal->Fill(hit.X(), hit.Y(), this_hit->get_edep());
358  ht->Fill(this_hit->get_avg_t() - t0, this_hit->get_edep());
359 
360  const double hit_azimuth = axis_azimuth.Dot(hit - vertex);
361  const double hit_polar = axis_polar.Dot(hit - vertex);
362  hlat->Fill(hit_polar, hit_azimuth, this_hit->get_edep());
363 
364  const TVector3 hit_entry(this_hit->get_x(0), this_hit->get_y(0), this_hit->get_z(0));
365  const TVector3 hit_exit(this_hit->get_x(1), this_hit->get_y(1), this_hit->get_z(1));
366  hmat_xy_cal->Fill(hit_entry.X(),hit_entry.Y());
367  hmat_xy_cal->Fill(hit_exit.X(),hit_exit.Y());
368  }
369  }
370 
372  {
373  h_info->Fill("G4Hit Absor.", _calo_abs_hit_container->size());
374 
375  TH2F * hxy_abs = dynamic_cast<TH2F*>(hm->getHisto(
376  get_histo_prefix() + "_G4Hit_XY_abs"));
377  assert(hxy_abs);
378 
379  TH2F * hmat_xy_abs = dynamic_cast<TH2F*>(hm->getHisto(
380  get_histo_prefix() + "_G4Hit_Mat_XY_abs"));
381  assert(hmat_xy_abs);
382 
383  PHG4HitContainer::ConstRange calo_abs_hit_range =
385  for (PHG4HitContainer::ConstIterator hit_iter = calo_abs_hit_range.first;
386  hit_iter != calo_abs_hit_range.second; hit_iter++)
387  {
388 
389  PHG4Hit *this_hit = hit_iter->second;
390  assert(this_hit);
391 
392  ea_calo += this_hit->get_edep();
393 
394  const TVector3 hit(this_hit->get_avg_x(), this_hit->get_avg_y(),
395  this_hit->get_avg_z());
396 
397  hxy_abs->Fill(hit.X(), hit.Y(), this_hit->get_edep());
398 
399  const TVector3 hit_entry(this_hit->get_x(0), this_hit->get_y(0), this_hit->get_z(0));
400  const TVector3 hit_exit(this_hit->get_x(1), this_hit->get_y(1), this_hit->get_z(1));
401  hmat_xy_abs->Fill(hit_entry.X(),hit_entry.Y());
402  hmat_xy_abs->Fill(hit_exit.X(),hit_exit.Y());
403  }
404  }
405 
406  if (Verbosity() > 3)
407  cout << "QAG4SimulationCalorimeter::process_event_G4Hit::" << _calo_name
408  << " - SF = " << e_calo / (e_calo + ea_calo + 1e-9) << ", VSF = "
409  << ev_calo / (e_calo + ea_calo + 1e-9) << endl;
410 
411  if (e_calo + ea_calo > 0)
412  {
413  h = dynamic_cast<TH1F*>(hm->getHisto(get_histo_prefix() + "_G4Hit_SF"));
414  assert(h);
415  h->Fill(e_calo / (e_calo + ea_calo));
416 
417  h = dynamic_cast<TH1F*>(hm->getHisto(get_histo_prefix() + "_G4Hit_VSF"));
418  assert(h);
419  h->Fill(ev_calo / (e_calo + ea_calo));
420  }
421 
422  h = dynamic_cast<TH1F*>(hm->getHisto(
423  get_histo_prefix() + "_G4Hit_FractionTruthEnergy"));
424  assert(h);
425  h->Fill((e_calo + ea_calo) / total_primary_energy);
426 
427  if (ev_calo > 0)
428  {
429  h = dynamic_cast<TH1F*>(hm->getHisto(
430  get_histo_prefix() + "_G4Hit_FractionEMVisibleEnergy"));
431  assert(h);
432  h->Fill(ev_calo_em / (ev_calo));
433  }
434 
435  if (Verbosity() > 3)
436  cout << "QAG4SimulationCalorimeter::process_event_G4Hit::" << _calo_name
437  << " - histogram " << h->GetName() << " Get Sum = " << h->GetSum()
438  << endl;
439 
440 
441  // at the end, count success events
442  h_info->Fill("Event", 1);
443 
445 }
446 
447 pair<int, int>
449 {
450  const int clus_edge_size = (cluster_size - 1) / 2;
451  assert(clus_edge_size >= 0);
452 
453  pair<int, int> max(-1000, -1000);
454  double max_e = 0;
455 
456  for (int col = 0; col < n_size; ++col)
457  for (int row = 0; row < n_size; ++row)
458  {
459  double energy = 0;
460 
461  for (int dcol = col - clus_edge_size; dcol <= col + clus_edge_size;
462  ++dcol)
463  for (int drow = row - clus_edge_size; drow <= row + clus_edge_size;
464  ++drow)
465  {
466  if (dcol < 0 or drow < 0)
467  continue;
468 
469  RawTower *t = towers->getTower(dcol, drow);
470  if (t)
471  energy += t->get_energy();
472  }
473 
474  if (energy > max_e)
475  {
476  max = make_pair(col, row);
477  max_e = energy;
478  }
479  }
480 
481  return max;
482 }