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)
225 allCSamplerNames = modTemporary->SamplerCNames();
226 allSSamplerNames = modTemporary->SamplerSNames();
229 samplerNames = allSamplerNames;
230 samplerCNames = allCSamplerNames;
231 samplerSNames = allSSamplerNames;
243 for (
const auto& n : branchNames)
244 {std::cout <<
"DataLoader::BuildEventBranchNameList> Non-sampler : " << n << std::endl;}
245 for (
const auto& n : samplerNames)
246 {std::cout <<
"DataLoader::BuildEventBranchNameList> Sampler : " << n << std::endl;}
247 for (
const auto& n : samplerCNames)
248 {std::cout <<
"DataLoader::BuildEventBranchNameList> SamplerC : " << n << std::endl;}
249 for (
const auto& n : samplerSNames)
250 {std::cout <<
"DataLoader::BuildEventBranchNameList> SamplerS : " << n << std::endl;}
251 for (
const auto& n : collimatorNames)
252 {std::cout <<
"DataLoader::BuildEventBranchNameList> Collimator : " << n << std::endl;}
260 {parChain->Add(fileNames[0].c_str());}
261 for (
const auto& filename : fileNames)
263 heaChain->Add(filename.c_str());
264 beaChain->Add(filename.c_str());
265 optChain->Add(filename.c_str());
266 modChain->Add(filename.c_str());
267 evtChain->Add(filename.c_str());
268 runChain->Add(filename.c_str());
273 const RBDS::BranchMap* bToTurnOn)
283 const RBDS::VectorString* evtBranches =
nullptr;
286 if (bToTurnOn->find(
"Event.") != bToTurnOn->end())
287 {evtBranches = &(*bToTurnOn).at(
"Event.");}
290 for (
const auto& bName: *evtBranches)
292 if (std::find(allSamplerNames.begin(), allSamplerNames.end(), bName +
".") != allSamplerNames.end())
293 {samplerNames.push_back(bName +
".");
continue;}
294 if (std::find(allCSamplerNames.begin(), allCSamplerNames.end(), bName +
".") != allCSamplerNames.end())
295 {samplerCNames.push_back(bName +
".");
continue;}
296 if (std::find(allSSamplerNames.begin(), allSSamplerNames.end(), bName +
".") != allSSamplerNames.end())
297 { samplerSNames.push_back(bName +
".");
continue;}
301 evt->
SetBranchAddress(evtChain, &samplerNames, allOn, evtBranches, &collimatorNames, &samplerCNames, &samplerSNames);
303 const RBDS::VectorString* runBranches =
nullptr;
306 if (bToTurnOn->find(
"Run.") != bToTurnOn->end())
307 {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()
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)