File indexing completed on 2026-12-02 09:11:07
0001 #include <random>
0002 #include <vector>
0003 #include <string>
0004 #include <numeric>
0005 #include <cmath>
0006 #include "podio/ROOTReader.h"
0007 #include "podio/Frame.h"
0008 #include "edm4eic/CherenkovParticleIDCollection.h"
0009 #include "edm4hep/MCParticleCollection.h"
0010 #include <chrono>
0011
0012
0013
0014 struct EventData {
0015 std::vector<double> theta;
0016 std::vector<double> thetaErr;
0017 double nPhotons;
0018 bool detected;
0019 };
0020
0021
0022 std::vector<EventData> readEventData(const std::vector<std::string>& infiles, int radiator) {
0023 std::vector<EventData> events;
0024 podio::ROOTReader reader;
0025 reader.openFiles(infiles);
0026 int nev = reader.getEntries("events");
0027 events.reserve(nev);
0028
0029
0030 std::string pidCol;
0031 double n_refr;
0032 double thmin, thmax;
0033 if (radiator == 0) {
0034 pidCol = "DRICHAerogelIrtCherenkovParticleID";
0035 n_refr = 1.019;
0036 thmax = 220;
0037 thmin = 150;
0038 } else if (radiator == 1) {
0039 pidCol = "DRICHGasIrtCherenkovParticleID";
0040 n_refr = 1.00076;
0041 thmax = 50;
0042 thmin = 20;
0043 }
0044 else throw std::runtime_error("bad radiator");
0045
0046 for (int i = 0; i < nev; ++i) {
0047 auto frame = podio::Frame(reader.readNextEntry("events"));
0048 auto& chcol = frame.get<edm4eic::CherenkovParticleIDCollection>(pidCol);
0049 auto& mc = frame.get<edm4hep::MCParticleCollection>("MCParticles");
0050
0051
0052 auto p4 = mc[0].getMomentum();
0053 double p = std::hypot(p4.x,p4.y,p4.z);
0054 double betaTrue = p/std::sqrt(p*p + mc[0].getMass()*mc[0].getMass());
0055 double chExpected = std::acos(1./(n_refr*betaTrue))*1000;
0056
0057
0058
0059 for (const auto& pid : chcol) {
0060 EventData ed;
0061 ed.nPhotons = 0;
0062
0063 for (auto& tp : pid.getThetaPhiPhotons()) {
0064 double th = tp[0]*1000;
0065 if (th < thmax && th > thmin) {
0066 ed.theta.push_back(th);
0067 ed.thetaErr.push_back(th - chExpected);
0068 ed.nPhotons += 1.;
0069 }
0070 }
0071 ed.detected = (ed.nPhotons > 5);
0072 events.push_back(std::move(ed));
0073 }
0074 }
0075
0076 return events;
0077 }
0078
0079 struct BootstrapResults {
0080 double mean_nPhot;
0081 double mean_theta;
0082 double mean_theta_mae;
0083 double frac_detected;
0084 };
0085
0086
0087
0088 BootstrapResults computeBootstrap(
0089 const std::vector<EventData>& events,
0090 int sampleSize,
0091 std::mt19937_64& rng
0092 ) {
0093 std::uniform_int_distribution<> pick(0, events.size() - 1);
0094
0095 double sum_nphot = 0.0;
0096 double sum_theta = 0.0;
0097 double sum_theta_err = 0.0;
0098 double total_photons = 0.0;
0099 int n_detected = 0;
0100
0101
0102 for (int i = 0; i < sampleSize; ++i) {
0103 const auto& ev = events[pick(rng)];
0104 sum_nphot += ev.nPhotons;
0105 if (ev.detected) ++n_detected;
0106
0107 for (double th : ev.theta) {
0108 sum_theta += th;
0109 total_photons += 1.;
0110 }
0111 for (double err : ev.thetaErr) {
0112 sum_theta_err += std::fabs(err);
0113 }
0114 }
0115
0116
0117
0118 BootstrapResults out;
0119 out.mean_nPhot = sum_nphot / sampleSize;
0120 out.frac_detected = double(n_detected) / sampleSize;
0121 if (total_photons > 0) {
0122 out.mean_theta = sum_theta / total_photons;
0123 out.mean_theta_mae = sum_theta_err / total_photons;
0124 } else {
0125 out.mean_theta = 0.0;
0126 out.mean_theta_mae = 0.0;
0127 }
0128 return out;
0129 }
0130
0131 std::tuple<double,double,double,double> bootstrapStats(const std::vector<EventData>& events_pi,
0132 const std::vector<EventData>& events_K,
0133 int sampleSize,
0134 int nBootstrap
0135 ){
0136
0137
0138 std::vector<double> final_piKsep(nBootstrap), final_acc(nBootstrap);
0139 std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
0140
0141 for (int b = 0; b < nBootstrap; b++) {
0142
0143 auto stats_pi = computeBootstrap(events_pi, sampleSize, rng);
0144 auto stats_K = computeBootstrap(events_K, sampleSize, rng);
0145
0146 double cher_diff = std::fabs(stats_pi.mean_theta - stats_K.mean_theta);
0147
0148 double avg_mae = (stats_pi.mean_theta_mae + stats_K.mean_theta_mae)/2.;
0149 double avg_acc = (stats_pi.frac_detected + stats_K.frac_detected)/2.;
0150 double avg_nphot = (stats_pi.mean_nPhot + stats_K.mean_nPhot)/2.;
0151
0152
0153 final_piKsep[b] = cher_diff*sqrt(avg_nphot)/avg_mae;
0154 final_acc[b] = avg_acc;
0155 }
0156
0157 auto compute_stats = [&](const std::vector<double>& v) {
0158 double N = v.size();
0159 double sum = std::accumulate(v.begin(), v.end(), 0.0);
0160 double mean = sum/N;
0161 double sq = 0;
0162 for (double x : v) sq += (x-mean)*(x-mean);
0163 double stdev = std::sqrt(sq/(N-1));
0164
0165 return std::tuple{mean, stdev};
0166 };
0167
0168
0169 auto [mean_acc, sd_acc] = compute_stats(final_acc);
0170 auto [mean_piKsep, sd_piKsep] = compute_stats(final_piKsep);
0171
0172 return std::tuple{mean_acc, sd_acc, mean_piKsep, sd_piKsep};
0173 }
0174
0175
0176 void dRICHAna_bootstrap(const std::vector<std::string>& infiles_pi,
0177 const std::vector<std::string>& infiles_K,
0178 const char* outname,
0179 int radiator,
0180 int sampleSize = 1000,
0181 int nBootstrap = 100
0182 )
0183 {
0184
0185 auto events_pi = readEventData(infiles_pi, radiator);
0186 auto events_K = readEventData(infiles_K, radiator);
0187
0188 std::cout << "N ev pi: " << events_pi.size() << " K: " << events_K.size() << std::endl;
0189 if (sampleSize > static_cast<int>(events_pi.size())) {
0190 sampleSize = static_cast<int>(events_pi.size());
0191 }
0192 if (sampleSize > static_cast<int>(events_K.size())) {
0193 sampleSize = static_cast<int>(events_K.size());
0194 }
0195
0196 auto [mean_acc, sd_acc, mean_piKsep, sd_piKsep] = bootstrapStats(events_pi,
0197 events_K,
0198 sampleSize,
0199 nBootstrap
0200 );
0201 if (sampleSize < static_cast<int>(events_pi.size())) {
0202 double ratio = double(sampleSize)/events_pi.size();
0203 sd_piKsep *= ratio;
0204 sd_acc *= ratio;
0205 }
0206
0207 FILE *outfile = fopen(outname,"w");
0208 fprintf(outfile, "%lf %lf %lf %lf \n", mean_acc, sd_acc, mean_piKsep, sd_piKsep);
0209 return;
0210 }
0211
0212 int main(int argc, char* argv[]) {
0213 if (argc < 7 || (argc - 5) % 2 != 0) {
0214 std::cout << "usage: dRICHAna_bootstrap pi.root K.root [pi2.root K2.root ...] output.txt radiator samples bootstraps\n";
0215 return 1;
0216 }
0217
0218 std::vector<std::string> infiles_pi, infiles_K;
0219 for (int i = 1; i < argc - 4; i += 2) {
0220 infiles_pi.emplace_back(argv[i]);
0221 infiles_K.emplace_back(argv[i + 1]);
0222 }
0223 int rad = std::stoi(argv[argc - 3]);
0224 int nsamples = std::stoi(argv[argc - 2]);
0225 int nbootstraps = std::stoi(argv[argc - 1]);
0226
0227 dRICHAna_bootstrap(infiles_pi, infiles_K, argv[argc - 4], rad, nsamples, nbootstraps);
0228 return 0;
0229 }