Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PHG4DSTReader.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PHG4DSTReader.cc
1 // $Id: PHG4DSTReader.cc,v 1.11 2015/01/06 02:52:07 jinhuang Exp $
2 
11 #include "PHG4DSTReader.h"
12 
15 
16 #include <calobase/RawTower.h>
17 #include <calobase/RawTowerContainer.h>
18 #include <calobase/RawTowerv1.h>
19 
20 #include <g4main/PHG4Hit.h>
21 #include <g4main/PHG4HitEval.h>
22 #include <g4main/PHG4Particle.h>
23 #include <g4main/PHG4Particlev2.h>
24 #include <g4main/PHG4VtxPoint.h>
25 #include <g4main/PHG4VtxPointv1.h>
26 
27 #include <jetbase/Jet.h>
28 #include <jetbase/JetContainer.h>
29 #include <jetbase/Jetv2.h>
30 
32 #include <fun4all/PHTFileServer.h>
33 #include <fun4all/SubsysReco.h>
34 
35 #include <phool/getClass.h>
36 
37 #include <TClass.h>
38 #include <TClonesArray.h>
39 #include <TObject.h>
40 #include <TString.h>
41 #include <TTree.h>
42 
43 #include <cassert>
44 #include <iostream>
45 #include <set>
46 #include <sstream>
47 #include <utility>
48 
54 
56  : SubsysReco("PHG4DSTReader")
57  , _out_file_name(filename)
58 {
59  // TODO Auto-generated constructor stub
60 }
61 
63 {
64  if (Verbosity() > 1)
65  {
66  std::cout << "PHG4DSTReader::destructor - Clean ups" << std::endl;
67  }
68  if (_T)
69  {
70  _T->ResetBranchAddresses();
71  }
72 
73  _records.clear();
74 }
75 
77 {
78  const int arr_size = 100;
79 
80  // BOOST_FOREACH(std::string nodenam, _node_postfix)
81  for (const auto &nodenam : _node_postfix)
82  {
83  const char *class_name = hit_type::Class()->GetName();
84 
85  std::string hname = Form("G4HIT_%s", nodenam.c_str());
86  // _node_name.push_back(hname);
87  if (Verbosity() > 0)
88  {
89  std::cout << "PHG4DSTReader::Init - saving hits from node: " << hname << " - "
90  << class_name << std::endl;
91  }
92 
93  record rec;
94  rec._cnt = 0;
95  rec._name = hname;
96  rec._arr = boost::make_shared<TClonesArray>(class_name, arr_size);
97  rec._arr_ptr = rec._arr.get();
98  rec._type = record::typ_hit;
99 
100  _records.push_back(rec);
101 
102  nblocks++;
103  }
104 
105  if (_tower_postfix.size() && Verbosity() > 0)
106  {
107  std::cout << "PHG4DSTReader::Init - zero suppression for calorimeter towers = "
108  << _tower_zero_sup << " GeV" << std::endl;
109  }
110  for (const auto &nodenam : _tower_postfix)
111  {
112  const char *class_name = RawTower_type::Class()->GetName();
113 
114  std::string hname = Form("TOWER_%s", nodenam.c_str());
115  // _node_name.push_back(hname);
116  if (Verbosity() > 0)
117  {
118  std::cout << "PHG4DSTReader::Init - saving raw tower info from node: " << hname
119  << " - " << class_name << std::endl;
120  }
121  record rec;
122  rec._cnt = 0;
123  rec._name = hname;
124  rec._arr = boost::make_shared<TClonesArray>(class_name, arr_size);
125  rec._arr_ptr = rec._arr.get();
126  rec._type = record::typ_tower;
127 
128  _records.push_back(rec);
129 
130  nblocks++;
131  }
132 
133  for (const auto &hname : _jet_postfix)
134  {
135  const char *class_name = PHPyJet_type::Class()->GetName();
136 
137  if (Verbosity() > 0)
138  {
139  std::cout << "PHG4DSTReader::Init - saving jets from node: " << hname << " - "
140  << class_name << std::endl;
141  }
142  record rec;
143  rec._cnt = 0;
144  rec._name = hname;
145  rec._arr = boost::make_shared<TClonesArray>(class_name, arr_size);
146  rec._arr_ptr = rec._arr.get();
147  rec._type = record::typ_jets;
148 
149  _records.push_back(rec);
150 
151  nblocks++;
152  }
153 
154  if (_save_particle)
155  {
156  // save particles
157 
158  const char *class_name = part_type::Class()->GetName();
159 
160  if (Verbosity() > 0)
161  {
162  std::cout << "PHG4DSTReader::Init - saving Particles node:" << class_name
163  << std::endl;
164  }
165  record rec;
166  rec._cnt = 0;
167  rec._name = "PHG4Particle";
168  rec._arr = boost::make_shared<TClonesArray>(class_name, arr_size);
169  rec._arr_ptr = rec._arr.get();
170  rec._type = record::typ_part;
171 
172  _records.push_back(rec);
173 
174  nblocks++;
175  }
176 
177  if (_save_vertex)
178  {
179  // save particles
180 
181  const char *class_name = vertex_type::Class()->GetName();
182 
183  if (Verbosity() > 0)
184  {
185  std::cout << "PHG4DSTReader::Init - saving vertex for particles under node:"
186  << class_name << std::endl;
187  }
188  record rec;
189  rec._cnt = 0;
190  rec._name = "PHG4VtxPoint";
191  rec._arr = boost::make_shared<TClonesArray>(class_name, arr_size);
192  rec._arr_ptr = rec._arr.get();
194 
195  _records.push_back(rec);
196 
197  nblocks++;
198  }
199  if (Verbosity() > 0)
200  {
201  std::cout << "PHG4DSTReader::Init - requested " << nblocks << " nodes" << std::endl;
202  }
203  build_tree();
204 
205  return 0;
206 }
207 
209 {
210  if (Verbosity() > 0)
211  {
212  std::cout << "PHG4DSTReader::build_tree - output to " << _out_file_name << std::endl;
213  }
214  static const int BUFFER_SIZE = 32000;
215 
216  // open TFile
217  PHTFileServer::get().open(_out_file_name, "RECREATE");
218 
219  _T = new TTree("T", "PHG4DSTReader");
220 
221  nblocks = 0;
222  for (auto &rec : _records)
223  {
224  if (Verbosity() > 0)
225  {
226  std::cout << "PHG4DSTReader::build_tree - Add " << rec._name << std::endl;
227  }
228  const std::string name_cnt = "n_" + rec._name;
229  const std::string name_cnt_desc = name_cnt + "/I";
230  _T->Branch(name_cnt.c_str(), &(rec._cnt), name_cnt_desc.c_str(),
231  BUFFER_SIZE);
232  _T->Branch(rec._name.c_str(), &(rec._arr_ptr), BUFFER_SIZE, 99);
233 
234  nblocks++;
235  }
236 
237  if (Verbosity() > 0)
238  {
239  std::cout << "PHG4DSTReader::build_tree - added " << nblocks << " nodes" << std::endl;
240  }
241  _T->SetAutoSave(16000);
242 }
243 
245 {
246  // const double significand = _event / TMath::Power(10, (int) (log10(_event)));
247  //
248  // if (fmod(significand, 1.0) == 0 && significand <= 10)
249  // std::cout << "PHG4DSTReader::process_event - " << _event << std::endl;
250  _event++;
251 
252  // clean ups
253  _particle_set.clear();
254  _vertex_set.clear();
255 
257  PHG4TruthInfoContainer>(topNode, "G4TruthInfo");
258  if (!truthInfoList)
259  {
260  if (_event < 2)
261  {
262  std::cout
263  << "PHG4DSTReader::process_event - Error - can not find node G4TruthInfo. Quit processing!"
264  << std::endl;
265  }
267  }
268 
269  for (auto &rec : _records)
270  {
271  rec._cnt = 0;
272  assert(rec._arr.get() == rec._arr_ptr);
273  assert(rec._arr.get());
274  rec._arr->Clear();
275 
276  if (rec._type == record::typ_hit)
277  {
278  if (Verbosity() >= 2)
279  {
280  std::cout << "PHG4DSTReader::process_event - processing " << rec._name
281  << std::endl;
282  }
283 
284  PHG4HitContainer *hits = findNode::getClass<PHG4HitContainer>(topNode,
285  rec._name);
286  if (!hits)
287  {
288  if (_event < 2)
289  {
290  std::cout
291  << "PHG4DSTReader::process_event - Error - can not find node "
292  << rec._name << std::endl;
293  }
294  }
295  else
296  {
297  PHG4HitContainer::ConstRange hit_range = hits->getHits();
298 
299  if (Verbosity() >= 2)
300  {
301  std::cout << "PHG4DSTReader::process_event - processing "
302  << rec._name << " and received " << hits->size() << " hits"
303  << std::endl;
304  }
305 
306  for (PHG4HitContainer::ConstIterator hit_iter = hit_range.first;
307  hit_iter != hit_range.second; hit_iter++)
308  {
309  PHG4Hit *hit = hit_iter->second;
310 
311  // hit_type * hit = dynamic_cast<hit_type *>(hit_raw);
312  //
313  assert(hit);
314 
315  new ((*(rec._arr.get()))[rec._cnt]) hit_type(hit);
316 
317  hit_type *new_hit =
318  dynamic_cast<hit_type *>(rec._arr.get()->At(rec._cnt));
319  assert(new_hit);
320 
321  // for (int i = 0; i < 2; i++)
322  // {
323  // new_hit->set_x(i, hit->get_x(i));
324  // new_hit->set_y(i, hit->get_y(i));
325  // new_hit->set_z(i, hit->get_z(i));
326  // new_hit->set_t(i, hit->get_t(i));
327  // }
328  // new_hit->set_edep(hit->get_edep());
329  // new_hit->set_layer(hit->get_layer());
330  // new_hit->set_hit_id(hit->get_hit_id());
331  // new_hit->set_trkid(hit->get_trkid());
332 
333  // *new_hit = (*hit);
334 
336  {
337  _particle_set.insert(hit->get_trkid());
338  }
339 
340  if (Verbosity() >= 2)
341  {
342  std::cout << "PHG4DSTReader::process_event - processing "
343  << rec._name << " and hit " << hit->get_hit_id()
344  << " with track id " << hit->get_trkid() << std::endl;
345  }
346 
347  rec._cnt++;
348  }
349  } // if (!hits)
350  } // if (rec._type == record::typ_hit)
351  else if (rec._type == record::typ_tower)
352  {
353  if (Verbosity() >= 2)
354  {
355  std::cout << "PHG4DSTReader::process_event - processing tower "
356  << rec._name << std::endl;
357  }
358 
359  RawTowerContainer *hits = findNode::getClass<RawTowerContainer>(
360  topNode, rec._name);
361  if (!hits)
362  {
363  if (_event < 2)
364  {
365  std::cout
366  << "PHG4DSTReader::process_event - Error - can not find node "
367  << rec._name << std::endl;
368  }
369  }
370  else
371  {
372  RawTowerContainer::ConstRange hit_range = hits->getTowers();
373 
374  if (Verbosity() >= 2)
375  {
376  std::cout << "PHG4DSTReader::process_event - processing "
377  << rec._name << " and received " << hits->size()
378  << " tower hits" << std::endl;
379  }
380 
381  for (RawTowerContainer::ConstIterator hit_iter = hit_range.first;
382  hit_iter != hit_range.second; hit_iter++)
383  {
384  RawTower *hit_raw = hit_iter->second;
385 
386  RawTower_type *hit = dynamic_cast<RawTower_type *>(hit_raw);
387  // RawTower * hit = hit_iter->second;
388 
389  assert(hit);
390 
391  if (hit->get_energy() < _tower_zero_sup)
392  {
393  if (Verbosity() >= 2)
394  {
395  std::cout
396  << "PHG4DSTReader::process_event - suppress low energy tower hit "
397  << rec._name << " @ ("
398  // << hit->get_thetaMin()
399  // << ", " << hit->get_phiMin()
400  << "), Energy = " << hit->get_energy() << std::endl;
401  }
402 
403  continue;
404  }
405 
406  new ((*(rec._arr.get()))[rec._cnt]) RawTower_type();
407 
408  if (Verbosity() >= 2)
409  {
410  std::cout
411  << "PHG4DSTReader::process_event - processing Tower hit "
412  << rec._name << " @ ("
413  // << hit->get_thetaMin() << ", "
414  // << hit->get_phiMin()
415  << "), Energy = " << hit->get_energy() << " - "
416  << rec._arr.get()->At(rec._cnt)->ClassName() << std::endl;
417  }
418 
419  // rec._arr->Print();
420 
421  RawTower_type *new_hit =
422  dynamic_cast<RawTower_type *>(rec._arr.get()->At(rec._cnt));
423  assert(new_hit);
424 
425  // //v1 copy
426  // new_hit->get_bineta(hit->get_bineta());
427  // new_hit->get_binphi(hit->get_binphi());
428  //
429  // //v2 copy
430  // new_hit->set_thetaMin(hit->get_thetaMin());
431  // new_hit->set_thetaSize(hit->get_thetaSize());
432  // new_hit->set_phiMin(hit->get_phiMin());
433  // new_hit->set_phiSize(hit->get_phiSize());
434  // new_hit->set_zMin(hit->get_zMin());
435  // new_hit->set_zSize(hit->get_zSize());
436 
437  *new_hit = (*hit);
438 
439  rec._cnt++;
440  }
441  } // if (!hits)
442  } // if (rec._type == record::typ_hit)
443  else if (rec._type == record::typ_jets)
444  {
445  // std::std::cout
446  // << "PHG4DSTReader::AddJet - Error - temp. disabled until jet added back to sPHENIX software"
447  // << std::std::endl;
448  //
449  if (Verbosity() >= 2)
450  {
451  std::cout << "PHG4DSTReader::process_event - processing jets "
452  << rec._name << std::endl;
453  }
454 
455  JetContainer *hits = findNode::getClass<JetContainer>(topNode, rec._name);
456  if (!hits)
457  {
458  if (_event < 2)
459  {
460  std::cout
461  << "PHG4DSTReader::process_event - Error - can not find node "
462  << rec._name << std::endl;
463  }
464  }
465  else
466  {
467  if (Verbosity() >= 2)
468  {
469  std::cout << "PHG4DSTReader::process_event - processing "
470  << rec._name << " and received " << hits->size() << " jets"
471  << std::endl;
472  }
473 
474  // for every recojet
475  for (auto hit_raw : *hits)
476  {
477  /* Jet *hit_raw = iter.second; */
478 
479  if (Verbosity() >= 2)
480  {
481  std::cout << "PHG4DSTReader::process_event - processing jet "
482  << rec._name << " @ (" << hit_raw->get_eta() << ", "
483  << hit_raw->get_phi() << "), pT = " << hit_raw->get_pt()
484  << " - with raw type " << hit_raw->ClassName() << std::endl;
485  }
486 
487  PHPyJet_type *hit = dynamic_cast<PHPyJet_type *>(hit_raw);
488 
489  assert(hit);
490 
491  new ((*(rec._arr.get()))[rec._cnt]) PHPyJet_type();
492 
493  PHPyJet_type *new_hit =
494  dynamic_cast<PHPyJet_type *>(rec._arr.get()->At(rec._cnt));
495  assert(new_hit);
496 
497  *new_hit = (Jetv2) (*hit);
498 
499  rec._cnt++;
500  }
501  } // if (!hits)
502  } // if (rec._type == record::typ_hit)
503  else if (rec._type == record::typ_part)
504  {
505  if (_load_all_particle)
506  {
507  static bool once = true;
508  if (once)
509  {
510  std::cout
511  << "PHG4DSTReader::process_event - will load all particle from G4TruthInfo"
512  << std::endl;
513 
514  once = false;
515  }
516 
517  for (auto particle_iter : truthInfoList->GetMap())
518  {
519  _particle_set.insert(particle_iter.first);
520  }
521 
522  } // if (_load_all_particle)
523  else
524  {
525  static bool once = true;
526  if (once)
527  {
528  if (Verbosity() > 0)
529  {
530  std::cout << "PHG4DSTReader::process_event - will load primary particle from G4TruthInfo" << std::endl;
531  }
532  once = false;
533  }
534 
535  PHG4TruthInfoContainer::ConstRange primary_range =
536  truthInfoList->GetPrimaryParticleRange();
537 
538  for (PHG4TruthInfoContainer::ConstIterator particle_iter =
539  primary_range.first;
540  particle_iter != primary_range.second;
541  ++particle_iter)
542  {
543  _particle_set.insert(particle_iter->first);
544 
545  } // if (_load_all_particle) else
546  }
547  for (int i : _particle_set)
548  {
549  auto particle = truthInfoList->GetParticle(i);
550  if (!particle)
551  {
552  std::cout
553  << "PHG4DSTReader::process_event - ERROR - can not find particle/track ID "
554  << i << " in G4TruthInfo" << std::endl;
555 
556  continue;
557  }
558 
559  add_particle(rec, particle);
560  } // for(PartSet_t::const_iterator i = _particle_set.begin();i!=_particle_set.end();i++)
561 
562  } // if (rec._type == record::typ_part)
563  else if (rec._type == record::typ_vertex)
564  {
565  static bool once = true;
566  if (once)
567  {
568  if (Verbosity() > 0)
569  {
570  std::cout << "PHG4DSTReader::process_event - will load vertex from G4TruthInfo" << std::endl;
571  }
572  once = false;
573  }
574 
575  for (int i : _vertex_set)
576  {
577  PHG4VtxPoint *v = truthInfoList->GetVtx(i);
578  if (!v)
579  {
580  std::cout
581  << "PHG4DSTReader::process_event - ERROR - can not find vertex ID "
582  << i << " in G4TruthInfo" << std::endl;
583 
584  continue;
585  }
586 
587  new ((*(rec._arr.get()))[rec._cnt]) vertex_type();
588 
589  if (Verbosity() >= 2)
590  {
591  std::cout << "PHG4DSTReader::process_event - saving vertex id "
592  << i << std::endl;
593  }
594 
595  vertex_type *new_v =
596  static_cast<vertex_type *>(rec._arr.get()->At(rec._cnt));
597  assert(new_v);
598 
599  new_v->set_x(v->get_x());
600  new_v->set_y(v->get_y());
601  new_v->set_z(v->get_z());
602  new_v->set_t(v->get_t());
603  new_v->set_id(v->get_id());
604 
605  rec._cnt++;
606  } // else if (rec._type == record::typ_vertex)
607 
608  } // if (_load_all_particle)
609 
610  } // for (records_t::iterator it = _records.begin(); it != _records.end(); ++it)
611 
612  if (_T)
613  {
614  _T->Fill();
615  }
616 
617  return 0;
618 } // for (records_t::iterator it = _records.begin(); it != _records.end(); ++it)
619 
621 {
622  assert(part);
623 
624  // if (Verbosity() >= 2)
625  // std::cout << "PHG4DSTReader::process_event - Particle type is "
626  // << p_raw->GetName() << " - " << p_raw->ClassName()
627  // << " get_track_id = " << p_raw->get_track_id() << std::endl;
628 
629  // part_type * part = dynamic_cast<part_type *>(p_raw);
630 
631  // assert(part);
632 
633  new ((*(rec._arr.get()))[rec._cnt]) part_type();
634 
635  part_type *new_part = static_cast<part_type *>(rec._arr.get()->At(rec._cnt));
636  assert(new_part);
637 
638  new_part->set_track_id(part->get_track_id());
639  new_part->set_vtx_id(part->get_vtx_id());
640  new_part->set_parent_id(part->get_parent_id());
641  new_part->set_primary_id(part->get_primary_id());
642  new_part->set_name(part->get_name());
643  new_part->set_pid(part->get_pid());
644  new_part->set_px(part->get_px());
645  new_part->set_py(part->get_py());
646  new_part->set_pz(part->get_pz());
647  new_part->set_e(part->get_e());
648 
649  _vertex_set.insert(part->get_vtx_id());
650 
651  rec._cnt++;
652 }
653 
655 {
656  if (Verbosity() > 1)
657  {
658  std::cout << "PHG4DSTReader::End - Clean ups" << std::endl;
659  }
660 
661  if (_T)
662  {
664  _T->Write();
665  _T->ResetBranchAddresses();
666  }
667 
668  _records.clear();
669 
670  return 0;
671 }