Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-07-26 08:22:18

0001 #include <fstream>
0002 #include <iostream>
0003 
0004 #include "TEfficiency.h"
0005 #include "TFile.h"
0006 #include "TH1.h"
0007 #include "TH1D.h"
0008 #include "TProfile.h"
0009 
0010 void process_teff(TEfficiency *eff, std::string tree, std::string out_dir) {
0011     const TH1 *hist = eff->GetTotalHistogram();
0012 
0013     int num_bins = hist->GetNbinsX();
0014 
0015     std::ofstream myfile;
0016     myfile.open(out_dir + "/" + tree + ".csv");
0017 
0018     myfile << "bin_left,bin_right,ntotal,efficiency,err_low,err_high"
0019            << std::endl;
0020 
0021     for (int i = 1; i <= num_bins; ++i) {
0022         myfile << hist->GetBinLowEdge(i) << ","
0023                << (hist->GetBinLowEdge(i) + hist->GetBinWidth(i)) << ","
0024                << hist->GetBinContent(i) << "," << eff->GetEfficiency(i) << ","
0025                << eff->GetEfficiencyErrorLow(i) << ","
0026                << eff->GetEfficiencyErrorUp(i) << std::endl;
0027     }
0028 
0029     myfile.close();
0030 }
0031 
0032 void process_profile(TProfile *hist, std::string tree, std::string out_dir) {
0033     int num_bins = hist->GetNbinsX();
0034 
0035     std::ofstream myfile;
0036     myfile.open(out_dir + "/" + tree + ".csv");
0037 
0038     myfile << "bin_left,bin_right,value,err_low,err_high" << std::endl;
0039 
0040     for (int i = 1; i <= num_bins; ++i) {
0041         myfile << hist->GetBinLowEdge(i) << ","
0042                << (hist->GetBinLowEdge(i) + hist->GetBinWidth(i)) << ","
0043                << hist->GetBinContent(i) << "," << hist->GetBinErrorLow(i)
0044                << "," << hist->GetBinErrorUp(i) << std::endl;
0045     }
0046 
0047     myfile.close();
0048 }
0049 
0050 void process_th1(TH1 *hist, std::string tree, std::string out_dir) {
0051     int num_bins = hist->GetNbinsX();
0052 
0053     std::ofstream myfile;
0054     myfile.open(out_dir + "/" + tree + ".csv");
0055 
0056     myfile << "bin_left,bin_right,ntotal,err_low,err_high" << std::endl;
0057 
0058     for (int i = 1; i <= num_bins; ++i) {
0059         myfile << hist->GetBinLowEdge(i) << ","
0060                << (hist->GetBinLowEdge(i) + hist->GetBinWidth(i)) << ","
0061                << hist->GetBinContent(i) << "," << hist->GetBinErrorLow(i)
0062                << "," << hist->GetBinErrorUp(i) << std::endl;
0063     }
0064 
0065     myfile.close();
0066 }
0067 
0068 void read_and_dump(TFile *file, std::string tree, std::string out_dir) {
0069     TEfficiency *eff = dynamic_cast<TEfficiency *>(file->Get(tree.c_str()));
0070 
0071     if (eff != nullptr) {
0072         std::cout << tree << " is a TEfficiency" << std::endl;
0073         process_teff(eff, tree, out_dir);
0074         return;
0075     }
0076 
0077     TProfile *prof = dynamic_cast<TProfile *>(file->Get(tree.c_str()));
0078 
0079     if (prof != nullptr) {
0080         std::cout << tree << " is a TProfile" << std::endl;
0081         process_profile(prof, tree, out_dir);
0082         return;
0083     }
0084 
0085     TH1 *th1 = dynamic_cast<TH1 *>(file->Get(tree.c_str()));
0086 
0087     if (th1 != nullptr) {
0088         std::cout << tree << " is a TH1" << std::endl;
0089         process_th1(th1, tree, out_dir);
0090         return;
0091     }
0092 }
0093 
0094 int main(int argc, char **argv) {
0095     if (argc < 3) {
0096         throw std::runtime_error("Too few args.");
0097     }
0098 
0099     std::unique_ptr<TFile> file(TFile::Open(argv[1]));
0100 
0101     std::vector<std::string> trees{"seeding_trackeff_vs_pT",
0102                                    "seeding_trackeff_vs_eta",
0103                                    "seeding_trackeff_vs_phi",
0104                                    "finding_trackeff_vs_pT",
0105                                    "finding_trackeff_vs_eta",
0106                                    "finding_trackeff_vs_phi",
0107                                    "pval",
0108                                    "ndf",
0109                                    "completeness",
0110                                    "purity",
0111                                    "finding_nDuplicated_vs_eta",
0112                                    "finding_nFakeTracks_vs_eta",
0113                                    "res_d0",
0114                                    "res_z0",
0115                                    "res_phi",
0116                                    "res_qop",
0117                                    "res_qopT",
0118                                    "res_qopz",
0119                                    "res_theta",
0120                                    "pull_d0",
0121                                    "pull_z0",
0122                                    "pull_phi",
0123                                    "pull_qop",
0124                                    "pull_qopT",
0125                                    "pull_qopz",
0126                                    "pull_theta"};
0127 
0128     for (const auto &tree : trees) {
0129         read_and_dump(file.get(), tree, argv[2]);
0130     }
0131 
0132     return 0;
0133 }