Belle II Software  release-06-02-00
b2file-merge.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 #include <framework/dataobjects/FileMetaData.h>
9 #include <framework/io/RootIOUtilities.h>
10 #include <framework/io/RootFileInfo.h>
11 #include <framework/logging/Logger.h>
12 #include <framework/pcore/Mergeable.h>
13 #include <framework/core/FileCatalog.h>
14 #include <framework/utilities/KeyValuePrinter.h>
15 
16 #include <boost/program_options.hpp>
17 #include <boost/filesystem.hpp>
18 #include <boost/algorithm/string.hpp>
19 
20 #include <TFile.h>
21 #include <TTree.h>
22 #include <TBranchElement.h>
23 
24 #include <iostream>
25 #include <iomanip>
26 #include <string>
27 #include <set>
28 #include <regex>
29 
30 using namespace Belle2;
31 namespace po = boost::program_options;
32 namespace fs = boost::filesystem;
33 
36 using EventInfo = std::tuple<int, int, unsigned int>;
37 
38 namespace {
41  std::string removeLegacyGt(const std::string& globaltags)
42  {
43  std::regex legacy_gt(",?Legacy_IP_Information");
44  return std::regex_replace(globaltags, legacy_gt, "");
45  }
46 }
47 
48 int main(int argc, char* argv[])
49 {
50  // Parse options
51  std::string outputfilename;
52  std::vector<std::string> inputfilenames;
53  po::options_description options("Options");
54  options.add_options()
55  ("help,h", "print all available options")
56  ("output,o", po::value<std::string>(&outputfilename), "output file name")
57  ("file", po::value<std::vector<std::string>>(&inputfilenames), "filename to merge")
58  ("force,f", "overwrite existing file")
59  ("no-catalog", "don't register output file in file catalog, This is now the default")
60  ("add-to-catalog", "register the output file in the file catalog")
61  ("quiet,q", "if given don't print infos, just warnings and errors");
62  po::positional_options_description positional;
63  positional.add("output", 1);
64  positional.add("file", -1);
65  po::variables_map variables;
66  po::store(po::command_line_parser(argc, argv).options(options).positional(positional).run(), variables);
67  po::notify(variables);
68  if (variables.count("help") || variables.count("output") == 0 || inputfilenames.empty()) {
69  std::cout << "Usage: " << argv[0] << " [<options>] OUTPUTFILE INPUTFILE [INPUTFILE...]" << std::endl;
70  std::cout << " " << argv[0] << " [<options>] [--file INPUTFILE...] "
71  << "-o OUTPUTFILE [--file INPUTFILE...]" << std::endl << std::endl;
72  std::cout << options << std::endl;
73  std::cout << (R"DOC(
74 This program is intended to merge files created by separate basf2 jobs. It's
75 similar to hadd but does correctly update the metadata in the file and merges
76 the objects in the persistent tree correctly.
77 
78 The following restrictions apply:
79  - The files have to be created with the same release and steering file
80  - The persistent tree is only allowed to contain FileMetaData and objects
81  inheriting from Mergeable and the same list of objects needs to be present
82  in all files.
83  - The event tree needs to contain the same DataStore entries in all files.
84 )DOC");
85  return 1;
86  }
87 
88  // Remove the {module:} from log messages
89  auto logConfig = LogSystem::Instance().getLogConfig();
92  }
93  if(variables.count("quiet")>0){
94  logConfig->setLogLevel(LogConfig::c_Warning);
95  }
96 
97  B2INFO("Merging files into " << std::quoted(outputfilename));
98  // check output file
99  if (fs::exists(outputfilename) && variables.count("force")==0) {
100  B2ERROR("Output file exists, use -f to force overwriting it");
101  return 1;
102  }
103  // First we check all input files for consistency ...
104 
105  // the final metadata we will write out
106  FileMetaData* outputMetaData{nullptr};
107  // set of all parent LFNs encountered in any file
108  std::set<std::string> allParents;
109  // map of all mergeable objects found in the persistent tree. The size_t is
110  // for counting to make sure we see all objects in all files
111  std::map<std::string, std::pair<Mergeable*, size_t>> persistentMergeables;
112  // set of all random seeds to print warning on duplicates
113  std::set<std::string> allSeeds;
114  // set of all users
115  std::set<std::string> allUsers;
116  // EventInfo for the high/low event numbers of the final FileMetaData
117  std::optional<EventInfo> lowEvt, highEvt;
118  // set of all branch names in the event tree to compare against to make sure
119  // that they're the same in all files
120  std::set<std::string> allEventBranches;
121  // Release version to compare against. Same as FileMetaData::getRelease() but with the optional -modified removed
122  std::string outputRelease;
123 
124  // so let's loop over all files and create FileMetaData and merge persistent
125  // objects if they inherit from Mergeable, bail if there's something else in
126  // there. The idea is that merging the persistent stuff is fast so we catch
127  // errors more quickly when we do this as a first step and events later on.
128  for (const auto& input : inputfilenames) {
129  try {
130  RootIOUtilities::RootFileInfo fileInfo(input);
131  // Ok, load the FileMetaData from the tree
132  const auto &fileMetaData = fileInfo.getFileMetaData();
133  // File looks usable, start checking metadata ...
134  B2INFO("adding file " << std::quoted(input));
135  if(LogSystem::Instance().isLevelEnabled(LogConfig::c_Info)) fileMetaData.Print("all");
136 
137  auto branches = fileInfo.getBranchNames();
138  if(branches.empty()) {
139  throw std::runtime_error("Could not find any branches in event tree");
140  }
141  if(allEventBranches.empty()) {
142  std::swap(allEventBranches,branches);
143  }else{
144  if(branches!=allEventBranches){
145  B2ERROR("Branches in " << std::quoted(input) << " differ from "
146  << std::quoted(inputfilenames.front()));
147  }
148  }
149 
150  // File looks good so far, now fix the persistent stuff, i.e. merge all
151  // objects in persistent tree
152  for(TObject* brObj: *fileInfo.getPersistentTree().GetListOfBranches()){
153  auto* br = dynamic_cast<TBranchElement*>(brObj);
154  // FileMetaData is handled separately
155  if(br && br->GetTargetClass() == FileMetaData::Class() && std::string(br->GetName()) == "FileMetaData")
156  continue;
157  // Make sure the branch is mergeable
158  if(!br || !br->GetTargetClass()->InheritsFrom(Mergeable::Class())){
159  B2ERROR("Branch " << std::quoted(br->GetName()) << " in persistent tree not inheriting from Mergable");
160  continue;
161  }
162  // Ok, it's an object we now how to handle so get it from the tree
163  Mergeable* object{nullptr};
164  br->SetAddress(&object);
165  if(br->GetEntry(0)<=0) {
166  B2ERROR("Could not read branch " << std::quoted(br->GetName()) << " of entry 0 from persistent tree in "
167  << std::quoted(input));
168  continue;
169  }
170  // and either insert it into the map of mergeables or merge with the existing one
171  auto it = persistentMergeables.insert(std::make_pair(br->GetName(), std::make_pair(object, 1)));
172  if(!it.second) {
173  try {
174  it.first->second.first->merge(object);
175  }catch(std::exception &e){
176  B2FATAL("Cannot merge " << std::quoted(br->GetName()) << " in " << std::quoted(input) << ": " << e.what());
177  }
178  it.first->second.second++;
179  // ok, merged, get rid of it.
180  delete object;
181  }else{
182  B2INFO("Found mergeable object " << std::quoted(br->GetName()) << " in persistent tree");
183  }
184  }
185 
186  std::string release = fileMetaData.getRelease();
187  if(release == "") {
188  B2ERROR("Cannot determine release used to create " << std::quoted(input));
189  continue;
190  }else if(boost::algorithm::ends_with(fileMetaData.getRelease(), "-modified")){
191  B2WARNING("File " << std::quoted(input) << " created with modified software "
192  << fileMetaData.getRelease()
193  << ": cannot verify that files are compatible");
194  release = release.substr(0, release.size() - std::string("-modified").size());
195  }
196 
197  // so, event tree looks good too. Now we merge the FileMetaData
198  if (!outputMetaData) {
199  // first input file, just take the event metadata
200  outputMetaData = new FileMetaData(fileMetaData);
201  outputRelease = release;
202  } else {
203  // check meta data for consistency, we could move this into FileMetaData...
204  if(release != outputRelease) {
205  B2ERROR("Release in " << std::quoted(input) << " differs from previous files: " <<
206  fileMetaData.getRelease() << " != " << outputMetaData->getRelease());
207  }
208  if(fileMetaData.getSteering() != outputMetaData->getSteering()){
209  // printing both steering files is not useful for anyone so just throw an error
210  B2ERROR("Steering file for " << std::quoted(input) << " differs from previous files.");
211  }
212  if(fileMetaData.getDatabaseGlobalTag() != outputMetaData->getDatabaseGlobalTag()){
213  // Related to BII-6093: we were adding the legacy gt only dependent on input file age, not creation release.
214  // This means there is a chance we want to merge files with and without the globaltag added if they cross the
215  // boundary. It doesn't hurt to keep the gt but we know we could process some of the files without it so as a remedy we
216  // check if the only difference is the legacy gt and if so we remove it from the output metadata ...
217  if(removeLegacyGt(fileMetaData.getDatabaseGlobalTag()) == removeLegacyGt(outputMetaData->getDatabaseGlobalTag())) {
218  outputMetaData->setDatabaseGlobalTag(removeLegacyGt(outputMetaData->getDatabaseGlobalTag()));
219  } else {
220  B2ERROR("Database globalTag in " << std::quoted(input) << " differs from previous files: " <<
221  fileMetaData.getDatabaseGlobalTag() << " != " << outputMetaData->getDatabaseGlobalTag());
222  }
223  }
224  if(fileMetaData.getDataDescription() != outputMetaData->getDataDescription()){
225  KeyValuePrinter cur(true);
226  for (const auto& descrPair : outputMetaData->getDataDescription())
227  cur.put(descrPair.first, descrPair.second);
228  KeyValuePrinter prev(true);
229  for (const auto& descrPair : fileMetaData.getDataDescription())
230  prev.put(descrPair.first, descrPair.second);
231 
232  B2ERROR("dataDescription in " << std::quoted(input) << " differs from previous files:\n" << cur.string() << " vs.\n" << prev.string());
233  }
234  if(fileMetaData.isMC() != outputMetaData->isMC()){
235  B2ERROR("Type (real/MC) for " << std::quoted(input) << " differs from previous files.");
236  }
237  // update event numbers ...
238  outputMetaData->setMcEvents(outputMetaData->getMcEvents() + fileMetaData.getMcEvents());
239  outputMetaData->setNEvents(outputMetaData->getNEvents() + fileMetaData.getNEvents());
240  }
241  if(fileMetaData.getNEvents() < 1) {
242  B2WARNING("File " << std::quoted(input) << " is empty.");
243  } else {
244  // make sure we have the correct low/high event numbers
245  EventInfo curLowEvt = EventInfo{fileMetaData.getExperimentLow(), fileMetaData.getRunLow(), fileMetaData.getEventLow()};
246  EventInfo curHighEvt = EventInfo{fileMetaData.getExperimentHigh(), fileMetaData.getRunHigh(), fileMetaData.getEventHigh()};
247  if(!lowEvt or curLowEvt < *lowEvt) lowEvt = curLowEvt;
248  if(!highEvt or curHighEvt > *highEvt) highEvt = curHighEvt;
249  }
250  // check if we have seen this random seed already in one of the previous files
251  auto it = allSeeds.insert(fileMetaData.getRandomSeed());
252  if(!it.second) {
253  B2WARNING("Duplicate Random Seed: " << std::quoted(fileMetaData.getRandomSeed()) << " present in more then one file");
254  }
255  allUsers.insert(fileMetaData.getUser());
256  // remember all parent files we encounter
257  for (int i = 0; i < fileMetaData.getNParents(); ++i) {
258  allParents.insert(fileMetaData.getParent(i));
259  }
260  }catch(std::exception &e) {
261  B2ERROR("input file " << std::quoted(input) << ": " << e.what());
262  }
263  }
264 
265  //Check if the same mergeables were found in all files
266  for(const auto &val: persistentMergeables){
267  if(val.second.second != inputfilenames.size()){
268  B2ERROR("Mergeable " << std::quoted(val.first) << " only present in " << val.second.second << " out of "
269  << inputfilenames.size() << " files");
270  }
271  }
272 
273  // Check for user names
274  if(allUsers.size()>1) {
275  B2WARNING("Multiple different users created input files: " << boost::algorithm::join(allUsers, ", "));
276  }
277 
278  // Stop processing in case of error
279  if (LogSystem::Instance().getMessageCounter(LogConfig::c_Error) > 0) return 1;
280 
281  if(!outputMetaData){
282  // technically it's rather impossible to arrive here: if there were no
283  // input files we exit with a usage message and if any of the files could
284  // not be processed then the error count should be >0. Nevertheless
285  // let's do this check to be on the very safe side and to make clang
286  // analyzer happy.
287  B2FATAL("For some reason no files could be processed");
288  return 1;
289  }
290  if(!lowEvt) {
291  B2WARNING("All Files were empty");
292  lowEvt = EventInfo{-1, -1, 0};
293  highEvt = EventInfo{-1, -1, 0};
294  }
295 
296  // Final changes to metadata
297  outputMetaData->setLfn("");
298  outputMetaData->setParents(std::vector<std::string>(allParents.begin(), allParents.end()));
299  outputMetaData->setLow(std::get<0>(*lowEvt), std::get<1>(*lowEvt), std::get<2>(*lowEvt));
300  outputMetaData->setHigh(std::get<0>(*highEvt), std::get<1>(*highEvt), std::get<2>(*highEvt));
301  // If more then one file set an empty random seed
302  if(inputfilenames.size()>1){
303  outputMetaData->setRandomSeed("");
304  }
305  RootIOUtilities::setCreationData(*outputMetaData);
306 
307  // OK we have a valid FileMetaData and merged all persistent objects, now do
308  // the conversion of the event trees and create the output file.
309  TFile output(outputfilename.c_str(), "RECREATE");
310  if (output.IsZombie()) {
311  B2ERROR("Could not create output file " << std::quoted(outputfilename));
312  return 1;
313  }
314 
315  TTree* outputEventTree{nullptr};
316  for (const auto& input : inputfilenames) {
317  B2INFO("processing events from " << std::quoted(input));
318  TFile tfile(input.c_str());
319  auto* tree = dynamic_cast<TTree*>(tfile.Get("tree"));
320  if(!outputEventTree){
321  output.cd();
322  outputEventTree = tree->CloneTree(0);
323  }else{
324  outputEventTree->CopyAddresses(tree);
325  }
326  // Now let's copy all entries without unpacking (fast), layout the
327  // baskets in an optimal order for sequential reading (SortBasketByEntry)
328  // and rebuild the index in case some parts of the index are missing
329  outputEventTree->CopyEntries(tree, -1, "fast SortBasketsByEntry BuildIndexOnError");
330  // and reset the branch addresses to not be connected anymore
331  outputEventTree->CopyAddresses(tree, true);
332  // finally clean up and close file.
333  delete tree;
334  tfile.Close();
335  }
336  // make sure we have an index ...
337  if(!outputEventTree->GetTreeIndex()) {
338  B2INFO("No Index found: building new index");
339  RootIOUtilities::buildIndex(outputEventTree);
340  }
341  // and finally write the tree
342  output.cd();
343  outputEventTree->Write();
344  B2INFO("Done processing events");
345 
346  // we need to set the LFN to the absolute path name
347  outputMetaData->setLfn(fs::absolute(outputfilename, fs::initial_path()).string());
348  // and maybe register it in the file catalog
349  if(variables.count("add-to-catalog")>0) {
350  FileCatalog::Instance().registerFile(outputfilename, *outputMetaData);
351  }
352  B2INFO("Writing FileMetaData");
353  // Create persistent tree
354  output.cd();
355  TTree outputMetaDataTree("persistent", "persistent");
356  outputMetaDataTree.Branch("FileMetaData", &outputMetaData);
357  for(auto &it: persistentMergeables){
358  outputMetaDataTree.Branch(it.first.c_str(), &it.second.first);
359  }
360  outputMetaDataTree.Fill();
361  outputMetaDataTree.Write();
362 
363  // now clean up the mess ...
364  for(const auto& val: persistentMergeables){
365  delete val.second.first;
366  }
367  persistentMergeables.clear();
368  delete outputMetaData;
369  output.Close();
370 }
static FileCatalog & Instance()
Static method to get a reference to the FileCatalog instance.
Definition: FileCatalog.cc:23
virtual bool registerFile(const std::string &fileName, FileMetaData &metaData, const std::string &oldLFN="")
Register a file in the (local) file catalog.
Definition: FileCatalog.cc:90
Metadata information about a file.
Definition: FileMetaData.h:29
create human-readable or JSON output for key value pairs.
@ c_Error
Error: for things that went wrong and have to be fixed.
Definition: LogConfig.h:30
@ c_Info
Info: for informational messages, e.g.
Definition: LogConfig.h:27
@ c_Fatal
Fatal: for situations were the program execution can not be continued.
Definition: LogConfig.h:31
@ c_Warning
Warning: for potential problems that the user should pay attention to.
Definition: LogConfig.h:29
@ c_Level
Log level of the message.
Definition: LogConfig.h:36
@ c_Message
Log message text.
Definition: LogConfig.h:37
void setLogInfo(ELogLevel logLevel, unsigned int logInfo)
Configure the printed log information for the given level.
Definition: LogConfig.h:127
LogConfig * getLogConfig()
Returns global log system configuration.
Definition: LogSystem.h:78
static LogSystem & Instance()
Static method to get a reference to the LogSystem instance.
Definition: LogSystem.cc:31
Abstract base class for objects that can be merged.
Definition: Mergeable.h:31
Helper class to factorize some necessary tasks when working with Belle2 output files.
Definition: RootFileInfo.h:26
void setCreationData(FileMetaData &metadata)
Fill the creation info of a file meta data: site, user, data.
void buildIndex(TTree *tree)
Build TTreeIndex on tree (assumes EventMetaData branch exists there).
Abstract base class for different kinds of events.
int main(int argc, char **argv)
Run all tests.
Definition: test_main.cc:75