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
9#include "TChain.h"
10#include <TString.h>
11
12std::string snd::analysis_tools::GetDataBasePath(const std::string& csv_file_path, int run_number) {
13 std::ifstream file(csv_file_path);
14 if (!file.is_open()) {
15 throw std::runtime_error("Could not open CSV file: " + csv_file_path);
16 }
17
18 std::string line;
19 std::getline(file, line); // skip header
20
21 while (std::getline(file, line)) {
22 std::istringstream ss(line);
23 std::string token;
24
25 std::getline(ss, token, ',');
26 int min_run = std::stoi(token);
27
28 std::getline(ss, token, ',');
29 int max_run = std::stoi(token);
30
31 std::getline(ss, token);
32 std::string path = token;
33
34 if (run_number >= min_run && run_number <= max_run) {
35 return path;
36 }
37 }
38 throw std::runtime_error("Run number not found in CSV mapping.");
39}
40
41std::unique_ptr<TChain> snd::analysis_tools::GetTChain(const std::string& csv_file_path, int run_number, int n_files){
42 std::string base_folder = GetDataBasePath(csv_file_path, run_number);
43 auto tchain = std::make_unique<TChain>("rawConv");
44 if (n_files == -1) {
45 tchain->Add(Form("%srun_%06d/sndsw_raw-*", base_folder.c_str(), run_number));
46 }
47 else {
48 for (int i = 0; i<n_files; ++i){
49 tchain->Add(Form("%srun_%06d/sndsw_raw-%04d.root", base_folder.c_str(), run_number, i));
50 }
51 }
52 return tchain;
53};
54
55std::unique_ptr<TChain> snd::analysis_tools::GetTChain(std::string file_name){
56 auto tchain = std::make_unique<TChain>("rawConv");
57 tchain->Add(file_name.c_str());
58 return tchain;
59};
std::string GetDataBasePath(const std::string &csv_file_path, int run_number)
std::unique_ptr< TChain > GetTChain(const std::string &csv_file_path, int run_number, int n_files=-1)