Belle II Software  release-06-00-14
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 
22 using namespace std;
23 using namespace Belle2;
24 using namespace boost::python;
25 
26 FileMetaData::FileMetaData() :
27  m_lfn(""), m_nEvents(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 
33 bool 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_experiment_low", &FileMetaData::getExperimentLow)
55  .def("get_run_low", &FileMetaData::getRunLow)
56  .def("get_event_low", &FileMetaData::getEventLow)
57  .def("get_experiment_high", &FileMetaData::getExperimentHigh)
58  .def("get_run_high", &FileMetaData::getRunHigh)
59  .def("get_event_high", &FileMetaData::getEventHigh)
60  .def("get_n_parents", &FileMetaData::getNParents)
61  .def("get_parent", &FileMetaData::getParent, return_value_policy<copy_const_reference>())
62  .def("get_date", &FileMetaData::getDate, return_value_policy<copy_const_reference>())
63  .def("get_site", &FileMetaData::getSite, return_value_policy<copy_const_reference>())
64  .def("get_user", &FileMetaData::getUser, return_value_policy<copy_const_reference>())
65  .def("get_random_seed", &FileMetaData::getRandomSeed, return_value_policy<copy_const_reference>())
66  .def("get_release", &FileMetaData::getRelease, return_value_policy<copy_const_reference>())
67  .def("get_steering", &FileMetaData::getSteering, return_value_policy<copy_const_reference>())
68  .def("is_mc", &FileMetaData::isMC)
69  .def("get_mc_events", &FileMetaData::getMcEvents)
70  .def("get_global_tag", &FileMetaData::getDatabaseGlobalTag, return_value_policy<copy_const_reference>())
71  .def("get_data_description", &FileMetaData::getDataDescription, return_value_policy<copy_const_reference>())
72  .def("set_lfn", &FileMetaData::setLfn);
73 }
74 
75 
76 void FileMetaData::Print(Option_t* option) const
77 {
78  if (option && (option == std::string("steering"))) {
79  cout << m_steering << endl;
80  return;
81  } else if (option && (option == std::string("json"))) {
82  cout << getJsonStr() << endl;
83  return;
84  }
85  const bool all = (option && option == std::string("all"));
86  KeyValuePrinter printer(false);
87  printer.put("LFN", m_lfn);
88  printer.put("nEvents", m_nEvents);
89  printer.put("range", std::to_string(m_experimentLow) + "/" + std::to_string(m_runLow) + "/" + std::to_string(m_eventLow)
90  + " - " + std::to_string(m_experimentHigh) + "/" + std::to_string(m_runHigh) + "/" + std::to_string(m_eventHigh));
91  printer.put("parents", m_parentLfns);
92  if (all) {
93  printer.put("date", m_date);
94  printer.put("site", m_site);
95  printer.put("user", m_user);
96  printer.put("randomSeed", m_randomSeed);
97  printer.put("release", m_release);
98  printer.put("isMC", m_isMC);
99  printer.put("mcEvents", m_mcEvents);
100  printer.put("globalTag", m_databaseGlobalTag);
101  printer.put("dataDescription", m_dataDescription);
102  }
103  std::cout << "=== FileMetaData ===\n";
104  std::cout << printer.string();
105 }
106 
107 bool FileMetaData::read(std::istream& input, std::string& physicalFileName)
108 {
109  physicalFileName = "";
110  if (input.eof()) return false;
111 
112  std::string line;
113  std::getline(input, line);
114  boost::algorithm::trim(line);
115  if (line.compare("<File>") != 0) return false;
116 
117  while (!input.eof()) {
118  std::getline(input, line);
119  boost::algorithm::trim(line);
120  if (line.compare("</File>") == 0) return true;
121 
122  int pos = line.find('>') + 1;
123  std::string tag = line.substr(0, pos);
124  std::string value = line.substr(pos, line.rfind('<') - pos);
125  if (tag.compare("<LFN>") == 0) {
126  m_lfn = HTML::unescape(value);
127  } else if (tag.compare("<PFN>") == 0) {
128  physicalFileName = HTML::unescape(value);
129  } else if (tag.compare("<ExperimentLow>") == 0) {
130  m_experimentLow = stoi(value);
131  } else if (tag.compare("<RunLow>") == 0) {
132  m_runLow = stoi(value);
133  } else if (tag.compare("<EventLow>") == 0) {
134  m_eventLow = stoi(value);
135  } else if (tag.compare("<ExperimentHigh>") == 0) {
136  m_experimentHigh = stoi(value);
137  } else if (tag.compare("<RunHigh>") == 0) {
138  m_runHigh = stoi(value);
139  } else if (tag.compare("<EventHigh>") == 0) {
140  m_eventHigh = stoi(value);
141  } else if (tag.compare("<Parents>") == 0) {
142  pos = value.find(',');
143  while (pos > 0) {
144  m_parentLfns.push_back(HTML::unescape(value.substr(0, pos)));
145  value.erase(0, pos + 1);
146  pos = value.find(',');
147  }
148  m_parentLfns.push_back(HTML::unescape(value));
149  }
150  }
151 
152  return false;
153 }
154 
155 bool FileMetaData::write(std::ostream& output, const std::string& physicalFileName) const
156 {
157  output << " <File>\n";
158  output << " <LFN>" << HTML::escape(m_lfn) << "</LFN>\n";
159  if (!physicalFileName.empty()) {
160  output << " <PFN>" << HTML::escape(physicalFileName) << "</PFN>\n";
161  }
162  output << " <ExperimentLow>" << m_experimentLow << "</ExperimentLow>\n";
163  output << " <RunLow>" << m_runLow << "</RunLow>\n";
164  output << " <EventLow>" << m_eventLow << "</EventLow>\n";
165  output << " <ExperimentHigh>" << m_experimentHigh << "</ExperimentHigh>\n";
166  output << " <RunHigh>" << m_runHigh << "</RunHigh>\n";
167  output << " <EventHigh>" << m_eventHigh << "</EventHigh>\n";
168  if (!m_parentLfns.empty()) {
169  output << " <Parents>" << HTML::escape(m_parentLfns[0]);
170  for (unsigned int parent = 1; parent < m_parentLfns.size(); parent++) {
171  output << "," << HTML::escape(m_parentLfns[parent]);
172  }
173  output << "</Parents>\n";
174  }
175  output << " </File>\n";
176 
177  return true;
178 }
179 
180 std::string FileMetaData::getJsonStr() const
181 {
182  nlohmann::json metadata = {
183  {"LFN", m_lfn},
184  {"nEvents", m_nEvents},
185  {"experimentLow", m_experimentLow},
186  {"runLow", m_runLow},
187  {"eventLow", m_eventLow},
188  {"experimentHigh", m_experimentHigh},
189  {"runHigh", m_runHigh},
190  {"eventHigh", m_eventHigh},
191  {"parents", m_parentLfns},
192  {"date", m_date},
193  {"site", m_site},
194  {"user", m_user},
195  {"randomSeed", m_randomSeed},
196  {"release", m_release},
197  {"isMC", m_isMC},
198  {"mcEvents", m_mcEvents},
199  {"globalTag", m_databaseGlobalTag},
200  {"dataDescription", m_dataDescription},
201  {"steering", m_steering}
202  };
203  return metadata.dump(2);
204 }
virtual void Print(Option_t *option="") const override
Print the content of the meta data object.
Definition: FileMetaData.cc:76
std::string m_steering
The steering file content.
Definition: FileMetaData.h:270
unsigned int getEventHigh() const
Highest event number in highest run getter.
Definition: FileMetaData.h:65
unsigned int m_eventHigh
Highest event number in highest run.
Definition: FileMetaData.h:256
const std::string & getRandomSeed() const
Random seed getter.
Definition: FileMetaData.h:103
const std::string & getUser() const
User who created the file getter.
Definition: FileMetaData.h:99
int getRunHigh() const
Highest run number getter.
Definition: FileMetaData.h:61
int getNParents() const
Get number of parent files.
Definition: FileMetaData.h:81
std::string m_randomSeed
The random seed used when producing the file.
Definition: FileMetaData.h:266
std::string m_databaseGlobalTag
Global tag in the database used for production of this file.
Definition: FileMetaData.h:276
unsigned int m_nEvents
Number of events.
Definition: FileMetaData.h:244
int getRunLow() const
Lowest run number getter.
Definition: FileMetaData.h:49
const std::string & getSteering() const
Steering file content getter.
Definition: FileMetaData.h:111
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
bool isMC() const
Is it generated data?.
Definition: FileMetaData.h:115
unsigned int getNEvents() const
Number of events getter.
Definition: FileMetaData.h:41
const std::map< std::string, std::string > & getDataDescription() const
get data description.
Definition: FileMetaData.h:129
const std::string & getDate() const
File creation date and time getter (UTC)
Definition: FileMetaData.h:91
bool m_isMC
Is it generated or real data?.
Definition: FileMetaData.h:272
unsigned int m_mcEvents
Number of generated events, 0 for real data.
Definition: FileMetaData.h:274
unsigned int getEventLow() const
Lowest event number in lowest run getter.
Definition: FileMetaData.h:53
std::string m_lfn
Logical file name.
Definition: FileMetaData.h:242
std::map< std::string, std::string > m_dataDescription
key-value store to describe the data.
Definition: FileMetaData.h:278
int m_runLow
Lowest run number.
Definition: FileMetaData.h:248
const std::string & getDatabaseGlobalTag() const
Get the database global tag used when creating this file.
Definition: FileMetaData.h:126
std::string m_release
Software release version.
Definition: FileMetaData.h:268
std::string getJsonStr() const
Get a json representation.
std::string m_date
File creation date and time (UTC).
Definition: FileMetaData.h:260
int getExperimentLow() const
Lowest experiment number getter.
Definition: FileMetaData.h:45
static void exposePythonAPI()
Exposes methods of the FileMetaData class to Python.
Definition: FileMetaData.cc:48
bool write(std::ostream &output, const std::string &physicalFileName) const
Write file meta data in xml format to the output stream.
const std::string & getLfn() const
Logical file name getter.
Definition: FileMetaData.h:37
const std::string & getRelease() const
Software release version getter.
Definition: FileMetaData.h:107
void setLfn(const std::string &lfn)
Setter for LFN.
Definition: FileMetaData.h:135
unsigned int m_eventLow
Lowest event number in lowest run.
Definition: FileMetaData.h:250
int m_experimentLow
Lowest experiment number.
Definition: FileMetaData.h:246
std::string m_user
User who created the file.
Definition: FileMetaData.h:264
std::string m_site
Site where the file was created.
Definition: FileMetaData.h:262
int getExperimentHigh() const
Highest experiment number getter.
Definition: FileMetaData.h:57
unsigned int getMcEvents() const
Number of generated events getter.
Definition: FileMetaData.h:119
int m_experimentHigh
Highest experiment number.
Definition: FileMetaData.h:252
std::vector< std::string > m_parentLfns
LFNs of parent files.
Definition: FileMetaData.h:258
const std::string & getParent(int iParent) const
Get LFN of parent file.
Definition: FileMetaData.h:88
const std::string & getSite() const
Site where the file was created getter.
Definition: FileMetaData.h:95
int m_runHigh
Highest run number.
Definition: FileMetaData.h:254
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:160
std::string unescape(const std::string &str)
inverse of escape()
Definition: HTML.cc:170
Abstract base class for different kinds of events.