|
| 1 | +#include <cmath> |
| 2 | +#include <fstream> |
| 3 | +#include <iostream> |
| 4 | +#include <string> |
| 5 | +#include <vector> |
| 6 | + |
| 7 | +#include "ROOT/RDataFrame.hxx" |
| 8 | +#include <TH1D.h> |
| 9 | +#include <TH2D.h> |
| 10 | +#include <TRandom3.h> |
| 11 | +#include <TFile.h> |
| 12 | +#include <TChain.h> |
| 13 | +#include <TTree.h> |
| 14 | +#include <TMath.h> |
| 15 | +#include <TVector3.h> |
| 16 | +#include <TCanvas.h> |
| 17 | + |
| 18 | +#include "TROOT.h" |
| 19 | +#include "TRandom.h" |
| 20 | +#include "TH3.h" |
| 21 | + |
| 22 | + |
| 23 | +#include "DD4hep/Detector.h" |
| 24 | +#include "DDRec/CellIDPositionConverter.h" |
| 25 | + |
| 26 | +#include <podio/Frame.h> |
| 27 | +#include <podio/CollectionBase.h> |
| 28 | +#include "podio/ROOTReader.h" |
| 29 | +//#include <podio/ROOTFrameReader.h> |
| 30 | +#include "podio/CollectionIDTable.h" |
| 31 | +#include "podio/ObjectID.h" |
| 32 | + |
| 33 | +#include "edm4hep/MCParticleCollection.h" |
| 34 | +#include "edm4hep/MCParticleCollectionData.h" |
| 35 | +#include "edm4hep/MCParticle.h" |
| 36 | +#include "edm4hep/MCParticleData.h" |
| 37 | + |
| 38 | +#include "edm4hep/SimCalorimeterHitCollectionData.h" |
| 39 | +#include "edm4hep/SimCalorimeterHitCollection.h" |
| 40 | +#include "edm4hep/SimCalorimeterHitData.h" |
| 41 | +#include "edm4hep/SimCalorimeterHit.h" |
| 42 | + |
| 43 | +#include "edm4hep/CalorimeterHit.h" |
| 44 | +#include "edm4hep/CalorimeterHitCollectionData.h" |
| 45 | +#include "edm4hep/CalorimeterHitCollection.h" |
| 46 | +#include "edm4hep/CalorimeterHitData.h" |
| 47 | +#include "edm4hep/CalorimeterHit.h" |
| 48 | +#include "edm4hep/CalorimeterHitObj.h" |
| 49 | + |
| 50 | +#include "edm4eic/ClusterCollection.h" |
| 51 | +#include "edm4eic/Cluster.h" |
| 52 | +#include "edm4eic/ClusterData.h" |
| 53 | + |
| 54 | +#include "edm4eic/CalorimeterHit.h" |
| 55 | +#include "edm4eic/CalorimeterHitCollectionData.h" |
| 56 | +#include "edm4eic/CalorimeterHitCollection.h" |
| 57 | +#include "edm4eic/CalorimeterHitData.h" |
| 58 | +#include "edm4eic/CalorimeterHit.h" |
| 59 | +#include "edm4eic/CalorimeterHitObj.h" |
| 60 | + |
| 61 | +#include <edm4eic/vector_utils_legacy.h> |
| 62 | +#include <edm4hep/Vector3f.h> |
| 63 | + |
| 64 | +using namespace std; |
| 65 | +using namespace ROOT; |
| 66 | +using namespace TMath; |
| 67 | +using namespace edm4hep; |
| 68 | + |
| 69 | +dd4hep::Detector* det = NULL; |
| 70 | + |
| 71 | +int sampling_fraction_analysis(const string &filename, string outname_pdf, string outname_png, TString compact_file) |
| 72 | +{ |
| 73 | + |
| 74 | + podio::ROOTReader *reader = new podio::ROOTReader(); |
| 75 | + reader->openFile(filename); |
| 76 | + unsigned nEvents = reader->getEntries("events"); |
| 77 | + cout << "Number of events: " << nEvents << endl; |
| 78 | + |
| 79 | + det = &(dd4hep::Detector::getInstance()); |
| 80 | + det->fromCompact(compact_file.Data()); |
| 81 | + det->volumeManager(); |
| 82 | + det->apply("DD4hepVolumeManager", 0, 0); |
| 83 | + |
| 84 | + dd4hep::rec::CellIDPositionConverter cellid_converter(*det); |
| 85 | + auto idSpec = det->readout("HcalEndcapNHits").idSpec(); |
| 86 | + auto decoder = idSpec.decoder(); |
| 87 | + const int slice_index = decoder->index("slice"); |
| 88 | + if (slice_index < 0) { |
| 89 | + cerr << "ERROR: 'slice' field not found in cell ID spec!" << endl; |
| 90 | + return 1; |
| 91 | + } |
| 92 | + |
| 93 | + TH2D *h_sampF_e = new TH2D("h_sampF_e", "nHCal sampling fraction vs. energy (e-); E [GeV]; sampling fraction; counts", |
| 94 | + 50, 0.0, 12.0, 50, 0.0, 1.0); |
| 95 | + TProfile *p_sampF_e = new TProfile("p_sampF_e", "nHCal sampling fraction vs. energy (e-); E [GeV]; sampling fraction", |
| 96 | + 50, 0.0, 12.0, 0.0, 1.0); |
| 97 | + |
| 98 | + TH2D *h_sampF_n = new TH2D("h_sampF_n", "nHCal sampling fraction vs. energy (neutron); E [GeV]; sampling fraction; counts", |
| 99 | + 50, 0.0, 12.0, 50, 0.0, 1.0); |
| 100 | + TProfile *p_sampF_n = new TProfile("p_sampF_n", "nHCal sampling fraction vs. energy (neutron); E [GeV]; sampling fraction", |
| 101 | + 50, 0.0, 12.0, 0.0, 1.0); |
| 102 | + |
| 103 | + TH2D *h_sampF_pi = new TH2D("h_sampF_pi", "nHCal sampling fraction vs. energy (#pi-); E [GeV]; sampling fraction; counts", |
| 104 | + 50, 0.0, 12.0, 50, 0.0, 1.0); |
| 105 | + TProfile *p_sampF_pi = new TProfile("p_sampF_pi", "nHCal sampling fraction vs. energy (#pi-); E [GeV]; sampling fraction", |
| 106 | + 50, 0.0, 12.0, 0.0, 1.0); |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + for (unsigned ev = 0; ev < nEvents; ev++) |
| 111 | + { |
| 112 | + double hit_Esum = 0; |
| 113 | + double hit_scint_Esum = 0; |
| 114 | + double singlePart_Ekin = 0; |
| 115 | + |
| 116 | + auto frameData = reader->readNextEntry(podio::Category::Event); |
| 117 | + if (!frameData) |
| 118 | + { |
| 119 | + cerr << "Invalid FrameData at event " << ev << endl; |
| 120 | + continue; |
| 121 | + } |
| 122 | + |
| 123 | + podio::Frame frame(std::move(frameData)); |
| 124 | + |
| 125 | + auto& MCParticles_coll = frame.get<edm4hep::MCParticleCollection>("MCParticles"); |
| 126 | + auto& SimCalorimeterHit_coll = frame.get<edm4hep::SimCalorimeterHitCollection>("HcalEndcapNHits"); |
| 127 | + |
| 128 | + if (!SimCalorimeterHit_coll.isValid()) |
| 129 | + { |
| 130 | + cerr << "HcalEndcapNHits collection is not valid!" << endl; |
| 131 | + } |
| 132 | + if (!MCParticles_coll.isValid()) |
| 133 | + { |
| 134 | + cerr << "MCParticles collection is not valid!" << endl; |
| 135 | + } |
| 136 | + |
| 137 | + edm4hep::MCParticle mcpart = MCParticles_coll.at(0); |
| 138 | + singlePart_Ekin = mcpart.getEnergy(); //-mcpart.getMass(); |
| 139 | + int pdg = mcpart.getPDG(); |
| 140 | + |
| 141 | + for (const auto& hit : SimCalorimeterHit_coll) |
| 142 | + { |
| 143 | + const int hit_slice = decoder->get(hit.getCellID(), slice_index); |
| 144 | + hit_Esum += hit.getEnergy(); |
| 145 | + if(hit_slice == 3) hit_scint_Esum += hit.getEnergy(); |
| 146 | + } |
| 147 | + |
| 148 | + if (pdg == 11) // e- |
| 149 | + { |
| 150 | + h_sampF_e->Fill(singlePart_Ekin, hit_scint_Esum/hit_Esum); |
| 151 | + p_sampF_e->Fill(singlePart_Ekin, hit_scint_Esum/hit_Esum); |
| 152 | + } |
| 153 | + else if (pdg == -211) // pi- |
| 154 | + { |
| 155 | + h_sampF_pi->Fill(singlePart_Ekin, hit_scint_Esum/hit_Esum); |
| 156 | + p_sampF_pi->Fill(singlePart_Ekin, hit_scint_Esum/hit_Esum); |
| 157 | + } |
| 158 | + else if (pdg == 2112) // neutron |
| 159 | + { |
| 160 | + h_sampF_n->Fill(singlePart_Ekin, hit_scint_Esum/hit_Esum); |
| 161 | + p_sampF_n->Fill(singlePart_Ekin, hit_scint_Esum/hit_Esum); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + delete reader; |
| 166 | + |
| 167 | + TProfile *p_e_over_pi = (TProfile*) p_sampF_e->Clone("p_e_over_pi"); |
| 168 | + p_e_over_pi->SetTitle("e/h ratio;E [GeV];e/h"); |
| 169 | + p_e_over_pi->Divide(p_sampF_pi); |
| 170 | + |
| 171 | + TCanvas *canvas = new TCanvas("canvas", "canvas", 1600, 800); |
| 172 | + canvas->Divide(2,2); |
| 173 | + canvas->cd(1); |
| 174 | + h_sampF_e->Draw("COLZ"); |
| 175 | + p_sampF_e->Draw("SAME"); |
| 176 | + canvas->cd(2); |
| 177 | + h_sampF_pi->Draw("COLZ"); |
| 178 | + p_sampF_pi->Draw("SAME"); |
| 179 | + canvas->cd(3); |
| 180 | + h_sampF_n->Draw("COLZ"); |
| 181 | + p_sampF_n->Draw("SAME"); |
| 182 | + canvas->cd(4); |
| 183 | + p_e_over_pi->Draw("HIST"); |
| 184 | + |
| 185 | + canvas->SaveAs(outname_pdf.c_str()); |
| 186 | + canvas->SaveAs(outname_png.c_str()); |
| 187 | + |
| 188 | + return 0; |
| 189 | +} |
0 commit comments