8 #include "TClonesArray.h" 11 #include "TLorentzVector.h" 12 #include "TParticle.h" 25 std::vector<std::string> PidStrings;
26 for (
auto Pid : Pids) {
27 auto PidString = std::to_string(Pid);
28 if (PidString.front() ==
'-')
29 PidString.front() =
'_';
30 PidStrings.push_back(PidString);
32 for (
const auto PidString : PidStrings) {
34 if (std::count(PidStrings.begin(), PidStrings.end(), PidString) > 1) {
35 auto Iter = PidStrings.begin();
36 while (Iter != PidStrings.end()) {
37 Iter = std::find(Iter, PidStrings.end(), PidString);
38 *Iter +=
"_" + std::to_string(Count);
47 std::vector<pid> Pids;
48 for (
const auto &PidString : UniqueStrings) {
50 auto StrippedPidString = PidString.substr(0, PidString.find(
"_", 0));
51 Pids.push_back(std::stoi(StrippedPidString));
52 }
catch (
const std::invalid_argument &e) {
54 "\" cannot be converted to a PID");
61 const std::string &TreeName,
62 long long NumberOfEventsToRead) {
64 auto temp_ErrorIgnoreLevel = gErrorIgnoreLevel;
65 gErrorIgnoreLevel = kBreak;
68 TChain Chain(TreeName.c_str());
69 Chain.Add(InputFilePath.c_str());
72 auto ListOfFiles = Chain.GetListOfFiles();
73 if (!ListOfFiles || !ListOfFiles->GetEntries()) {
77 if (!Chain.GetEntries()) {
79 "\" cannot be opened from file(s) " +
82 if (NumberOfEventsToRead <= 0 || NumberOfEventsToRead > Chain.GetEntries()) {
83 NumberOfEventsToRead = Chain.GetEntries();
85 if (!Chain.GetBranch(
"weights")) {
87 "\" in file \"" + InputFilePath +
88 "\" does not contain weights branch");
92 std::vector<std::string> PidStrings;
93 auto Branches = Chain.GetListOfBranches();
94 for (
int i = 0; i < Branches->GetEntries(); ++i) {
95 auto Branch = Branches->At(i);
98 std::string Name = Branch->GetName();
99 if (Name ==
"weights")
101 PidStrings.push_back(Name);
103 if (!PidStrings.size()) {
105 "\" in file \"" + InputFilePath +
106 "\" does not contain any LorentzVector branches");
112 std::vector<TLorentzVector *> LorentzVectors(ImportedDataSample.Pids.size());
113 if (Chain.SetBranchAddress(
"weights", &Weight))
114 throw std::runtime_error(
115 "Could not set branch address of branch \"weights\"");
117 TIter Next(Chain.GetListOfBranches());
118 while (TObject *obj = Next()) {
119 std::string BranchName = obj->GetName();
120 if (BranchName ==
"weights")
122 if (Chain.SetBranchAddress(BranchName.c_str(), &LorentzVectors.at(i)))
123 throw std::runtime_error(
"Could not set branch address of branch \"" +
129 ImportedDataSample.Events.resize(NumberOfEventsToRead);
130 for (Long64_t i = 0; i < NumberOfEventsToRead; ++i) {
132 auto &
Event = ImportedDataSample.Events.at(i);
134 for (
const auto &LorentzVector : LorentzVectors) {
137 LorentzVector->Pz(), LorentzVector->Energy()));
141 gErrorIgnoreLevel = temp_ErrorIgnoreLevel;
142 return ImportedDataSample;
146 const std::string &OutputFileName,
const std::string &TreeName,
147 bool OverwriteFile) {
149 auto temp_ErrorIgnoreLevel = gErrorIgnoreLevel;
150 gErrorIgnoreLevel = kBreak;
153 if (OutputSample.
Events.size() == 0) {
159 "Root::writeData(): number of PIDs in EventCollection " 160 "does not match that of the PID list!");
164 std::string WriteFlag{
"UPDATE"};
166 WriteFlag =
"RECREATE";
167 TFile File(OutputFileName.c_str(), WriteFlag.c_str());
168 if (File.IsZombie()) {
169 throw std::runtime_error(
"Root::writeData(): can't open data file: " +
172 LOG(INFO) <<
"Root::writeData(): writing vector of " 173 << OutputSample.
Events.size() <<
" events to file " 177 TTree Tree(TreeName.c_str(), TreeName.c_str());
179 std::vector<TLorentzVector> LorentzVectors(OutputSample.
Pids.size());
181 Tree.Branch(
"weights", &Weight);
182 for (
size_t i = 0; i < LorentzVectors.size(); ++i) {
183 Tree.Branch(BranchNames.at(i).c_str(), &LorentzVectors.at(i));
189 auto &LorentzVector = LorentzVectors.at(i);
199 gErrorIgnoreLevel = temp_ErrorIgnoreLevel;
ComPWA four momentum class.
Input data file is corrupt or incomplete.
std::vector< pid > uniqueStringsToPids(std::vector< std::string > UniqueStrings)
void writeData(const EventCollection &OutputSample, const std::string &OutputFileName, const std::string &TreeName, bool OverwriteFile)
Write a vector of Events to a ROOT file.
std::vector< Event > Events
std::vector< std::string > pidsToUniqueStrings(std::vector< pid > Pids)
ComPWA::EventCollection readData(const std::string &InputFilePath, const std::string &TreeName, long long NumberOfEventsToRead)
Create a vector of Events from a ROOT file.
std::vector< FourMomentum > FourMomenta
Data structure containing all kinematic information of a physics event.
bool checkPidMatchesEvents() const