Belle II Software light-2505-deimos
FileMetaData.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <boost/python/class.hpp>
10#include <boost/python/copy_const_reference.hpp>
11
12#include <framework/dataobjects/FileMetaData.h>
13#include <framework/utilities/HTML.h>
14#include <framework/utilities/KeyValuePrinter.h>
15
16#include <boost/algorithm/string.hpp>
17
18#include <nlohmann/json.hpp>
19
20#include <iostream>
21
22using namespace std;
23using namespace Belle2;
24using namespace boost::python;
25
32
33bool FileMetaData::containsEvent(int experiment, int run, unsigned int event) const
34{
35 if (experiment < m_experimentLow) return false;
36 if (experiment == m_experimentLow) {
37 if (run < m_runLow) return false;
38 if ((run == m_runLow) && (event < m_eventLow)) return false;
39 }
40 if (experiment > m_experimentHigh) return false;
41 if (experiment == m_experimentHigh) {
42 if (run > m_runHigh) return false;
43 if ((run == m_runHigh) && (event > m_eventHigh)) return false;
44 }
45 return true;
46}
47
49{
50 //Note: these can only be used with update_file_metadata(), PyROOT is the more common interface
51 class_<FileMetaData>("FileMetaData")
52 .def("get_lfn", &FileMetaData::getLfn, return_value_policy<copy_const_reference>())
53 .def("get_nevents", &FileMetaData::getNEvents)
54 .def("get_nfullevents", &FileMetaData::getNFullEvents)
55 .def("get_experiment_low", &FileMetaData::getExperimentLow)
56 .def("get_run_low", &FileMetaData::getRunLow)
57 .def("get_event_low", &FileMetaData::getEventLow)
58 .def("get_experiment_high", &FileMetaData::getExperimentHigh)
59 .def("get_run_high", &FileMetaData::getRunHigh)
60 .def("get_event_high", &FileMetaData::getEventHigh)
61 .def("get_n_parents", &FileMetaData::getNParents)
62 .def("get_parent", &FileMetaData::getParent, return_value_policy<copy_const_reference>())
63 .def("get_date", &FileMetaData::getDate, return_value_policy<copy_const_reference>())
64 .def("get_site", &FileMetaData::getSite, return_value_policy<copy_const_reference>())
65 .def("get_user", &FileMetaData::getUser, return_value_policy<copy_const_reference>())
66 .def("get_random_seed", &FileMetaData::getRandomSeed, return_value_policy<copy_const_reference>())
67 .def("get_release", &FileMetaData::getRelease, return_value_policy<copy_const_reference>())
68 .def("get_steering", &FileMetaData::getSteering, return_value_policy<copy_const_reference>())
69 .def("is_mc", &FileMetaData::isMC)
70 .def("get_mc_events", &FileMetaData::getMcEvents)
71 .def("get_global_tag", &FileMetaData::getDatabaseGlobalTag, return_value_policy<copy_const_reference>())
72 .def("get_data_description", &FileMetaData::getDataDescription, return_value_policy<copy_const_reference>())
73 .def("set_lfn", &FileMetaData::setLfn);
74}
75
76
77void FileMetaData::Print(Option_t* option) const
78{
79 if (option && (option == std::string("steering"))) {
80 cout << m_steering << endl;
81 return;
82 } else if (option && (option == std::string("json"))) {
83 cout << getJsonStr() << endl;
84 return;
85 }
86 const bool all = (option && option == std::string("all"));
87 KeyValuePrinter printer(false);
88 printer.put("LFN", m_lfn);
89 printer.put("nEvents", m_nEvents);
90 printer.put("nFullEvents", m_nFullEvents);
91 printer.put("range", std::to_string(m_experimentLow) + "/" + std::to_string(m_runLow) + "/" + std::to_string(m_eventLow)
92 + " - " + std::to_string(m_experimentHigh) + "/" + std::to_string(m_runHigh) + "/" + std::to_string(m_eventHigh));
93 printer.put("parents", m_parentLfns);
94 if (all) {
95 printer.put("date", m_date);
96 printer.put("site", m_site);
97 printer.put("user", m_user);
98 printer.put("randomSeed", m_randomSeed);
99 printer.put("release", m_release);
100 printer.put("isMC", m_isMC);
101 printer.put("mcEvents", m_mcEvents);
102 printer.put("globalTag", m_databaseGlobalTag);
103 printer.put("dataDescription", m_dataDescription);
104 }
105 std::cout << "=== FileMetaData ===\n";
106 std::cout << printer.string();
107}
108
109bool FileMetaData::read(std::istream& input, std::string& physicalFileName)
110{
111 physicalFileName = "";
112 if (input.eof()) return false;
113
114 std::string line;
115 std::getline(input, line);
116 boost::algorithm::trim(line);
117 if (line.compare("<File>") != 0) return false;
118
119 while (!input.eof()) {
120 std::getline(input, line);
121 boost::algorithm::trim(line);
122 if (line.compare("</File>") == 0) return true;
123
124 int pos = line.find('>') + 1;
125 std::string tag = line.substr(0, pos);
126 std::string value = line.substr(pos, line.rfind('<') - pos);
127 if (tag.compare("<LFN>") == 0) {
128 m_lfn = HTML::unescape(value);
129 } else if (tag.compare("<PFN>") == 0) {
130 physicalFileName = HTML::unescape(value);
131 } else if (tag.compare("<ExperimentLow>") == 0) {
132 m_experimentLow = stoi(value);
133 } else if (tag.compare("<RunLow>") == 0) {
134 m_runLow = stoi(value);
135 } else if (tag.compare("<EventLow>") == 0) {
136 m_eventLow = stoi(value);
137 } else if (tag.compare("<ExperimentHigh>") == 0) {
138 m_experimentHigh = stoi(value);
139 } else if (tag.compare("<RunHigh>") == 0) {
140 m_runHigh = stoi(value);
141 } else if (tag.compare("<EventHigh>") == 0) {
142 m_eventHigh = stoi(value);
143 } else if (tag.compare("<Parents>") == 0) {
144 pos = value.find(',');
145 while (pos > 0) {
146 m_parentLfns.push_back(HTML::unescape(value.substr(0, pos)));
147 value.erase(0, pos + 1);
148 pos = value.find(',');
149 }
150 m_parentLfns.push_back(HTML::unescape(value));
151 }
152 }
153
154 return false;
155}
156
157bool FileMetaData::write(std::ostream& output, const std::string& physicalFileName) const
158{
159 output << " <File>\n";
160 output << " <LFN>" << HTML::escape(m_lfn) << "</LFN>\n";
161 if (!physicalFileName.empty()) {
162 output << " <PFN>" << HTML::escape(physicalFileName) << "</PFN>\n";
163 }
164 output << " <ExperimentLow>" << m_experimentLow << "</ExperimentLow>\n";
165 output << " <RunLow>" << m_runLow << "</RunLow>\n";
166 output << " <EventLow>" << m_eventLow << "</EventLow>\n";
167 output << " <ExperimentHigh>" << m_experimentHigh << "</ExperimentHigh>\n";
168 output << " <RunHigh>" << m_runHigh << "</RunHigh>\n";
169 output << " <EventHigh>" << m_eventHigh << "</EventHigh>\n";
170 if (!m_parentLfns.empty()) {
171 output << " <Parents>" << HTML::escape(m_parentLfns[0]);
172 for (unsigned int parent = 1; parent < m_parentLfns.size(); parent++) {
173 output << "," << HTML::escape(m_parentLfns[parent]);
174 }
175 output << "</Parents>\n";
176 }
177 output << " </File>\n";
178
179 return true;
180}
181
182std::string FileMetaData::getJsonStr() const
183{
184 nlohmann::json metadata = {
185 {"LFN", m_lfn},
186 {"nEvents", m_nEvents},
187 {"nFullEvents", m_nFullEvents},
188 {"experimentLow", m_experimentLow},
189 {"runLow", m_runLow},
190 {"eventLow", m_eventLow},
191 {"experimentHigh", m_experimentHigh},
192 {"runHigh", m_runHigh},
193 {"eventHigh", m_eventHigh},
194 {"parents", m_parentLfns},
195 {"date", m_date},
196 {"site", m_site},
197 {"user", m_user},
198 {"randomSeed", m_randomSeed},
199 {"release", m_release},
200 {"isMC", m_isMC},
201 {"mcEvents", m_mcEvents},
202 {"globalTag", m_databaseGlobalTag},
203 {"dataDescription", m_dataDescription},
204 {"steering", m_steering}
205 };
206 return metadata.dump(2);
207}
virtual void Print(Option_t *option="") const override
Print the content of the meta data object.
const std::string & getDatabaseGlobalTag() const
Get the database global tag used when creating this file.
std::string m_steering
The steering file content.
unsigned int getEventHigh() const
Highest event number in highest run getter.
unsigned int m_eventHigh
Highest event number in highest run.
int getRunHigh() const
Highest run number getter.
const std::string & getLfn() const
Logical file name getter.
int getNParents() const
Get number of parent files.
std::string m_randomSeed
The random seed used when producing the file.
std::string m_databaseGlobalTag
Global tag in the database used for production of this file.
unsigned int m_nEvents
Number of events.
const std::string & getRelease() const
Software release version getter.
int getRunLow() const
Lowest run number getter.
bool containsEvent(int experiment, int run, unsigned int event) const
Check whether the given event is in the covered range of events.
const std::string & getSteering() const
Steering file content getter.
bool isMC() const
Is it generated data?
unsigned int getNEvents() const
Number of events getter.
bool m_isMC
Is it generated or real data?
const std::string & getDate() const
File creation date and time getter (UTC)
unsigned int m_nFullEvents
Number of full events.
unsigned int m_mcEvents
Number of generated events, 0 for real data.
FileMetaData()
Constructor.
unsigned int getEventLow() const
Lowest event number in lowest run getter.
std::string m_lfn
Logical file name.
std::map< std::string, std::string > m_dataDescription
key-value store to describe the data.
int m_runLow
Lowest run number.
const std::string & getSite() const
Site where the file was created getter.
std::string m_release
Software release version.
std::string getJsonStr() const
Get a json representation.
std::string m_date
File creation date and time (UTC).
int getExperimentLow() const
Lowest experiment number getter.
static void exposePythonAPI()
Exposes methods of the FileMetaData class to Python.
const std::map< std::string, std::string > & getDataDescription() const
get data description.
bool write(std::ostream &output, const std::string &physicalFileName) const
Write file meta data in xml format to the output stream.
void setLfn(const std::string &lfn)
Setter for LFN.
unsigned int m_eventLow
Lowest event number in lowest run.
const std::string & getUser() const
User who created the file getter.
unsigned int getNFullEvents() const
Number of full events getter.
int m_experimentLow
Lowest experiment number.
const std::string & getRandomSeed() const
Random seed getter.
std::string m_user
User who created the file.
std::string m_site
Site where the file was created.
int getExperimentHigh() const
Highest experiment number getter.
unsigned int getMcEvents() const
Number of generated events getter.
int m_experimentHigh
Highest experiment number.
std::vector< std::string > m_parentLfns
LFNs of parent files.
int m_runHigh
Highest run number.
const std::string & getParent(int iParent) const
Get LFN of parent file.
bool read(std::istream &input, std::string &physicalFileName)
Read file meta data in xml format from the input stream.
create human-readable or JSON output for key value pairs.
void put(const std::string &key, const T &value)
Add one key-value pair.
std::string string() const
Return completed string.
std::string escape(const std::string &str)
Convert &, <, > etc.
Definition HTML.cc:159
std::string unescape(const std::string &str)
inverse of escape()
Definition HTML.cc:169
Abstract base class for different kinds of events.
STL namespace.