Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HFMLTriggerOccupancy.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file HFMLTriggerOccupancy.C
1 #include "HFMLTriggerOccupancy.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>
14 #include <g4main/PHG4VtxPoint.h>
16 
17 #include <g4mvtx/PHG4CylinderGeom_MVTX.h>
18 
19 #include <g4detectors/PHG4Cell.h>
25 
26 #include <trackbase_historic/SvtxCluster.h>
27 #include <trackbase_historic/SvtxClusterMap.h>
28 #include <trackbase_historic/SvtxHit.h>
29 #include <trackbase_historic/SvtxHitMap.h>
32 #include <trackbase_historic/SvtxVertex.h>
33 #include <trackbase_historic/SvtxVertexMap.h>
34 
35 #include <g4eval/SvtxEvalStack.h>
36 
37 #include <HepMC/GenEvent.h>
38 #include <HepMC/GenVertex.h>
41 
42 #include <TDatabasePDG.h>
43 #include <TFile.h>
44 #include <TH1D.h>
45 #include <TH1F.h>
46 #include <TH2F.h>
47 #include <TH3F.h>
48 #include <TLorentzVector.h>
49 #include <TString.h>
50 #include <TTree.h>
51 
52 #include <rapidjson/document.h>
53 #include <rapidjson/ostreamwrapper.h>
54 #include <rapidjson/prettywriter.h>
55 
56 #include <boost/format.hpp>
57 #include <boost/property_tree/json_parser.hpp>
58 #include <boost/property_tree/ptree.hpp>
59 #include <boost/property_tree/xml_parser.hpp>
60 
61 #include <cassert>
62 #include <cmath>
63 #include <cstddef>
64 #include <iostream>
65 
66 using namespace std;
67 
69  : SubsysReco("HFMLTriggerOccupancy")
70  , _ievent(0)
71  , _f(nullptr)
72  , _eta_min(-1)
73  , _eta_max(1)
74  , _embedding_id(1)
75  , _nlayers_maps(3)
76  , _svtxevalstack(nullptr)
77  , m_hitMap(nullptr)
78  , m_Geoms(nullptr)
79  , m_truthInfo(nullptr)
80  , m_Flags(nullptr)
81  , m_hNorm(nullptr)
82  , m_hNChEta(nullptr)
83  , m_hVertexZ(nullptr)
84  , m_hitStaveLayer(nullptr)
85  , m_hitModuleHalfStave(nullptr)
86  , m_hitChipModule(nullptr)
87  , m_hitLayerMap(nullptr)
88  , m_hitPixelPhiMap(nullptr)
89  , m_hitPixelPhiMapHL(nullptr)
90  , m_hitPixelZMap(nullptr)
91  , m_Multiplicity(nullptr)
92  , m_LayerMultiplicity(nullptr)
93  , m_LayerChipMultiplicity(nullptr)
94 {
96 }
97 
99 {
100  _ievent = 0;
101 
102  _f = new TFile((_foutname + string(".root")).c_str(), "RECREATE");
103 
104  // m_jsonOut.open((_foutname + string(".json")).c_str(), fstream::out);
105  //
106  // m_jsonOut << "{" << endl;
107  // m_jsonOut << "\"Events\" : [" << endl;
108 
109  // _h2 = new TH2D("h2", "", 100, 0, 100.0, 40, -2, +2);
110  // _h2_b = new TH2D("h2_b", "", 100, 0, 100.0, 40, -2, +2);
111  // _h2_c = new TH2D("h2_c", "", 100, 0, 100.0, 40, -2, +2);
112  // _h2all = new TH2D("h2all", "", 100, 0, 100.0, 40, -2, +2);
113 
114  m_hNorm = new TH1F("hNormalization", //
115  "Normalization;Items;Summed quantity", 10, .5, 10.5);
116  int i = 1;
117  m_hNorm->GetXaxis()->SetBinLabel(i++, "IntegratedLumi");
118  m_hNorm->GetXaxis()->SetBinLabel(i++, "Event");
119  m_hNorm->GetXaxis()->LabelsOption("v");
120 
121  m_hNChEta = new TH1F("hNChEta", //
122  "Charged particle #eta distribution;#eta;Count",
123  1000, -5, 5);
124  m_hVertexZ = new TH1F("hVertexZ", //
125  "Vertex z distribution;z [cm];Count",
126  1000, -200, 200);
127 
128  m_hitLayerMap = new TH3F("hitLayerMap", "hitLayerMap", 600, -10, 10, 600, -10, 10, 10, -.5, 9.5);
129  m_hitLayerMap->SetTitle("hitLayerMap;x [mm];y [mm];Half Layers");
130 
131 // m_hitPixelPhiMap = new TH3F("hitPixelPhiMap", "hitPixelPhiMap", 16000, -.5, 16000 - .5, 600, -M_PI, M_PI, 10, -.5, 9.5);
132 // m_hitPixelPhiMap->SetTitle("hitPixelPhiMap;PixelPhiIndex in layer;phi [rad];Half Layers Index");
133 // m_hitPixelPhiMapHL = new TH3F("hitPixelPhiMapHL", "hitPixelPhiMapHL", 16000, -.5, 16000 - .5, 600, -M_PI, M_PI, 10, -.5, 9.5);
134 // m_hitPixelPhiMapHL->SetTitle("hitPixelPhiMap;PixelPhiIndex in half layer;phi [rad];Half Layers Index");
135 // m_hitPixelZMap = new TH3F("hitPixelZMap", "hitPixelZMap", 16000, -.5, 16000 - .5, 600, 15, 15, 10, -.5, 9.5);
136 // m_hitPixelZMap->SetTitle("hitPixelZMap;hitPixelZMap;z [cm];Half Layers");
137 
138  m_hitStaveLayer = new TH2F("hitStaveLayer", "hitStaveLayer", 100, -.5, 100 - .5, 10, -.5, 9.5);
139  m_hitStaveLayer->SetTitle("hitStaveLayer;Stave index;Half Layers");
140  m_hitModuleHalfStave = new TH2F("hitModuleHalfStave", "hitModuleHalfStave", 100, -.5, 100 - .5, 10, -.5, 9.5);
141  m_hitModuleHalfStave->SetTitle("hitModuleHalfStave;Module index;Half Stave");
142  m_hitChipModule = new TH2F("hitChipModule", "hitChipModule", 100, -.5, 100 - .5, 10, -.5, 9.5);
143  m_hitChipModule->SetTitle("hitChipModule;Chip;Module");
144 
145  m_Multiplicity = new TH1F("Multiplicity", "Multiplicity", 10000, -.5, 10000 - .5);
146  m_Multiplicity->SetTitle("Multiplicity;Chip Multiplicity;Count");
147 
148  m_LayerMultiplicity = new TH2F("LayerMultiplicity", "LayerMultiplicity", 3, -.5, 2.5, 1000, -.5, 1000 - .5);
149  m_LayerMultiplicity->SetTitle("LayerMultiplicity;Layer;Chip Multiplicity");
150 
151  m_LayerChipMultiplicity = new TH3F("LayerChipMultiplicity", "LayerChipMultiplicity", 3, -.5, 2.5, kNCHip, -.5, kNCHip - .5, 1000, -.5, 1000 - .5);
152  m_LayerChipMultiplicity->SetTitle("LayerChipMultiplicity;Layer;Chip");
153 
155 }
156 
158 {
159  m_hitMap = findNode::getClass<SvtxHitMap>(topNode, "SvtxHitMap");
160  if (!m_hitMap)
161  {
162  std::cout << PHWHERE << "ERROR: Can't find node SvtxHitMap" << std::endl;
164  }
165  m_Geoms =
166  findNode::getClass<PHG4CylinderGeomContainer>(topNode, "CYLINDERGEOM_MVTX");
167  if (!m_Geoms)
168  {
169  std::cout << PHWHERE << "ERROR: Can't find node CYLINDERCELLGEOM_MVTX" << std::endl;
171  }
172 
173  m_truthInfo = findNode::getClass<PHG4TruthInfoContainer>(topNode, "G4TruthInfo");
174  if (!m_truthInfo)
175  {
176  std::cout << PHWHERE << "ERROR: Can't find node G4TruthInfo" << std::endl;
178  }
179 
180  m_Flags = findNode::getClass<PdbParameterMap>(topNode, "HFMLTrigger_HepMCTriggerFlags");
181  if (!m_Flags)
182  {
183  cout << "HFMLTriggerOccupancy::InitRun - WARNING - missing HFMLTrigger_HepMCTriggerFlags" << endl;
184  }
185 
187 }
188 
190 {
191  if (!_svtxevalstack)
192  {
193  _svtxevalstack = new SvtxEvalStack(topNode);
196  }
197  else
198  {
199  _svtxevalstack->next_event(topNode);
200  }
201 
202  // SvtxVertexEval* vertexeval = _svtxevalstack->get_vertex_eval();
203  // SvtxTrackEval* trackeval = _svtxevalstack->get_track_eval();
204  // SvtxClusterEval* clustereval = _svtxevalstack->get_cluster_eval();
206  // SvtxTruthEval* trutheval = _svtxevalstack->get_truth_eval();
207 
208  // PHHepMCGenEventMap* geneventmap = findNode::getClass<PHHepMCGenEventMap>(topNode, "PHHepMCGenEventMap");
209  // if (!geneventmap)
210  // {
211  // std::cout << PHWHERE << " - Fatal error - missing node PHHepMCGenEventMap" << std::endl;
212  // return Fun4AllReturnCodes::ABORTRUN;
213  // }
214  //
215  // PHHepMCGenEvent* genevt = geneventmap->get(_embedding_id);
216  // if (!genevt)
217  // {
218  // std::cout << PHWHERE << " - Fatal error - node PHHepMCGenEventMap missing subevent with embedding ID " << _embedding_id;
219  // std::cout << ". Print PHHepMCGenEventMap:";
220  // geneventmap->identify();
221  // return Fun4AllReturnCodes::ABORTRUN;
222  // }
223  //
224  // HepMC::GenEvent* theEvent = genevt->getEvent();
225  // assert(theEvent);
226  // if (Verbosity())
227  // {
228  // cout << "HFMLTriggerOccupancy::process_event - process HepMC::GenEvent with signal_process_id = "
229  // << theEvent->signal_process_id();
230  // if (theEvent->signal_process_vertex())
231  // {
232  // cout << " and signal_process_vertex : ";
233  // theEvent->signal_process_vertex()->print();
234  // }
235  // cout << " - Event record:" << endl;
236  // theEvent->print();
237  // }
238 
239  assert(m_hNorm);
240  m_hNorm->Fill("Event", 1.);
241 
243  assert(m_hNChEta);
245 
246  const PHG4VtxPoint* vtx =
248  if (vtx)
249  {
250  m_hVertexZ -> Fill(vtx->get_z());
251  }
252 
253  const auto primary_range =
255  for (auto particle_iter =
256  primary_range.first;
257  particle_iter != primary_range.second;
258  ++particle_iter)
259  {
260  const PHG4Particle *p = particle_iter->second;
261  assert(p);
262  TParticlePDG *pdg_p = TDatabasePDG::Instance()->GetParticle(
263  p->get_pid());
264  assert(pdg_p);
265  if (fabs(pdg_p->Charge()) > 0)
266  {
267  TVector3 pvec(p->get_px(), p->get_py(), p->get_pz());
268  if (pvec.Perp2() > 0)
269  {
270  assert(m_hNChEta);
271  m_hNChEta->Fill(pvec.PseudoRapidity());
272  }
273  }
274  } // if (_load_all_particle) else
275 
276 
277  // chip counting
278  vector<vector<vector<int> > >
279  multiplicity_vec(_nlayers_maps);
280 
281  //
282  for (unsigned int layer = 0; layer < _nlayers_maps; ++layer)
283  {
284  PHG4CylinderGeom_MVTX* geom = dynamic_cast<PHG4CylinderGeom_MVTX*>(m_Geoms->GetLayerGeom(layer));
285  assert(geom);
286 
287  const int n_stave = geom->get_N_staves();
288 
289  assert(multiplicity_vec[layer].empty());
290  multiplicity_vec[layer].resize(n_stave, vector<int>(kNCHip, 0));
291  }
292 
293  set<unsigned int> mapsHits; //internal consistency check for later stages of truth tracks
294  assert(m_hitMap);
295  for (SvtxHitMap::Iter iter = m_hitMap->begin();
296  iter != m_hitMap->end();
297  ++iter)
298  {
299  SvtxHit* hit = iter->second;
300  assert(hit);
301  unsigned int layer = hit->get_layer();
302  if (layer < _nlayers_maps)
303  {
304  unsigned int hitID = hit->get_id();
305  mapsHits.insert(hitID);
306 
307  PHG4Cell* cell = hiteval->get_cell(hit);
308  assert(cell);
309  PHG4CylinderGeom_MVTX* geom = dynamic_cast<PHG4CylinderGeom_MVTX*>(m_Geoms->GetLayerGeom(layer));
310  assert(geom);
311 
312  TVector3 local_coords = geom->get_local_coords_from_pixel(cell->get_pixel_index());
313  TVector3 world_coords = geom->get_world_from_local_coords(cell->get_stave_index(), cell->get_half_stave_index(), cell->get_module_index(), cell->get_chip_index(), local_coords);
314 
315  unsigned int pixel_x(cell->get_pixel_index() % geom->get_NX());
316  unsigned int pixel_z(cell->get_pixel_index() / geom->get_NX());
317  unsigned int halflayer = (int) pixel_x >= geom->get_NX() / 2 ? 0 : 1;
318 
319  assert((int) pixel_x < geom->get_NX());
320  assert((int) pixel_z < geom->get_NZ());
321 
322  unsigned int halfLayerIndex(layer * 2 + halflayer);
323 // unsigned int pixelPhiIndex(
324 // cell->get_stave_index() * geom->get_NX() + pixel_x);
325 // unsigned int pixelPhiIndexHL(
326 // cell->get_stave_index() * geom->get_NX() / 2 + pixel_x % (geom->get_NX() / 2));
327 // unsigned int pixelZIndex(cell->get_chip_index() * geom->get_NZ() + pixel_z);
328 
329  // // ptree hitTree;
330  // rapidjson::Value hitTree(rapidjson::kObjectType);
331  //
332  // // ptree hitIDTree;
333  // rapidjson::Value hitIDTree(rapidjson::kObjectType);
334  // hitIDTree.AddMember("HitSequenceInEvent", hitID, alloc);
335  //
336  // hitIDTree.AddMember("PixelHalfLayerIndex", halfLayerIndex, alloc);
337  // hitIDTree.AddMember("PixelPhiIndexInLayer", pixelPhiIndex, alloc);
338  // hitIDTree.AddMember("PixelPhiIndexInHalfLayer", pixelPhiIndexHL, alloc);
339  // hitIDTree.AddMember("PixelZIndex", pixelZIndex, alloc);
340  //
341  // hitIDTree.AddMember("Layer", layer, alloc);
342  // hitIDTree.AddMember("HalfLayer", halflayer, alloc);
343  // hitIDTree.AddMember("Stave", cell->get_stave_index(), alloc);
344  // // hitIDTree.put("HalfStave", cell->get_half_stave_index());
345  // // hitIDTree.put("Module", cell->get_module_index());
346  // hitIDTree.AddMember("Chip", cell->get_chip_index(), alloc);
347  // hitIDTree.AddMember("Pixel", cell->get_pixel_index(), alloc);
348  // hitTree.AddMember("ID", hitIDTree, alloc);
349  //
350  // hitTree.AddMember("Coordinate",
351  // loadCoordinate(world_coords.x(),
352  // world_coords.y(),
353  // world_coords.z()),
354  // alloc);
355 
356  if (Verbosity() >= 2)
357  cout << "HFMLTriggerOccupancy::process_event - MVTX hit "
358  << "layer " << layer << "\t"
359  << "Stave " << cell->get_stave_index() << "\t"
360  << "Chip " << cell->get_chip_index() << "\t"
361  << "Pixel " << cell->get_pixel_index() << "\t"
362  << "Coordinate"
363  << "(" << world_coords.x() << "," << world_coords.y() << "," << world_coords.z() << ")" << endl;
364 
365  // rawHitsTree.add_child("MVTXHit", hitTree);
366  // rawHitsTree.PushBack(hitTree, alloc);
367 
368  m_hitStaveLayer->Fill(cell->get_stave_index(), halfLayerIndex);
370  m_hitChipModule->Fill(cell->get_chip_index(), cell->get_module_index());
371 
372  m_hitLayerMap->Fill(world_coords.x(), world_coords.y(), halfLayerIndex);
373 // m_hitPixelPhiMap->Fill(pixelPhiIndex, atan2(world_coords.y(), world_coords.x()), halfLayerIndex);
374 // m_hitPixelPhiMapHL->Fill(pixelPhiIndexHL, atan2(world_coords.y(), world_coords.x()), halfLayerIndex);
375 // m_hitPixelZMap->Fill(pixelZIndex, world_coords.z(), halfLayerIndex);
376 
377  assert(layer < multiplicity_vec.size());
378  auto& multiplicity_layer = multiplicity_vec[layer];
379  assert(static_cast<size_t>(cell->get_stave_index()) < multiplicity_layer.size());
380  auto& multiplicity_stave = multiplicity_layer[cell->get_stave_index()];
381  assert(static_cast<size_t>(cell->get_chip_index()) < multiplicity_stave.size());
382  multiplicity_stave[cell->get_chip_index()]++;
383 
384  } // if (layer < _nlayers_maps)
385 
386  } // for (SvtxHitMap::Iter iter = m_hitMap->begin();
387  // rawHitTree.AddMember("MVTXHits", rawHitsTree, alloc);
388 
389  // m_Multiplicity = new TH1F("Multiplicity", "Multiplicity", 10000, -.5, 10000 - .5);
390  // m_Multiplicity->SetTitle("Multiplicity;Chip Multiplicity;Count");
391  //
392  // m_LayerMultiplicity = new TH2F("LayerMultiplicity", "LayerMultiplicity", 3, -.5, 2.5, 1000, -.5, 1000 - .5);
393  // m_LayerMultiplicity->SetTitle("LayerMultiplicity;Layer;Chip Multiplicity");
394  //
395  // m_LayerChipMultiplicity = new TH3F("LayerChipMultiplicity", "LayerChipMultiplicity", 3, -.5, 2.5, 9, -.5, 8.5, 1000, -.5, 1000 - .5);
396  // m_LayerChipMultiplicity->SetTitle("LayerChipMultiplicity;Layer;Chip");
397 
398  int layer = 0;
399  for (auto& multiplicity_layer : multiplicity_vec)
400  {
401  int stave = 0;
402  for (auto& multiplicity_stave : multiplicity_layer)
403  {
404  int chip = 0;
405  for (auto& multiplicity_chip : multiplicity_stave)
406  {
407  m_Multiplicity->Fill(multiplicity_chip);
408  m_LayerMultiplicity->Fill(layer, multiplicity_chip);
409  m_LayerChipMultiplicity->Fill(layer, chip, multiplicity_chip);
410 
411  chip++;
412  }
413 
414  if (Verbosity() >= 2)
415  cout << "HFMLTriggerOccupancy::process_event - fill layer " << layer << " stave. " << stave << "Accumulated chips = "
416  << m_Multiplicity->GetSum()
417  << endl;
418 
419  stave++;
420  }
421  layer++;
422  }
423 
424  ++_ievent;
425 
427 }
428 
430 {
431  if (_f)
432  {
433  _f->cd();
434  _f->Write();
435  //_f->Close();
436 
437  // m_hitLayerMap->Write();
438  }
439 
440  // if (m_jsonOut.is_open())
441  // {
442  // m_jsonOut << "]" << endl;
443  // m_jsonOut << "}" << endl;
444  //
445  // m_jsonOut.close();
446  // }
447 
448  cout << "HFMLTriggerOccupancy::End - output to " << _foutname << ".*" << endl;
449 
451 }