File indexing completed on 2025-08-06 08:13:24
0001 #pragma once
0002 #if ROOT_VERSION_CODE >= ROOT_VERSION(6,00,0)
0003 #include <string>
0004 #include <sstream>
0005 #include <vector>
0006 #include <utility>
0007 #include <map>
0008 #include <algorithm>
0009 #include <TFile.h>
0010 #include <TTree.h>
0011 #include <TChain.h>
0012 #include <TString.h>
0013 #include <fstream>
0014 std::map<std::string, std::string> getListofObjects(std::string filename)
0015 {
0016 TFile f1 (filename.c_str());
0017 std::map<std::string, std::string> keys_and_type;
0018 for(TObject* keys: *f1.GetListOfKeys()){
0019 auto key = dynamic_cast<TKey*>(keys);
0020 keys_and_type[std::string(key->GetName())] = std::string(key->GetClassName());
0021 }
0022 return keys_and_type;
0023 }
0024
0025 int MergeRootOutput(std::string file_naming_style, std::string run_string="21", std::string n_files_string="0")
0026 {
0027 int run_number, n_files;
0028 run_number=std::stoi(run_string);
0029 n_files=std::stoi(n_files_string);
0030 std::vector<std::string> filenames;
0031
0032 for(int i=0; i<n_files; i++){
0033 std::string filename = file_naming_style + run_string + "-"+std::to_string(i)+".root";
0034 filenames.push_back(filename);
0035 }
0036 std::string output_filename=file_naming_style+run_string+".root";
0037 std::map<std::string, std::string> output_names;
0038 output_names=getListofObjects(filenames.at(0));
0039
0040 std::map<std::string, TH1F*> th1_to_merge_into;
0041 std::map<std::string, TH2F*> th2_to_merge_into;
0042 TFile* f1=new TFile(filenames.at(0).c_str(), "READ");
0043 for(auto i:output_names)
0044 {
0045 int n_evts=0;
0046 std::string n_th1_name="n_";
0047 std::string stripped_name=i.first;
0048 if(stripped_name.find("Towards") != std::string::npos){
0049 stripped_name.substr(stripped_name.find("Towards"));
0050 }
0051 else if(stripped_name.find("Away") != std::string::npos){
0052 stripped_name.substr(stripped_name.find("Away"));
0053 }
0054 else if(stripped_name.find("Transverse") != std::string::npos){
0055 stripped_name.substr(stripped_name.find("Transverse"));
0056 }
0057 else if(stripped_name.find("Full") != std::string::npos){
0058 stripped_name.substr(stripped_name.find("Full"));
0059 }
0060 n_th1_name+=stripped_name;
0061 if(n_th1_name.find("CAL") != std::string::npos) {
0062 auto temp=f1->Get(n_th1_name.c_str());
0063 if(temp){
0064 TH1F* h_N=(TH1F*)temp->Clone();
0065 if(h_N) n_evts=h_N->GetEntries();
0066 }
0067 }
0068 if(n_evts == 0 ) n_evts=1;
0069 if(i.second.find("TH1F") != std::string::npos ){
0070 auto temp=f1->Get(i.first.c_str());
0071 if(temp){
0072 auto h1=(TH1F*)temp->Clone();
0073 if(i.first.find("e2c") != std::string::npos || i.first.find("e3c")!= std::string::npos){
0074 h1->Scale(n_evts);
0075 }
0076 th1_to_merge_into[i.first]=(TH1F*)h1->Clone();
0077 }
0078 }
0079 if(i.second.find("TH2F") != std::string::npos){
0080 auto temp=f1->Get(i.first.c_str());
0081 if(temp){
0082 auto h1=(TH2F*)temp->Clone();
0083 if(i.first.find("e2c") != std::string::npos || i.first.find("e3c")!= std::string::npos){
0084 h1->Scale(n_evts);
0085 }
0086 th2_to_merge_into[i.first]=(TH2F*)h1->Clone();
0087 }
0088 }
0089 }
0090 f1->Close();
0091 std::cout<<"Dealt with the first file"<<std::endl;
0092 for(auto j:filenames){
0093 if(j==filenames[0]) continue;
0094 TFile* f=new TFile(j.c_str(), "READ");
0095 for(auto i:output_names)
0096 {
0097 int n_evts=0;
0098 std::string n_th1_name="n_";
0099 std::string stripped_name=i.first;
0100 if(stripped_name.find("Towards") != std::string::npos){
0101 stripped_name.substr(stripped_name.find("Towards"));
0102 }
0103 else if(stripped_name.find("Away") != std::string::npos){
0104 stripped_name.substr(stripped_name.find("Away"));
0105 }
0106 else if(stripped_name.find("Transverse") != std::string::npos){
0107 stripped_name.substr(stripped_name.find("Transverse"));
0108 }
0109 else if(stripped_name.find("Full") != std::string::npos){
0110 stripped_name.substr(stripped_name.find("Full"));
0111 }
0112 n_th1_name+=stripped_name;
0113 if(n_th1_name.find("CAL") != std::string::npos) {
0114 auto temp=f1->Get(n_th1_name.c_str());
0115 if(temp){
0116 TH1F* h_N=(TH1F*)temp->Clone();
0117 if(h_N) n_evts=h_N->GetEntries();
0118 }
0119 }
0120 if(n_evts == 0 ) n_evts=1;
0121 if(i.second.find("TH1F") != std::string::npos){
0122 auto temp=f1->Get(i.first.c_str());
0123 if(temp){
0124 auto h1=(TH1F*)temp->Clone();
0125 if(i.first.find("e2c") != std::string::npos || i.first.find("e3c")!= std::string::npos){
0126 h1->Scale(n_evts);
0127 }
0128 th1_to_merge_into[i.first]->Add(h1);
0129 }
0130 }
0131 else if(i.second.find("TH2F") != std::string::npos){
0132 auto temp=f1->Get(i.first.c_str());
0133 if(temp){
0134 auto h1=(TH1F*)temp->Clone();
0135 if(i.first.find("e2c") != std::string::npos || i.first.find("e3c")!= std::string::npos){
0136 h1->Scale(n_evts);
0137 }
0138 th2_to_merge_into[i.first]->Add(h1);
0139 }
0140 }
0141 }
0142 f->Close();
0143 }
0144 std::cout<<"all files merged" <<std::endl;
0145
0146
0147
0148
0149 TFile* f=new TFile(output_filename.c_str(), "RECREATE");
0150 for(auto i:output_names)
0151 {
0152 int n_evts=0;
0153 std::string n_th1_name="n_";
0154 std::string stripped_name=i.first;
0155 if(stripped_name.find("Towards") != std::string::npos){
0156 stripped_name.substr(stripped_name.find("Towards"));
0157 }
0158 else if(stripped_name.find("Away") != std::string::npos){
0159 stripped_name.substr(stripped_name.find("Away"));
0160 }
0161 else if(stripped_name.find("Transverse") != std::string::npos){
0162 stripped_name.substr(stripped_name.find("Transverse"));
0163 }
0164 else if(stripped_name.find("Full") != std::string::npos){
0165 stripped_name.substr(stripped_name.find("Full"));
0166 }
0167 std::cout<<__LINE__<<std::endl;
0168 n_th1_name+=stripped_name;
0169 if(n_th1_name.find("CAL") != std::string::npos) {
0170 if(th1_to_merge_into.find(n_th1_name) == th1_to_merge_into.end()) continue;
0171 auto temp=th1_to_merge_into[n_th1_name];
0172 if(temp){
0173 TH1F* h_N=(TH1F*)temp->Clone();
0174 if(h_N) n_evts=h_N->GetEntries();
0175 }
0176 }
0177
0178 if(n_evts == 0 ) n_evts=1;
0179 if(i.second.find("TH1F")!=std::string::npos){
0180 if(th1_to_merge_into.find(i.first) == th1_to_merge_into.end()) continue;
0181 auto h1=th1_to_merge_into[i.first];
0182 if(i.first.find("e2c") != std::string::npos || i.first.find("e3c")!= std::string::npos){
0183 h1->Scale(1/(float)n_evts);
0184 }
0185 if(h1){
0186
0187 h1->Write();
0188 }
0189 }
0190 else if(i.second.find("TH2F")!=std::string::npos){
0191 if(th2_to_merge_into.find(i.first) == th2_to_merge_into.end()) continue;
0192 auto h1=th2_to_merge_into[i.first];
0193 if(i.first.find("e2c") != std::string::npos || i.first.find("e3c")!= std::string::npos){
0194 h1->Scale(1/(float)n_evts);
0195 }
0196 if(h1) h1->Write();
0197 }
0198 }
0199 std::cout<<"all normalization done" <<std::endl;
0200 std::cout<<"All 1d histos done" <<std::endl;
0201
0202 f->Close();
0203 return 0;
0204
0205 }
0206 #endif