Belle II Software development
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
27 m_lfn(""), m_nEvents(0), m_nFullEvents(0), m_experimentLow(0), m_runLow(0), m_eventLow(0),
28 m_experimentHigh(0), m_runHigh(0), m_eventHigh(0), m_date(""), m_site(""), m_user(""), m_release(""),
29 m_steering(""), m_isMC(true), m_mcEvents(0)
30{
31}
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.
Definition: FileMetaData.cc:77
const std::string & getDatabaseGlobalTag() const
Get the database global tag used when creating this file.
Definition: FileMetaData.h:130
std::string m_steering
The steering file content.
Definition: FileMetaData.h:282
unsigned int getEventHigh() const
Highest event number in highest run getter.
Definition: FileMetaData.h:69
unsigned int m_eventHigh
Highest event number in highest run.
Definition: FileMetaData.h:268
int getRunHigh() const
Highest run number getter.
Definition: FileMetaData.h:65
const std::string & getLfn() const
Logical file name getter.
Definition: FileMetaData.h:37
int getNParents() const
Get number of parent files.
Definition: FileMetaData.h:85
std::string m_randomSeed
The random seed used when producing the file.
Definition: FileMetaData.h:278
std::string m_databaseGlobalTag
Global tag in the database used for production of this file.
Definition: FileMetaData.h:288
unsigned int m_nEvents
Number of events.
Definition: FileMetaData.h:254
const std::string & getRelease() const
Software release version getter.
Definition: FileMetaData.h:111
int getRunLow() const
Lowest run number getter.
Definition: FileMetaData.h:53
bool containsEvent(int experiment, int run, unsigned int event) const
Check whether the given event is in the covered range of events.
Definition: FileMetaData.cc:33
const std::string & getSteering() const
Steering file content getter.
Definition: FileMetaData.h:115
bool isMC() const
Is it generated data?.
Definition: FileMetaData.h:119
unsigned int getNEvents() const
Number of events getter.
Definition: FileMetaData.h:41
bool m_isMC
Is it generated or real data?.
Definition: FileMetaData.h:284
const std::string & getDate() const
File creation date and time getter (UTC)
Definition: FileMetaData.h:95
unsigned int m_nFullEvents
Number of full events.
Definition: FileMetaData.h:256
unsigned int m_mcEvents
Number of generated events, 0 for real data.
Definition: FileMetaData.h:286
FileMetaData()
Constructor.
Definition: FileMetaData.cc:26
unsigned int getEventLow() const
Lowest event number in lowest run getter.
Definition: FileMetaData.h:57
std::string m_lfn
Logical file name.
Definition: FileMetaData.h:252
std::map< std::string, std::string > m_dataDescription
key-value store to describe the data.
Definition: FileMetaData.h:290
int m_runLow
Lowest run number.
Definition: FileMetaData.h:260
const std::string & getSite() const
Site where the file was created getter.
Definition: FileMetaData.h:99
std::string m_release
Software release version.
Definition: FileMetaData.h:280
std::string getJsonStr() const
Get a json representation.
std::string m_date
File creation date and time (UTC).
Definition: FileMetaData.h:272
int getExperimentLow() const
Lowest experiment number getter.
Definition: FileMetaData.h:49
static void exposePythonAPI()
Exposes methods of the FileMetaData class to Python.
Definition: FileMetaData.cc:48
const std::map< std::string, std::string > & getDataDescription() const
get data description.
Definition: FileMetaData.h:133
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.
Definition: FileMetaData.h:139
unsigned int m_eventLow
Lowest event number in lowest run.
Definition: FileMetaData.h:262
const std::string & getUser() const
User who created the file getter.
Definition: FileMetaData.h:103
unsigned int getNFullEvents() const
Number of full events getter.
Definition: FileMetaData.h:45
int m_experimentLow
Lowest experiment number.
Definition: FileMetaData.h:258
const std::string & getRandomSeed() const
Random seed getter.
Definition: FileMetaData.h:107
std::string m_user
User who created the file.
Definition: FileMetaData.h:276
std::string m_site
Site where the file was created.
Definition: FileMetaData.h:274
int getExperimentHigh() const
Highest experiment number getter.
Definition: FileMetaData.h:61
unsigned int getMcEvents() const
Number of generated events getter.
Definition: FileMetaData.h:123
int m_experimentHigh
Highest experiment number.
Definition: FileMetaData.h:264
std::vector< std::string > m_parentLfns
LFNs of parent files.
Definition: FileMetaData.h:270
int m_runHigh
Highest run number.
Definition: FileMetaData.h:266
const std::string & getParent(int iParent) const
Get LFN of parent file.
Definition: FileMetaData.h:92
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.