BmnRoot 26.10.0-233-gb9bbe585
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
BmnDataReader.h
Go to the documentation of this file.
1/*
2 BM@N alignment routine
3 BM@N experiment at NICA complex, JINR, 2025
4
5 Department: Math & Soft Group of HEP lab
6 Author: Igor Polev, polev@jinr.int
7
8 BmnDataReader class header-only implementation
9 Stadard interface for reading data from set of ROOT files
10*/
11
12#ifndef BMNDATAREADER_H
13#define BMNDATAREADER_H
14
15#include "TChain.h" // IWYU pragma: keep
16#include "TString.h" // IWYU pragma: keep
17#include "TTreeReader.h" // IWYU pragma: keep
18
19#include <fstream>
20#include <iostream>
21#include <string>
22
23#define BMN_TREE_NAME_DEFAULT "bmndata"
24
26{
27 public:
28 BmnDataReader() = delete;
29 BmnDataReader(const char* filesListPath, Bool_t verbose = kFALSE, const char* treeName = BMN_TREE_NAME_DEFAULT);
30 BmnDataReader(const std::string& filesListPath,
31 Bool_t verbose = kFALSE,
32 const char* treeName = BMN_TREE_NAME_DEFAULT)
33 : BmnDataReader(filesListPath.c_str(), verbose, treeName) {};
34 BmnDataReader(const TString& filesListPath, Bool_t verbose = kFALSE, const char* treeName = BMN_TREE_NAME_DEFAULT)
35 : BmnDataReader(filesListPath.Data(), verbose, treeName) {};
37 {
38 delete fpDataChain;
39 delete fpReader;
40 }
41
42 TChain& Chain() { return *fpDataChain; }
43 TTreeReader& Reader() { return *fpReader; }
44
45 private:
48};
49
50inline BmnDataReader::BmnDataReader(const char* filePathList, Bool_t verbose, const char* treeName)
51{
52 // collect .root files into chain
53 if (verbose)
54 std::cout << "--- BmnDataReader: reading list of files from " << filePathList << std::endl;
55 std::ifstream file(filePathList);
56 if (!file.is_open())
57 throw std::runtime_error(std::string("BmnDataReader: failed to open ROOT list file ") + filePathList);
58 fpDataChain = new TChain(treeName);
59 std::string filePath;
60 Int_t nFiles{0};
61 while (getline(file, filePath)) {
62 // skip comments: leading whitespace then '#'
63 auto firstChar = filePath.find_first_not_of(" \t");
64 if (firstChar == std::string::npos || filePath[firstChar] == '#')
65 continue;
66 if (!fpDataChain->Add(filePath.c_str())) {
67 delete fpDataChain;
68 throw std::runtime_error(std::string("BmnDataReader: failed to access file ") + filePath + " or tree "
69 + treeName + " not found");
70 } else if (verbose) {
71 std::cout << " BmnDataReader: file " << filePath << " added" << std::endl;
72 nFiles++;
73 }
74 }
75 file.close();
76 if (verbose)
77 std::cout << "--- BmnDataReader: " << nFiles << " added successfully" << std::endl;
78
79 // init reader with warmup (to avoid misterious ROOT hockus-pokus with trees)
81 fpReader->Next();
82 fpReader->Restart();
83 if (-1 == fpReader->GetEntries(kTRUE)) {
84 delete fpReader;
85 delete fpDataChain;
86 throw std::runtime_error(std::string("BmnDataReader: failed to init reader for ") + treeName);
87 }
88}
89
90#endif // BMNDATAREADER_H
#define BMN_TREE_NAME_DEFAULT
bool Bool_t
int Int_t
constexpr Bool_t kFALSE
constexpr Bool_t kTRUE
BmnDataReader(const TString &filesListPath, Bool_t verbose=kFALSE, const char *treeName=BMN_TREE_NAME_DEFAULT)
TTreeReader & Reader()
TChain * fpDataChain
TTreeReader * fpReader
BmnDataReader(const std::string &filesListPath, Bool_t verbose=kFALSE, const char *treeName=BMN_TREE_NAME_DEFAULT)
TChain & Chain()
BmnDataReader()=delete