File indexing completed on 2025-08-05 08:12:44
0001 #include "HFMLTriggerOccupancy.h"
0002
0003 #include <fun4all/Fun4AllReturnCodes.h>
0004 #include <pdbcalbase/PdbParameterMap.h>
0005 #include <phool/PHCompositeNode.h>
0006 #include <phool/PHTimeServer.h>
0007 #include <phool/PHTimer.h>
0008 #include <phool/getClass.h>
0009
0010 #include <phool/PHCompositeNode.h>
0011
0012 #include <g4main/PHG4Hit.h>
0013 #include <g4main/PHG4Particle.h>
0014 #include <g4main/PHG4VtxPoint.h>
0015 #include <g4main/PHG4TruthInfoContainer.h>
0016
0017 #include <g4detectors/PHG4Cell.h>
0018 #include <g4detectors/PHG4CellContainer.h>
0019 #include <g4detectors/PHG4CylinderCellGeom.h>
0020 #include <g4detectors/PHG4CylinderCellGeomContainer.h>
0021 #include <g4detectors/PHG4CylinderGeom.h>
0022 #include <g4detectors/PHG4CylinderGeomContainer.h>
0023
0024 #include <g4eval/SvtxEvalStack.h>
0025
0026 #include <trackbase/TrkrHitSetContainerv1.h>
0027 #include <trackbase/TrkrHitSetv1.h>
0028 #include <trackbase/TrkrHitv1.h>
0029
0030 #include <mvtx/CylinderGeom_Mvtx.h>
0031 #include <mvtx/MvtxDefs.h>
0032
0033 #include <HepMC/GenEvent.h>
0034 #include <HepMC/GenVertex.h>
0035 #include <phhepmc/PHHepMCGenEvent.h>
0036 #include <phhepmc/PHHepMCGenEventMap.h>
0037
0038 #include <TDatabasePDG.h>
0039 #include <TFile.h>
0040 #include <TH1D.h>
0041 #include <TH1F.h>
0042 #include <TH2F.h>
0043 #include <TH3F.h>
0044 #include <TLorentzVector.h>
0045 #include <TString.h>
0046 #include <TTree.h>
0047
0048 #include <rapidjson/document.h>
0049 #include <rapidjson/ostreamwrapper.h>
0050 #include <rapidjson/prettywriter.h>
0051
0052 #include <boost/format.hpp>
0053 #include <boost/property_tree/json_parser.hpp>
0054 #include <boost/property_tree/ptree.hpp>
0055 #include <boost/property_tree/xml_parser.hpp>
0056
0057 #include <cassert>
0058 #include <cmath>
0059 #include <cstddef>
0060 #include <iostream>
0061
0062 using namespace std;
0063
0064 HFMLTriggerOccupancy::HFMLTriggerOccupancy(std::string filename)
0065 : SubsysReco("HFMLTriggerOccupancy")
0066 , _ievent(0)
0067 , _f(nullptr)
0068 , _eta_min(-1)
0069 , _eta_max(1)
0070 , _embedding_id(1)
0071 , _nlayers_maps(3)
0072 , _svtxevalstack(nullptr)
0073 , m_hitsetcont(nullptr)
0074 , m_Geoms(nullptr)
0075 , m_truthInfo(nullptr)
0076 , m_Flags(nullptr)
0077 , m_hNorm(nullptr)
0078 , m_hNChEta(nullptr)
0079 , m_hVertexZ(nullptr)
0080 , m_hitStaveLayer(nullptr)
0081 , m_hitModuleHalfStave(nullptr)
0082 , m_hitChipModule(nullptr)
0083 , m_hitLayerMap(nullptr)
0084 , m_hitPixelPhiMap(nullptr)
0085 , m_hitPixelPhiMapHL(nullptr)
0086 , m_hitPixelZMap(nullptr)
0087 , m_Multiplicity(nullptr)
0088 , m_LayerMultiplicity(nullptr)
0089 , m_LayerChipMultiplicity(nullptr)
0090 {
0091 _foutname = filename;
0092 }
0093
0094 int HFMLTriggerOccupancy::Init(PHCompositeNode* topNode)
0095 {
0096 _ievent = 0;
0097
0098 _f = new TFile((_foutname + string(".root")).c_str(), "RECREATE");
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110 m_hNorm = new TH1F("hNormalization",
0111 "Normalization;Items;Summed quantity", 10, .5, 10.5);
0112 int i = 1;
0113 m_hNorm->GetXaxis()->SetBinLabel(i++, "IntegratedLumi");
0114 m_hNorm->GetXaxis()->SetBinLabel(i++, "Event");
0115 m_hNorm->GetXaxis()->LabelsOption("v");
0116
0117 m_hNChEta = new TH1F("hNChEta",
0118 "Charged particle #eta distribution;#eta;Count",
0119 1000, -5, 5);
0120 m_hVertexZ = new TH1F("hVertexZ",
0121 "Vertex z distribution;z [cm];Count",
0122 1000, -200, 200);
0123
0124 m_hitLayerMap = new TH3F("hitLayerMap", "hitLayerMap", 600, -10, 10, 600, -10, 10, 10, -.5, 9.5);
0125 m_hitLayerMap->SetTitle("hitLayerMap;x [mm];y [mm];Half Layers");
0126
0127
0128
0129
0130
0131
0132
0133
0134 m_hitStaveLayer = new TH2F("hitStaveLayer", "hitStaveLayer", 100, -.5, 100 - .5, 10, -.5, 9.5);
0135 m_hitStaveLayer->SetTitle("hitStaveLayer;Stave index;Half Layers");
0136 m_hitModuleHalfStave = new TH2F("hitModuleHalfStave", "hitModuleHalfStave", 100, -.5, 100 - .5, 10, -.5, 9.5);
0137 m_hitModuleHalfStave->SetTitle("hitModuleHalfStave;Module index;Half Stave");
0138 m_hitChipModule = new TH2F("hitChipModule", "hitChipModule", 100, -.5, 100 - .5, 10, -.5, 9.5);
0139 m_hitChipModule->SetTitle("hitChipModule;Chip;Module");
0140
0141 m_Multiplicity = new TH1F("Multiplicity", "Multiplicity", 10000, -.5, 10000 - .5);
0142 m_Multiplicity->SetTitle("Multiplicity;Chip Multiplicity;Count");
0143
0144 m_LayerMultiplicity = new TH2F("LayerMultiplicity", "LayerMultiplicity", 3, -.5, 2.5, 1000, -.5, 1000 - .5);
0145 m_LayerMultiplicity->SetTitle("LayerMultiplicity;Layer;Chip Multiplicity");
0146
0147 m_LayerChipMultiplicity = new TH3F("LayerChipMultiplicity", "LayerChipMultiplicity", 3, -.5, 2.5, kNCHip, -.5, kNCHip - .5, 1000, -.5, 1000 - .5);
0148 m_LayerChipMultiplicity->SetTitle("LayerChipMultiplicity;Layer;Chip");
0149
0150 return Fun4AllReturnCodes::EVENT_OK;
0151 }
0152
0153 int HFMLTriggerOccupancy::InitRun(PHCompositeNode* topNode)
0154 {
0155 m_hitsetcont = findNode::getClass<TrkrHitSetContainerv1>(topNode, "TRKR_HITSET");
0156 if (!m_hitsetcont)
0157 {
0158 std::cout << PHWHERE << "ERROR: Can't find node TrkrHitSetContainer" << std::endl;
0159 return Fun4AllReturnCodes::ABORTRUN;
0160 }
0161 m_Geoms =
0162 findNode::getClass<PHG4CylinderGeomContainer>(topNode, "CYLINDERGEOM_MVTX");
0163 if (!m_Geoms)
0164 {
0165 std::cout << PHWHERE << "ERROR: Can't find node CYLINDERCELLGEOM_MVTX" << std::endl;
0166 return Fun4AllReturnCodes::ABORTRUN;
0167 }
0168
0169 m_truthInfo = findNode::getClass<PHG4TruthInfoContainer>(topNode, "G4TruthInfo");
0170 if (!m_truthInfo)
0171 {
0172 std::cout << PHWHERE << "ERROR: Can't find node G4TruthInfo" << std::endl;
0173 return Fun4AllReturnCodes::ABORTRUN;
0174 }
0175
0176 m_Flags = findNode::getClass<PdbParameterMap>(topNode, "HFMLTrigger_HepMCTriggerFlags");
0177 if (!m_Flags)
0178 {
0179 cout << "HFMLTriggerOccupancy::InitRun - WARNING - missing HFMLTrigger_HepMCTriggerFlags" << endl;
0180 }
0181
0182 return Fun4AllReturnCodes::EVENT_OK;
0183 }
0184
0185 int HFMLTriggerOccupancy::process_event(PHCompositeNode* topNode)
0186 {
0187 if (!_svtxevalstack)
0188 {
0189 _svtxevalstack = new SvtxEvalStack(topNode);
0190 _svtxevalstack->set_strict(0);
0191 _svtxevalstack->set_verbosity(Verbosity() + 1);
0192 }
0193 else
0194 {
0195 _svtxevalstack->next_event(topNode);
0196 }
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235 assert(m_hNorm);
0236 m_hNorm->Fill("Event", 1.);
0237
0238 assert(m_truthInfo);
0239 assert(m_hNChEta);
0240 assert(m_hVertexZ);
0241
0242 const PHG4VtxPoint* vtx =
0243 m_truthInfo->GetPrimaryVtx(m_truthInfo->GetPrimaryVertexIndex());
0244 if (vtx)
0245 {
0246 m_hVertexZ -> Fill(vtx->get_z());
0247 }
0248
0249 const auto primary_range =
0250 m_truthInfo->GetPrimaryParticleRange();
0251 for (auto particle_iter =
0252 primary_range.first;
0253 particle_iter != primary_range.second;
0254 ++particle_iter)
0255 {
0256 const PHG4Particle *p = particle_iter->second;
0257 assert(p);
0258 TParticlePDG *pdg_p = TDatabasePDG::Instance()->GetParticle(
0259 p->get_pid());
0260 assert(pdg_p);
0261 if (fabs(pdg_p->Charge()) > 0)
0262 {
0263 TVector3 pvec(p->get_px(), p->get_py(), p->get_pz());
0264 if (pvec.Perp2() > 0)
0265 {
0266 assert(m_hNChEta);
0267 m_hNChEta->Fill(pvec.PseudoRapidity());
0268 }
0269 }
0270 }
0271
0272
0273
0274 vector<vector<vector<int> > >
0275 multiplicity_vec(_nlayers_maps);
0276
0277
0278 for (unsigned int layer = 0; layer < _nlayers_maps; ++layer)
0279 {
0280 CylinderGeom_Mvtx* geom = dynamic_cast<CylinderGeom_Mvtx*>(m_Geoms->GetLayerGeom(layer));
0281 assert(geom);
0282
0283 const int n_stave = geom->get_N_staves();
0284
0285 assert(multiplicity_vec[layer].empty());
0286 multiplicity_vec[layer].resize(n_stave, vector<int>(kNCHip, 0));
0287 }
0288
0289
0290 assert(m_hitsetcont);
0291
0292 TrkrHitSetContainer::ConstRange hitset_range = m_hitsetcont->getHitSets(TrkrDefs::TrkrId::mvtxId);
0293 for (TrkrHitSetContainer::ConstIterator hitset_iter = hitset_range.first;
0294 hitset_iter != hitset_range.second;
0295 ++hitset_iter)
0296 {
0297 TrkrDefs::hitsetkey hitSetKey = hitset_iter->first;
0298 TrkrHitSet::ConstRange hit_range = hitset_iter->second->getHits();
0299 for (TrkrHitSet::ConstIterator hit_iter = hit_range.first;
0300 hit_iter != hit_range.second;
0301 ++hit_iter)
0302 {
0303 TrkrDefs::hitkey hitKey = hit_iter->first;
0304 TrkrHit* hit = hit_iter->second;
0305 assert(hit);
0306 unsigned int layer = TrkrDefs::getLayer(hitset_iter->first);
0307 if (layer < _nlayers_maps)
0308 {
0309
0310
0311
0312
0313
0314
0315 CylinderGeom_Mvtx* geom = dynamic_cast<CylinderGeom_Mvtx*>(m_Geoms->GetLayerGeom(layer));
0316 assert(geom);
0317 unsigned int pixel_x = MvtxDefs::getRow(hitKey);
0318 unsigned int pixel_z = MvtxDefs::getCol(hitKey);
0319 assert((int)pixel_x < geom->get_NX());
0320 assert((int)pixel_z < geom->get_NZ());
0321 unsigned int chip = MvtxDefs::getChipId(hitSetKey);
0322 unsigned int stave = MvtxDefs::getStaveId(hitSetKey);
0323 TVector3 local_coords = geom->get_local_coords_from_pixel(pixel_x, pixel_z);
0324 TVector3 world_coords = geom->get_world_from_local_coords(stave,
0325 0,
0326 0,
0327 chip,
0328 local_coords);
0329
0330
0331
0332
0333
0334
0335
0336
0337
0338
0339
0340
0341
0342
0343
0344
0345
0346
0347
0348
0349
0350
0351
0352
0353
0354
0355
0356
0357
0358
0359
0360
0361
0362
0363
0364
0365
0366
0367
0368 if (Verbosity() >= 2)
0369 cout << "HFMLTriggerOccupancy::process_event - MVTX hit "
0370 << "layer " << layer << "\t"
0371 << "Stave " << stave << "\t"
0372 << "Chip " << chip << "\t"
0373 << "Pixel x " << pixel_x << "\t"
0374 << "Pixel z " << pixel_z << "\t"
0375 << "Coordinate"
0376 << "(" << world_coords.x() << "," << world_coords.y() << "," << world_coords.z() << ")" << endl;
0377
0378
0379
0380
0381 m_hitStaveLayer->Fill(stave, layer);
0382 m_hitModuleHalfStave->Fill(stave, stave);
0383 m_hitChipModule->Fill(chip, stave);
0384
0385 m_hitLayerMap->Fill(world_coords.x(), world_coords.y(), layer);
0386
0387
0388
0389
0390 assert(layer < multiplicity_vec.size());
0391 auto& multiplicity_layer = multiplicity_vec[layer];
0392 assert(static_cast<size_t>(stave) < multiplicity_layer.size());
0393 auto& multiplicity_stave = multiplicity_layer[stave];
0394 assert(static_cast<size_t>(chip) < multiplicity_stave.size());
0395 multiplicity_stave[chip]++;
0396
0397 }
0398
0399 }
0400
0401 }
0402
0403
0404
0405
0406
0407
0408
0409
0410
0411
0412
0413 int layer = 0;
0414 for (auto& multiplicity_layer : multiplicity_vec)
0415 {
0416 int stave = 0;
0417 for (auto& multiplicity_stave : multiplicity_layer)
0418 {
0419 int chip = 0;
0420 for (auto& multiplicity_chip : multiplicity_stave)
0421 {
0422 m_Multiplicity->Fill(multiplicity_chip);
0423 m_LayerMultiplicity->Fill(layer, multiplicity_chip);
0424 m_LayerChipMultiplicity->Fill(layer, chip, multiplicity_chip);
0425
0426 chip++;
0427 }
0428
0429 if (Verbosity() >= 2)
0430 cout << "HFMLTriggerOccupancy::process_event - fill layer " << layer << " stave. " << stave << "Accumulated chips = "
0431 << m_Multiplicity->GetSum()
0432 << endl;
0433
0434 stave++;
0435 }
0436 layer++;
0437 }
0438
0439 ++_ievent;
0440
0441 return Fun4AllReturnCodes::EVENT_OK;
0442 }
0443
0444 int HFMLTriggerOccupancy::End(PHCompositeNode* topNode)
0445 {
0446 if (_f)
0447 {
0448 _f->cd();
0449 _f->Write();
0450
0451
0452
0453 }
0454
0455
0456
0457
0458
0459
0460
0461
0462
0463 cout << "HFMLTriggerOccupancy::End - output to " << _foutname << ".*" << endl;
0464
0465 return Fun4AllReturnCodes::EVENT_OK;
0466 }