18#include "DataLoader.hh"
21#include "FileMapper.hh"
22#include "ParticleData.hh"
26#include "RBDSException.hh"
27#include "RebdsimTypes.hh"
31#include "BDSOutputROOTEventAperture.hh"
32#include "BDSOutputROOTEventCollimator.hh"
33#include "BDSOutputROOTEventSampler.hh"
34#include "BDSVersionData.hh"
46DataLoader::DataLoader(
const std::string& fileName,
48 bool processSamplersIn,
50 const RBDS::BranchMap* branchesToTurnOnIn,
51 bool backwardsCompatibleIn):
53 processSamplers(processSamplersIn),
54 allBranchesOn(allBranchesOnIn),
55 branchesToTurnOn(branchesToTurnOnIn),
56 backwardsCompatible(backwardsCompatibleIn),
58 dataVersion(BDSIM_DATA_VERSION)
63DataLoader::~DataLoader()
87 bea =
new Beam(debug);
93 heaChain =
new TChain(
"Header",
"Header");
95 {parChain =
new TChain(
"ParticleData",
"ParticleData");}
96 beaChain =
new TChain(
"Beam",
"Beam");
97 optChain =
new TChain(
"Options",
"Options");
98 modChain =
new TChain(
"Model",
"Model");
99 evtChain =
new TChain(
"Event",
"Event");
100 runChain =
new TChain(
"Run",
"Run");
109 parChain->GetEntry(0);
114 BDSOutputROOTEventCollimator::particleTable = par->
particleData;
115 BDSOutputROOTEventAperture::particleTable = par->
particleData;
122 if (inputPath.empty())
123 {
throw RBDSException(
"DataLoader::BuildInputFileList> no file specified");}
126 std::vector<std::string> fileNamesTemp;
127 if (inputPath.find(
'*') != std::string::npos)
130 glob(inputPath.c_str(), GLOB_TILDE,
nullptr, &glob_result);
131 for(
unsigned int i = 0; i < glob_result.gl_pathc; ++i)
132 {fileNamesTemp.emplace_back(glob_result.gl_pathv[i]);}
133 globfree(&glob_result);
136 else if (inputPath.find(
".root") != std::string::npos)
137 {fileNamesTemp.push_back(inputPath);}
139 else if (inputPath[inputPath.length()-1] == std::string(
"/"))
142 inputPath.append(
"/*.root");
145 glob(inputPath.c_str(), GLOB_TILDE,
nullptr, &glob_result);
146 for (
unsigned int i = 0; i < glob_result.gl_pathc; ++i)
147 {fileNamesTemp.emplace_back(glob_result.gl_pathv[i]);}
148 globfree(&glob_result);
152 int* fileDataVersion =
nullptr;
153 if (!backwardsCompatible)
155 fileDataVersion =
new int();
156 (*fileDataVersion) = 0;
158 for (
const auto& fn : fileNamesTemp)
160 if (backwardsCompatible)
161 {fileNames.push_back(fn);}
164 int value = fileDataVersion ? *fileDataVersion : -1;
165 std::cout <<
"Loading> \"" << fn <<
"\" : data version " << value << std::endl;
166 fileNames.push_back(fn);
170 {std::cout << fn <<
" is not a BDSIM output file - skipping!" << std::endl;}
172 delete fileDataVersion;
174 if (fileNames.empty())
175 {
throw RBDSException(
"DataLoader - No valid files found - check input file path / name");}
181 TFile* f =
new TFile(fileNames[0].c_str());
183 {
throw RBDSException(__METHOD_NAME__,
"No such file \"" + fileNames[0] +
"\"");}
185 TList* kl = f->GetListOfKeys();
186 for (
int i = 0; i < kl->GetEntries(); ++i)
187 {treeNames.emplace_back(std::string(kl->At(i)->GetName()));}
194 for (
const auto& tr : treeNames)
195 {std::cout <<
"DataLoader::BuildTreeNameList> " << tr << std::endl;}
201 TFile* f =
new TFile(fileNames[0].c_str());
203 {
throw RBDSException(__METHOD_NAME__,
"No such file \"" + fileNames[0] +
"\"");}
205 TTree* mt = (TTree*)f->Get(
"Model");
213 if (mt->GetEntries() == 0)
216 TTree* et = (TTree*)f->Get(
"Event");
221 throw RBDSException(__METHOD_NAME__,
"No Event tree in file - likely a corrupted file.");
223 const std::set<std::string> samplerClasses = {
"BDSOutputROOTEventSampler",
224 "BDSOutputROOTEventSampler<float>",
225 "BDSOutputROOTEventSampler<double>",
226 "BDSOutputROOTEventSamplerC",
227 "BDSOutputROOTEventSamplerS"};
228 std::map<std::string, std::vector<std::string>*> vectors = {
229 {
"BDSOutputROOTEventSampler", &allSamplerNames},
230 {
"BDSOutputROOTEventSampler<float>", &allSamplerNames},
231 {
"BDSOutputROOTEventSampler<double>", &allSamplerNames},
232 {
"BDSOutputROOTEventSamplerC", &allCSamplerNames},
233 {
"BDSOutputROOTEventSamplerS", &allSSamplerNames},
235 TObjArray* branches = et->GetListOfBranches();
236 for (
auto branch : *branches)
238 TBranch* b = (TBranch*)branch;
239 std::string branchClassName = std::string(b->GetClassName());
240 if (samplerClasses.count(branchClassName) != 0)
241 {vectors[branchClassName]->push_back(std::string(b->GetName()));}
242 else if (branchClassName ==
"BDSOutputROOTEventCollimator")
243 {collimatorNames.push_back(std::string(b->GetName()));}
254 allCSamplerNames = modTemporary->SamplerCNames();
255 allSSamplerNames = modTemporary->SamplerSNames();
264 allSamplerCNamesSet.insert(allCSamplerNames.begin(), allCSamplerNames.end());
266 allSamplerSNamesSet.insert(allSSamplerNames.begin(), allSSamplerNames.end());
270 samplerNames = allSamplerNames;
271 samplerCNames = allCSamplerNames;
272 samplerSNames = allSSamplerNames;
277 std::cout <<
"All sampler names:" << std::endl;
278 for (
const auto& n : allSamplerNames)
279 {std::cout <<
"DataLoader::BuildEventBranchNameList> Sampler : " << n << std::endl;}
280 for (
const auto& n : allCSamplerNames)
281 {std::cout <<
"DataLoader::BuildEventBranchNameList> SamplerC : " << n << std::endl;}
282 for (
const auto& n : allSSamplerNames)
283 {std::cout <<
"DataLoader::BuildEventBranchNameList> SamplerS : " << n << std::endl;}
284 for (
const auto& n : collimatorNames)
285 {std::cout <<
"DataLoader::BuildEventBranchNameList> Collimator : " << n << std::endl;}
293 {parChain->Add(fileNames[0].c_str());}
294 for (
const auto& filename : fileNames)
296 heaChain->Add(filename.c_str());
297 beaChain->Add(filename.c_str());
298 optChain->Add(filename.c_str());
299 modChain->Add(filename.c_str());
300 evtChain->Add(filename.c_str());
301 runChain->Add(filename.c_str());
306 const RBDS::BranchMap* bToTurnOn)
316 const RBDS::VectorString* evtBranches =
nullptr;
319 if (bToTurnOn->find(
"Event.") != bToTurnOn->end())
320 {evtBranches = &(*bToTurnOn).at(
"Event.");}
323 for (
const auto& bName: *evtBranches)
325 if (std::find(allSamplerNames.begin(), allSamplerNames.end(), bName +
".") != allSamplerNames.end())
326 {samplerNames.push_back(bName +
".");
continue;}
327 if (std::find(allCSamplerNames.begin(), allCSamplerNames.end(), bName +
".") != allCSamplerNames.end())
328 {samplerCNames.push_back(bName +
".");
continue;}
329 if (std::find(allSSamplerNames.begin(), allSSamplerNames.end(), bName +
".") != allSSamplerNames.end())
330 {samplerSNames.push_back(bName +
".");
continue;}
334 evt->
SetBranchAddress(evtChain, &samplerNames, allOn, evtBranches, &collimatorNames, &samplerCNames, &samplerSNames);
336 const RBDS::VectorString* runBranches =
nullptr;
339 if (bToTurnOn->find(
"Run.") != bToTurnOn->end())
340 {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)