19#include "DataLoader.hh"
22#include "FileMapper.hh"
23#include "ParticleData.hh"
27#include "RBDSException.hh"
28#include "RebdsimTypes.hh"
32#include "BDSOutputROOTEventAperture.hh"
33#include "BDSOutputROOTEventCollimator.hh"
34#include "BDSOutputROOTEventSampler.hh"
35#include "BDSVersionData.hh"
47DataLoader::DataLoader(
const std::string& fileName,
49 bool processSamplersIn,
51 const RBDS::BranchMap* branchesToTurnOnIn,
52 bool backwardsCompatibleIn):
54 processSamplers(processSamplersIn),
55 allBranchesOn(allBranchesOnIn),
56 branchesToTurnOn(branchesToTurnOnIn),
57 backwardsCompatible(backwardsCompatibleIn),
59 dataVersion(BDSIM_DATA_VERSION)
64DataLoader::~DataLoader()
88 bea =
new Beam(debug);
94 heaChain =
new TChain(
"Header",
"Header");
96 {parChain =
new TChain(
"ParticleData",
"ParticleData");}
97 beaChain =
new TChain(
"Beam",
"Beam");
98 optChain =
new TChain(
"Options",
"Options");
99 modChain =
new TChain(
"Model",
"Model");
100 evtChain =
new TChain(
"Event",
"Event");
101 runChain =
new TChain(
"Run",
"Run");
110 parChain->GetEntry(0);
115 BDSOutputROOTEventCollimator::particleTable = par->
particleData;
116 BDSOutputROOTEventAperture::particleTable = par->
particleData;
123 if (inputPath.empty())
124 {
throw RBDSException(
"DataLoader::BuildInputFileList> no file specified");}
127 std::vector<std::string> fileNamesTemp;
128 if (inputPath.find(
'*') != std::string::npos)
131 glob(inputPath.c_str(), GLOB_TILDE,
nullptr, &glob_result);
132 for(
unsigned int i = 0; i < glob_result.gl_pathc; ++i)
133 {fileNamesTemp.emplace_back(glob_result.gl_pathv[i]);}
134 globfree(&glob_result);
137 else if (inputPath.find(
".root") != std::string::npos)
138 {fileNamesTemp.push_back(inputPath);}
140 else if (inputPath[inputPath.length()-1] == std::string(
"/"))
143 inputPath.append(
"/*.root");
146 glob(inputPath.c_str(), GLOB_TILDE,
nullptr, &glob_result);
147 for (
unsigned int i = 0; i < glob_result.gl_pathc; ++i)
148 {fileNamesTemp.emplace_back(glob_result.gl_pathv[i]);}
149 globfree(&glob_result);
153 int* fileDataVersion =
nullptr;
154 if (!backwardsCompatible)
156 fileDataVersion =
new int();
157 (*fileDataVersion) = 0;
159 for (
const auto& fn : fileNamesTemp)
161 if (backwardsCompatible)
162 {fileNames.push_back(fn);}
165 int value = fileDataVersion ? *fileDataVersion : -1;
166 std::cout <<
"Loading> \"" << fn <<
"\" : data version " << value << std::endl;
167 fileNames.push_back(fn);
171 {std::cout << fn <<
" is not a BDSIM output file - skipping!" << std::endl;}
173 delete fileDataVersion;
175 if (fileNames.empty())
176 {
throw RBDSException(
"DataLoader - No valid files found - check input file path / name");}
182 TFile* f =
new TFile(fileNames[0].c_str());
184 {
throw RBDSException(__METHOD_NAME__,
"No such file \"" + fileNames[0] +
"\"");}
186 TList* kl = f->GetListOfKeys();
187 for (
int i = 0; i < kl->GetEntries(); ++i)
188 {treeNames.emplace_back(std::string(kl->At(i)->GetName()));}
195 for (
const auto& tr : treeNames)
196 {std::cout <<
"DataLoader::BuildTreeNameList> " << tr << std::endl;}
202 TFile* f =
new TFile(fileNames[0].c_str());
204 {
throw RBDSException(__METHOD_NAME__,
"No such file \"" + fileNames[0] +
"\"");}
206 TTree* mt = (TTree*)f->Get(
"Model");
214 if (mt->GetEntries() == 0)
217 TTree* et = (TTree*)f->Get(
"Event");
222 throw RBDSException(__METHOD_NAME__,
"No Event tree in file - likely a corrupted file.");
224 const std::set<std::string> samplerClasses = {
"BDSOutputROOTEventSampler",
225 "BDSOutputROOTEventSampler<float>",
226 "BDSOutputROOTEventSampler<double>",
227 "BDSOutputROOTEventSamplerC",
228 "BDSOutputROOTEventSamplerS"};
229 std::map<std::string, std::vector<std::string>*> vectors = {
230 {
"BDSOutputROOTEventSampler", &allSamplerNames},
231 {
"BDSOutputROOTEventSampler<float>", &allSamplerNames},
232 {
"BDSOutputROOTEventSampler<double>", &allSamplerNames},
233 {
"BDSOutputROOTEventSamplerC", &allCSamplerNames},
234 {
"BDSOutputROOTEventSamplerS", &allSSamplerNames},
236 TObjArray* branches = et->GetListOfBranches();
237 for (
auto branch : *branches)
239 TBranch* b = (TBranch*)branch;
240 std::string branchClassName = std::string(b->GetClassName());
241 if (samplerClasses.count(branchClassName) != 0)
242 {vectors[branchClassName]->push_back(std::string(b->GetName()));}
243 else if (branchClassName ==
"BDSOutputROOTEventCollimator")
244 {collimatorNames.push_back(std::string(b->GetName()));}
255 allCSamplerNames = modTemporary->SamplerCNames();
256 allSSamplerNames = modTemporary->SamplerSNames();
265 allSamplerCNamesSet.insert(allCSamplerNames.begin(), allCSamplerNames.end());
267 allSamplerSNamesSet.insert(allSSamplerNames.begin(), allSSamplerNames.end());
271 samplerNames = allSamplerNames;
272 samplerCNames = allCSamplerNames;
273 samplerSNames = allSSamplerNames;
278 std::cout <<
"All sampler names:" << std::endl;
279 for (
const auto& n : allSamplerNames)
280 {std::cout <<
"DataLoader::BuildEventBranchNameList> Sampler : " << n << std::endl;}
281 for (
const auto& n : allCSamplerNames)
282 {std::cout <<
"DataLoader::BuildEventBranchNameList> SamplerC : " << n << std::endl;}
283 for (
const auto& n : allSSamplerNames)
284 {std::cout <<
"DataLoader::BuildEventBranchNameList> SamplerS : " << n << std::endl;}
285 for (
const auto& n : collimatorNames)
286 {std::cout <<
"DataLoader::BuildEventBranchNameList> Collimator : " << n << std::endl;}
294 {parChain->Add(fileNames[0].c_str());}
295 for (
const auto& filename : fileNames)
297 heaChain->Add(filename.c_str());
298 beaChain->Add(filename.c_str());
299 optChain->Add(filename.c_str());
300 modChain->Add(filename.c_str());
301 evtChain->Add(filename.c_str());
302 runChain->Add(filename.c_str());
307 const RBDS::BranchMap* bToTurnOn)
317 const RBDS::VectorString* evtBranches =
nullptr;
320 if (bToTurnOn->find(
"Event.") != bToTurnOn->end())
321 {evtBranches = &(*bToTurnOn).at(
"Event.");}
324 for (
const auto& bName: *evtBranches)
326 if (std::find(allSamplerNames.begin(), allSamplerNames.end(), bName +
".") != allSamplerNames.end())
327 {samplerNames.push_back(bName +
".");
continue;}
328 if (std::find(allCSamplerNames.begin(), allCSamplerNames.end(), bName +
".") != allCSamplerNames.end())
329 {samplerCNames.push_back(bName +
".");
continue;}
330 if (std::find(allSSamplerNames.begin(), allSSamplerNames.end(), bName +
".") != allSSamplerNames.end())
331 {samplerSNames.push_back(bName +
".");
continue;}
335 evt->
SetBranchAddress(evtChain, &samplerNames, allOn, evtBranches, &collimatorNames, &samplerCNames, &samplerSNames);
337 const RBDS::VectorString* runBranches =
nullptr;
340 if (bToTurnOn->find(
"Run.") != bToTurnOn->end())
341 {runBranches = &(*bToTurnOn).at(
"Run.");}
Information stored per sampler per event.
void SetBranchAddress(TTree *t, bool allBranchesOn=true, const RBDS::VectorString *branchesToTurnOn=nullptr)
Set the branch addresses to address the contents of the file.
Loader for a ROOT file using classes used to generate the file.
void SetBranchAddress(bool allOn=true, const RBDS::BranchMap *bToTurnOn=nullptr)
Map each chain to the member instance of each storage class in this class.
void BuildEventBranchNameList()
std::set< std::string > allSamplerCAndSNames
int dataVersion
Integer version of data loaded.
void CommonCtor(const std::string &fileName)
void BuildTreeNameList()
Open the first file in the file list and map the trees in it.
void BuildInputFileList(std::string inputPath)
Build up the input file list.
void ChainTrees()
Create a tree for each sampler and add all the files to it.
void SetBranchAddress(TTree *t, const RBDS::VectorString *samplerNames=nullptr, bool allBranchesOn=false, const RBDS::VectorString *branchesToTurnOn=nullptr, const RBDS::VectorString *collimatorNamesIn=nullptr, const RBDS::VectorString *samplerCNamesIn=nullptr, const RBDS::VectorString *samplerSNamesIn=nullptr)
std::vector< std::string > CollimatorNames() const
Access all the sampler branch names from the model.
void SetBranchAddress(TTree *t, bool allBranchesOn=true, const RBDS::VectorString *branchesToTurnOn=nullptr)
Set the branch addresses to address the contents of the file.
std::vector< std::string > SamplerNames() const
Access all the unique sampler names from the model.
void SetBranchAddress(TTree *t, bool allBranchesOn=true, const RBDS::VectorString *branchesToTurnOn=nullptr)
Set the branch addresses to address the contents of the file.
void SetBranchAddress(TTree *t)
Set the branch addresses to address the contents of the file.
BDSOutputROOTParticleData * particleData
Member that ROOT can map file data to locally.
General exception with possible name of object and message.
Class to overlay a run from a ROOT file with.
void SetBranchAddress(TTree *t, bool allBranchesOn=true, const RBDS::VectorString *branchesToTurnOn=nullptr)
Map the ROOT file to members in this class.
bool IsBDSIMOutputFile(TFile *file, int *dataVersion=nullptr)