Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HFMLTriggerInterface.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file HFMLTriggerInterface.C
1 #include "HFMLTriggerInterface.h"
2 
4 #include <pdbcalbase/PdbParameterMap.h>
6 #include <phool/PHTimeServer.h>
7 #include <phool/PHTimer.h>
8 #include <phool/getClass.h>
9 
10 #include <phool/PHCompositeNode.h>
11 
12 #include <g4main/PHG4Hit.h>
13 #include <g4main/PHG4Particle.h>
16 
17 //#include <trackbase/TrkrHitTruthAssoc.h>
19 //#include <trackbase/TrkrClusterHitAssoc.h>
20 #include <trackbase/TrkrHitSet.h>
21 #include <trackbase/TrkrHit.h>
22 //#include <trackbase/TrkrCluster.h>
23 
24 #include <mvtx/MvtxDefs.h>
25 #include <mvtx/CylinderGeom_Mvtx.h>
26 
27 #include <g4detectors/PHG4Cell.h>
33 
34 //#include <g4eval/SvtxEvalStack.h>
35 
36 #include <HepMC/GenEvent.h>
37 #include <HepMC/GenVertex.h>
40 
41 #include <TDatabasePDG.h>
42 #include <TFile.h>
43 #include <TH2F.h>
44 #include <TH3F.h>
45 #include <TLorentzVector.h>
46 #include <TString.h>
47 #include <TTree.h>
48 
49 #include <rapidjson/document.h>
50 #include <rapidjson/ostreamwrapper.h>
51 #include <rapidjson/prettywriter.h>
52 
53 #include <boost/format.hpp>
54 #include <boost/property_tree/json_parser.hpp>
55 #include <boost/property_tree/ptree.hpp>
56 #include <boost/property_tree/xml_parser.hpp>
57 
58 
59 #include <cassert>
60 #include <cmath>
61 #include <cstddef>
62 #include <iostream>
63 
64 using namespace std;
65 
67  : SubsysReco("HFMLTriggerInterface")
68  , _ievent(0)
69  , _f(nullptr)
70  , _eta_min(-1)
71  , _eta_max(1)
72  , _embedding_id(1)
73  , _nlayers_maps(3)
74  , m_hitsets(nullptr)
75  , m_GenEventMap(nullptr)
76  , m_truthInfo(nullptr)
77  , m_g4hits_mvtx(nullptr)
78  // , _svtxevalstack(nullptr)
79  // , m_hit_truth_map(nullptr)
80  , m_Flags(nullptr)
81  , m_Geoms(nullptr)
82  , m_hitStaveLayer(nullptr)
83  , m_hitModuleHalfStave(nullptr)
84  , m_hitChipModule(nullptr)
85  , m_hitLayerMap(nullptr)
86  , m_hitPixelPhiMap(nullptr)
87  , m_hitPixelPhiMapHL(nullptr)
88  , m_hitPixelZMap(nullptr)
89 {
91 }
92 
94 {
95  _ievent = 0;
96 
97  _f = new TFile((_foutname + string(".root")).c_str(), "RECREATE");
98 
99  m_jsonOut.open((_foutname + string(".json")).c_str(), fstream::out);
100 
101  m_jsonOut << "{" << endl;
102  m_jsonOut << "\"Events\" : [" << endl;
103 
104  // _h2 = new TH2D("h2", "", 100, 0, 100.0, 40, -2, +2);
105  // _h2_b = new TH2D("h2_b", "", 100, 0, 100.0, 40, -2, +2);
106  // _h2_c = new TH2D("h2_c", "", 100, 0, 100.0, 40, -2, +2);
107  // _h2all = new TH2D("h2all", "", 100, 0, 100.0, 40, -2, +2);
108 
109  m_hitLayerMap = new TH3F("hitLayerMap", "hitLayerMap", 600, -10, 10, 600, -10, 10, 10, -.5, 9.5);
110  m_hitLayerMap->SetTitle("hitLayerMap;x [mm];y [mm];Half Layers");
111 
112  m_hitPixelPhiMap = new TH3F("hitPixelPhiMap", "hitPixelPhiMap", 16000, -.5, 16000 - .5, 600, -M_PI, M_PI, 10, -.5, 9.5);
113  m_hitPixelPhiMap->SetTitle("hitPixelPhiMap;PixelPhiIndex in layer;phi [rad];Half Layers Index");
114  m_hitPixelPhiMapHL = new TH3F("hitPixelPhiMapHL", "hitPixelPhiMapHL", 16000, -.5, 16000 - .5, 600, -M_PI, M_PI, 10, -.5, 9.5);
115  m_hitPixelPhiMapHL->SetTitle("hitPixelPhiMap;PixelPhiIndex in half layer;phi [rad];Half Layers Index");
116  m_hitPixelZMap = new TH3F("hitPixelZMap", "hitPixelZMap", 16000, -.5, 16000 - .5, 600, 15, 15, 10, -.5, 9.5);
117  m_hitPixelZMap->SetTitle("hitPixelZMap;hitPixelZMap;z [cm];Half Layers");
118 
119  m_hitStaveLayer = new TH2F("hitStaveLayer", "hitStaveLayer", 100, -.5, 100 - .5, 10, -.5, 9.5);
120  m_hitStaveLayer->SetTitle("hitStaveLayer;Stave index;Half Layers");
121  m_hitModuleHalfStave = new TH2F("hitModuleHalfStave", "hitModuleHalfStave", 100, -.5, 100 - .5, 10, -.5, 9.5);
122  m_hitModuleHalfStave->SetTitle("hitModuleHalfStave;Module index;Half Stave");
123  m_hitChipModule = new TH2F("hitChipModule", "hitChipModule", 100, -.5, 100 - .5, 10, -.5, 9.5);
124  m_hitChipModule->SetTitle("hitChipModule;Chip;Module");
125 
127 }
128 
130 {
131  m_Geoms =
132  findNode::getClass<PHG4CylinderGeomContainer>(topNode, "CYLINDERGEOM_MVTX");
133  if (!m_Geoms)
134  {
135  std::cout << PHWHERE << "ERROR: Can't find node CYLINDERCELLGEOM_MVTX" << std::endl;
137  }
138 
139  m_truthInfo = findNode::getClass<PHG4TruthInfoContainer>(topNode, "G4TruthInfo");
140  if (!m_truthInfo)
141  {
142  std::cout << PHWHERE << "ERROR: Can't find node G4TruthInfo" << std::endl;
144  }
145 
146  m_Flags = findNode::getClass<PdbParameterMap>(topNode, "HFMLTrigger_HepMCTriggerFlags");
147  if (!m_Flags)
148  {
149  cout << "HFMLTriggerInterface::InitRun - WARNING - missing HFMLTrigger_HepMCTriggerFlags" << endl;
150  }
151 
153 }
154 
156 {
157  // load nodes
158  auto res = load_nodes(topNode);
159  if (res != Fun4AllReturnCodes::EVENT_OK)
160  return res;
161 
162 
163  // if (!_svtxevalstack)
164  // {
165  // _svtxevalstack = new SvtxEvalStack(topNode);
166  // _svtxevalstack->set_strict(0);
167  // _svtxevalstack->set_verbosity(Verbosity() + 1);
168  // }
169  // else
170  // {
171  // _svtxevalstack->next_event(topNode);
172  // }
173 
174  // SvtxVertexEval* vertexeval = _svtxevalstack->get_vertex_eval();
175  // SvtxTrackEval* trackeval = _svtxevalstack->get_track_eval();
176  // SvtxClusterEval* clustereval = _svtxevalstack->get_cluster_eval();
177  //SvtxHitEval* hiteval = _svtxevalstack->get_hit_eval();
178  // SvtxTruthEval* trutheval = _svtxevalstack->get_truth_eval();
179 
181 
183  if (!genevt)
184  {
185  std::cout << PHWHERE << " - Fatal error - node PHHepMCGenEventMap missing subevent with embedding ID " << _embedding_id;
186  std::cout << ". Print PHHepMCGenEventMap:";
189  }
190 
191  HepMC::GenEvent* theEvent = genevt->getEvent();
192  assert(theEvent);
193  if (Verbosity())
194  {
195  cout << "HFMLTriggerInterface::process_event - process HepMC::GenEvent with signal_process_id = "
196  << theEvent->signal_process_id();
197  if (theEvent->signal_process_vertex())
198  {
199  cout << " and signal_process_vertex : ";
200  theEvent->signal_process_vertex()->print();
201  }
202  cout << " - Event record:" << endl;
203  theEvent->print();
204  }
205 
206  // property tree preparation
207  // using boost::property_tree::ptree;
208  // ptree pt;
209 
210  rapidjson::Document d;
211  d.SetObject();
212  rapidjson::Document::AllocatorType& alloc = d.GetAllocator();
213 
214  auto loadCoordinate = [&](double x, double y, double z) {
215  // ptree vertexTree;
216  rapidjson::Value vertexTree(rapidjson::kArrayType);
217 
218  // ptree vertexX;
219  // vertexX.put("", x);
220  // vertexTree.push_back(make_pair("", vertexX));
221 
222  // ptree vertexY;
223  // vertexY.put("", y);
224  // vertexTree.push_back(make_pair("", vertexY));
225 
226  // ptree vertexZ;
227  // vertexZ.put("", z);
228  // vertexTree.push_back(make_pair("", vertexZ));
229 
230  vertexTree.PushBack(x, alloc).PushBack(y, alloc).PushBack(z, alloc);
231 
232  return vertexTree;
233  };
234 
235  // Create a root
236  // ptree pTree;
237 
238  // meta data
239  // ptree metaTree;
240  rapidjson::Value metaTree(rapidjson::kObjectType);
241 
242  metaTree.AddMember("Description", "These are meta data for this event. Not intended to use in ML algorithm", alloc);
243  metaTree.AddMember("EventID", _ievent, alloc);
244  metaTree.AddMember("Unit", "cm", alloc);
245  metaTree.AddMember("CollisionVertex",
246  loadCoordinate(genevt->get_collision_vertex().x(),
247  genevt->get_collision_vertex().y(),
248  genevt->get_collision_vertex().z()),
249  alloc);
250 
251  metaTree.AddMember("Layer_Count", _nlayers_maps, alloc);
252  metaTree.AddMember("PixelHalfLayerIndex_Count", _nlayers_maps * 2, alloc);
253 
254  for (unsigned int layer = 0; layer < _nlayers_maps; ++layer)
255  {
257  assert(geom);
258 
259  // ptree layerDescTree;
260  rapidjson::Value layerDescTree(rapidjson::kObjectType);
261 
262  static const unsigned int nChip(9);
263 
264  layerDescTree.AddMember("PixelPhiIndexInLayer_Count", geom->get_N_staves() * geom->get_NX(), alloc);
265  layerDescTree.AddMember("PixelPhiIndexInHalfLayer_Count", geom->get_N_staves() * geom->get_NX() / 2, alloc);
266  layerDescTree.AddMember("PixelZIndex_Count", nChip * geom->get_NZ(), alloc);
267  layerDescTree.AddMember("HalfLayer_Count", 2, alloc);
268  layerDescTree.AddMember("Stave_Count", geom->get_N_staves(), alloc);
269  layerDescTree.AddMember("Chip_Count", nChip, alloc);
270  layerDescTree.AddMember("Pixel_Count", geom->get_NX() * geom->get_NZ(), alloc);
271 
272  // metaTree.AddMember(
273  // str(boost::format{"Layer%1%"} % layer).c_str(),
274  // layerDescTree, alloc);
275  rapidjson::Value keyName(str(boost::format{"Layer%1%"} % layer).c_str(), alloc);
276  metaTree.AddMember(keyName,
277  layerDescTree, alloc);
278  }
279 
280  // ptree truthTriggerFlagTree;
281  rapidjson::Value truthTriggerFlagTree(rapidjson::kObjectType);
282 
283  truthTriggerFlagTree.AddMember("Description",
284  "These are categorical true/false MonteCalo truth tags for the event. These are only known in training sample. This would be trigger output in real data processing.",
285  alloc);
286  // truthTriggerFlagTree.AddMember("ExampleSignal1", true, alloc);
287  // truthTriggerFlagTree.AddMember("ExampleSignal2", false, alloc);
288  rapidjson::Value flagsTree(rapidjson::kObjectType);
289  if (m_Flags)
290  {
291  auto range = m_Flags->get_iparam_iters();
292 
293  for (auto flagIter = range.first; flagIter != range.second; ++flagIter)
294  {
295 // rapidjson::Value aFlag(rapidjson::kObjectType);
296 
297  const string& name = flagIter->first;
298  rapidjson::Value keyName(name.c_str(), alloc);
299  const bool flag = flagIter->second > 0 ? true : false;
300 
301  flagsTree.AddMember(keyName, flag, alloc);
302 // flagsTree.PushBack(aFlag, alloc);
303  }
304  }
305  truthTriggerFlagTree.AddMember("Flags", flagsTree, alloc);
306 
307  // Raw hits
308  // ptree rawHitTree;
309  rapidjson::Value rawHitTree(rapidjson::kObjectType);
310  rawHitTree.AddMember("Description",
311  "Raw data in the event in an unordered set of hit ID. To help in visualization stage, the coordinate of the hit is also appended. These would be input in real data.",
312  alloc);
313 
314  // rawHitTree.put("LayerRage", "0-2");
315 
316  // ptree rawHitsTree;
317  rapidjson::Value rawHitsTree(rapidjson::kArrayType);
318 
319  assert(m_hitsets);
320  unsigned int hitID(0);
321  auto hitset_range = m_hitsets->getHitSets(TrkrDefs::TrkrId::mvtxId);
322  for (auto hitset_iter = hitset_range.first; hitset_iter != hitset_range.second; ++hitset_iter)
323  {
324  auto hitsetkey = hitset_iter->first;
325  auto hit_range = hitset_iter->second->getHits();
326  for (auto hit_iter = hit_range.first;
327  hit_iter != hit_range.second;
328  ++hit_iter)
329  {
330  auto hitkey = hit_iter->first;
331  auto hit = hit_iter->second;
332  assert(hit);
333  unsigned int layer = TrkrDefs::getLayer(hitset_iter->first);
334  if (layer < _nlayers_maps)
335  {
336  CylinderGeom_Mvtx* geom = dynamic_cast<CylinderGeom_Mvtx*>(m_Geoms->GetLayerGeom(layer));
337  assert(geom);
338 
339  unsigned int pixel_x = MvtxDefs::getRow(hitkey);
340  unsigned int pixel_z = MvtxDefs::getCol(hitkey);
341  assert((int)pixel_x < geom->get_NX());
342  assert((int)pixel_z < geom->get_NZ());
343  unsigned int chip = MvtxDefs::getChipId(hitsetkey);
344  unsigned int stave = MvtxDefs::getStaveId(hitsetkey);
345  TVector3 local_coords = geom->get_local_coords_from_pixel(pixel_x, pixel_z);
346  TVector3 world_coords = geom->get_world_from_local_coords(stave,
347  0,
348  0,
349  chip,
350  local_coords);
351 
352  //unsigned int halflayer = (int) pixel_x >= geom->get_NX() / 2 ? 0 : 1;
353 
354  //unsigned int halfLayerIndex(layer * 2 + halflayer);
355  //unsigned int pixelPhiIndex(
356  // cell->get_stave_index() * geom->get_NX() + pixel_x);
357  //unsigned int pixelPhiIndexHL(
358  // cell->get_stave_index() * geom->get_NX() / 2 + pixel_x % (geom->get_NX() / 2));
359  // unsigned int pixelZIndex(cell->get_chip_index() * geom->get_NZ() + pixel_z);
360 
361  // ptree hitTree;
362  rapidjson::Value hitTree(rapidjson::kObjectType);
363 
364  // ptree hitIDTree;
365  rapidjson::Value hitIDTree(rapidjson::kObjectType);
366  hitIDTree.AddMember("HitSequenceInEvent", hitID, alloc);
367 
368  //hitIDTree.AddMember("PixelHalfLayerIndex", halfLayerIndex, alloc);
369  //hitIDTree.AddMember("PixelPhiIndexInLayer", pixelPhiIndex, alloc);
370  //hitIDTree.AddMember("PixelPhiIndexInHalfLayer", pixelPhiIndexHL, alloc);
371  //hitIDTree.AddMember("PixelZIndex", pixelZIndex, alloc);
372 
373  hitIDTree.AddMember("Layer", layer, alloc);
374  //hitIDTree.AddMember("HalfLayer", halflayer, alloc);
375  hitIDTree.AddMember("Stave", stave, alloc);
376  // hitIDTree.put("HalfStave", cell->get_half_stave_index());
377  // hitIDTree.put("Module", cell->get_module_index());
378  hitIDTree.AddMember("Chip", chip, alloc);
379  hitIDTree.AddMember("Pixel_x", pixel_x, alloc);
380  hitIDTree.AddMember("Pixel_z", pixel_z, alloc);
381  hitTree.AddMember("ID", hitIDTree, alloc);
382 
383  hitTree.AddMember("Coordinate",
384  loadCoordinate(world_coords.x(),
385  world_coords.y(),
386  world_coords.z()),
387  alloc);
388 
389  // rawHitsTree.add_child("MVTXHit", hitTree);
390  rawHitsTree.PushBack(hitTree, alloc);
391 
392  m_hitStaveLayer->Fill(stave, layer);
393  m_hitModuleHalfStave->Fill(stave, layer);
394  m_hitChipModule->Fill(chip, stave);
395 
396  m_hitLayerMap->Fill(world_coords.x(), world_coords.y(), layer);
397  //m_hitPixelPhiMap->Fill(pixelPhiIndex, atan2(world_coords.y(), world_coords.x()), layer);
398  //m_hitPixelPhiMapHL->Fill(pixelPhiIndexHL, atan2(world_coords.y(), world_coords.x()), layer);
399  //m_hitPixelZMap->Fill(pixelZIndex, world_coords.z(), halfLayerIndex);
400  ++hitID;
401  } // if (layer < _nlayers_maps)
402  } // for (TrkrHitSetContainer::ConstIterator hitset_iter = hitset_range.first;
403  } // for (TrkrHitSet::ConstIterator hit_iter = hit_range.first;
404  rawHitTree.AddMember("MVTXHits", rawHitsTree, alloc);
405 
406  // Truth hits
407  // ptree truthHitTree;
408  rapidjson::Value truthHitTree(rapidjson::kObjectType);
409  truthHitTree.AddMember("Description", "From the MonteCalo truth information, pairs of track ID and subset of RawHit that belong to the track. These are not presented in real data. The track ID is arbitary.",
410  alloc);
411 
413  std::multimap<const int, const unsigned long long> m_track_g4hits_map;
414  for (auto g4hit_iter = m_g4hits_mvtx->getHits().first;
415  g4hit_iter != m_g4hits_mvtx->getHits().second;
416  ++g4hit_iter)
417  {
418  PHG4Hit *g4hit = g4hit_iter->second;
419  m_track_g4hits_map.insert(make_pair(g4hit->get_trkid(), g4hit_iter->first));
420  }
421 
422  // ptree truthTracksTree;
423  rapidjson::Value truthTracksTree(rapidjson::kArrayType);
424 
425  // get set of primary particle
427  auto pp_range = m_truthInfo->GetPrimaryParticleRange();
428  for (auto pp_iter = pp_range.first;
429  pp_iter != pp_range.second;
430  ++pp_iter)
431  {
432  PHG4Particle *g4particle = pp_iter->second;
433  assert(g4particle);
434 
435  if (! m_track_g4hits_map.count(g4particle->get_track_id()))
436  {
437  if (Verbosity() >= VERBOSITY_MORE)
438  std::cout << "WARNING: G4 particle " << g4particle->get_track_id() \
439  << " does not hit any MVTX layer." << std::endl;
440  continue;
441  }
442 
443  // ptree trackHitTree;
444  rapidjson::Value trackHitTree(rapidjson::kArrayType);
445  //unsigned int nMAPS(0);
446  auto g4hits_iter = m_track_g4hits_map.equal_range(g4particle->get_track_id());
447  for (auto& g4hit_iter = g4hits_iter.first;
448  g4hit_iter != g4hits_iter.second; ++g4hit_iter)
449  {
450  // ptree hitIDTree;
451  // hitIDTree.put("", g4hit_key);
452  trackHitTree.PushBack(static_cast<uint64_t>(g4hit_iter->second), alloc);
453  } // for (auto& clus_key : clus_keys)
454 
455  // ptree trackTree;
456  rapidjson::Value trackTree(rapidjson::kObjectType);
457  trackTree.AddMember("TrackSequenceInEvent", g4particle->get_track_id(), alloc);
458  trackTree.AddMember("HitSequenceInEvent", trackHitTree, alloc);
459 
460  trackTree.AddMember("ParticleTypeID", g4particle->get_pid(), alloc);
461  trackTree.AddMember("TrackMomentum",
462  loadCoordinate(g4particle->get_px(),
463  g4particle->get_py(),
464  g4particle->get_pz()),
465  alloc);
466  // trackTree.put("TrackDCA3DXY", track->get_dca3d_xy());
467  // trackTree.put("TrackDCA3DZ", track->get_dca3d_z());
468 
469  // trackTree.add_child("TruthHit", trackHitTree);
470 
471  // truthTracksTree.add_child("TruthTrack", trackTree);
472  truthTracksTree.PushBack(trackTree, alloc);
473  } // for (PHG4TruthInfoContainer::ConstIterator iter = range.first;
474 
475  truthHitTree.AddMember("TruthTracks", truthTracksTree, alloc);
476 
477  //output
478  d.AddMember("MetaData", metaTree, alloc);
479  d.AddMember("TruthTriggerFlag", truthTriggerFlagTree, alloc);
480  d.AddMember("RawHit", rawHitTree, alloc);
481  d.AddMember("TruthHit", truthHitTree, alloc);
482 
483  assert(m_jsonOut.is_open());
484 
485  if (_ievent > 0)
486  {
487  m_jsonOut << "," << endl;
488  }
489 
490  // write_json(m_jsonOut, pTree);
491  // write_xml(m_jsonOut, jsonTree);
492 
493  // d.AddMember("Test", 1, d.GetAllocator());
494 
495  rapidjson::OStreamWrapper osw(m_jsonOut);
496  rapidjson::PrettyWriter<rapidjson::OStreamWrapper> writer(osw);
497 
498  d.Accept(writer);
499 
500  ++_ievent;
501 
503 }
504 
506 {
507  if (_f)
508  {
509  _f->cd();
510  _f->Write();
511  }
512 
513  if (m_jsonOut.is_open())
514  {
515  m_jsonOut << "]" << endl;
516  m_jsonOut << "}" << endl;
517 
518  m_jsonOut.close();
519  }
520 
521  cout << "HFMLTriggerInterface::End - output to " << _foutname << ".*" << endl;
522 
524 }
525 
527 {
528 
529  m_hitsets = findNode::getClass<TrkrHitSetContainer>(topNode, "TRKR_HITSET");
530  if (!m_hitsets)
531  {
532  std::cout << PHWHERE << "ERROR: Can't find node TRKR_HITSET" << std::endl;
534  }
535 
536  //m_hit_truth_map = findNode::getClass<TrkrHitTruthAssoc>(topNode, "TRKR_HITTRUTHASSOC");
537  //if (!m_hit_truth_map)
538  //{
539  // std::cout << PHWHERE << " unable to find DST node TRKR_HITTRUTHASSOC" << std::endl;
540  // return Fun4AllReturnCodes::ABORTEVENT;
541  //}
542 
543  //m_cluster_hit_map = findNode::getClass<TrkrClusterHitAssoc>(topNode, "TRKR_CLUSTERHITASSOC");
544  //if (!m_cluster_hit_map)
545  //{
546  // std::cout << PHWHERE << " unable to find DST node TRKR_CLUSTERHITASSOC" << std::endl;
547  // return Fun4AllReturnCodes::ABORTEVENT;
548  //}
549 
550  m_g4hits_mvtx = findNode::getClass<PHG4HitContainer>(topNode, "G4HIT_MVTX");
551  if (!m_g4hits_mvtx)
552  {
553  std::cout << PHWHERE << " unable to find DST node G4HIT_MVTX" << std::endl;
555  }
556 
557  m_GenEventMap = findNode::getClass<PHHepMCGenEventMap>(topNode, "PHHepMCGenEventMap");
558  if (!m_GenEventMap)
559  {
560  std::cout << PHWHERE << " - Fatal error - missing node PHHepMCGenEventMap" << std::endl;
562  }
563 
565 }