SND@LHC Software
Loading...
Searching...
No Matches
sndTchainGetter.cxx
Go to the documentation of this file.
1#include "sndTchainGetter.h"
2
3#include <string>
4#include <stdexcept>
5#include <fstream>
6#include <sstream>
7#include <memory>
8#include <cstdlib>
9
10#include "TChain.h"
11#include <TString.h>
12
13std::string snd::analysis_tools::GetDataBasePath(int run_number, std::string csv_file_path) {
14 if (csv_file_path.empty()) {
15 csv_file_path = std::string(getenv("SNDSW_ROOT")) + "/analysis/tools/data_paths.csv";
16 }
17 std::ifstream file(csv_file_path);
18 if (!file.is_open()) {
19 throw std::runtime_error("Could not open CSV file: " + csv_file_path);
20 }
21
22 std::string line;
23 std::getline(file, line); // skip header
24
25 while (std::getline(file, line)) {
26 std::istringstream ss(line);
27 std::string token;
28
29 std::getline(ss, token, ',');
30 int min_run = std::stoi(token);
31
32 std::getline(ss, token, ',');
33 int max_run = std::stoi(token);
34
35 std::getline(ss, token);
36 std::string path = token;
37
38 if (run_number >= min_run && run_number <= max_run) {
39 return path;
40 }
41 }
42 throw std::runtime_error("Run number not found in CSV mapping.");
43}
44
45std::unique_ptr<TChain> snd::analysis_tools::GetTChain(int run_number, int n_files, const std::string& csv_file_path){
46 std::string base_folder = GetDataBasePath(run_number, csv_file_path);
47 auto tchain = std::make_unique<TChain>("rawConv");
48 if (n_files == -1) {
49 tchain->Add(Form("%srun_%06d/sndsw_raw-*", base_folder.c_str(), run_number));
50 }
51 else {
52 for (int i = 0; i<n_files; ++i){
53 tchain->Add(Form("%srun_%06d/sndsw_raw-%04d.root", base_folder.c_str(), run_number, i));
54 }
55 }
56 return tchain;
57};
58
59std::unique_ptr<TChain> snd::analysis_tools::GetTChain(const std::string& file_name){
60 auto tchain = std::make_unique<TChain>("rawConv");
61 tchain->Add(file_name.c_str());
62 return tchain;
63};
std::unique_ptr< TChain > GetTChain(int run_number, int n_files=-1, const std::string &csv_file_path="")
std::string GetDataBasePath(int run_number, std::string csv_file_path="")