Belle II Software  release-05-01-25
PyBasf2.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Andreas Moll, Thomas Kuhr *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <boost/python.hpp>
12 #include <framework/utilities/RegisterPythonModule.h>
13 
14 #include <framework/pybasf2/Framework.h>
15 #include <framework/pybasf2/LogPythonInterface.h>
16 #include <framework/pybasf2/ProcessStatisticsPython.h>
17 #include <framework/core/Module.h>
18 #include <framework/core/Path.h>
19 #include <framework/core/PyObjROOTUtils.h>
20 #include <framework/core/RandomNumbers.h>
21 #include <framework/core/ModuleParamInfoPython.h>
22 #include <framework/core/FileCatalog.h>
23 #include <framework/dataobjects/FileMetaData.h>
24 #include <framework/database/Database.h>
25 #include <framework/io/RootFileInfo.h>
26 
27 #include <TFile.h>
28 #include <TTree.h>
29 
30 using namespace Belle2;
31 using namespace boost::python;
32 
33 
34 FileMetaData updateFileMetaData(const std::string& fileName, const std::string& lfn)
35 {
36  // open the root file
37  TFile* file = TFile::Open(fileName.c_str(), "UPDATE");
38  if (!file || !file->IsOpen()) {
39  B2ERROR("Failed to open the file " << fileName);
40  return FileMetaData();
41  }
42 
43  // read the FileMetaData object or create a new one if it doesn't exist
44  FileMetaData* fileMetaData = nullptr;
45  auto* tree = dynamic_cast<TTree*>(file->Get("persistent"));
46  TTree* newTree = nullptr;
47  if (!tree) {
48  fileMetaData = dynamic_cast<FileMetaData*>(file->Get("FileMetaData"));
49  if (!fileMetaData) {
50  B2WARNING("Failed to get persistent tree in the file " << fileName);
51  tree = new TTree("persistent", "persistent");
52  fileMetaData = new FileMetaData;
53  tree->Branch("FileMetaData", &fileMetaData);
54  newTree = tree;
55  }
56  } else {
57  tree->SetBranchAddress("FileMetaData", &fileMetaData);
58  newTree = tree->CloneTree(0);
59  tree->GetEntry(0);
60  }
61 
62  // update the IDs and write the updated FileMetaData to the file
63  const std::string oldLFN = fileMetaData->getLfn();
64  fileMetaData->setLfn(lfn);
65  if (newTree) {
66  newTree->Fill();
67  newTree->Write();
68  } else {
69  fileMetaData->Write("FileMetaData");
70  }
71 
72  // update the local file catalog but only *if* the file was already registered
73  std::string oldPFN = oldLFN;
74  FileMetaData localMetaData;
75  if (FileCatalog::Instance().getMetaData(oldPFN, localMetaData)) {
76  localMetaData = *fileMetaData;
77  FileCatalog::Instance().registerFile(fileName, localMetaData, oldLFN);
78  }
79  return *fileMetaData;
80 }
81 
82 object getFileMetadata(const std::string& filename)
83 {
84  RootIOUtilities::RootFileInfo fileInfo(filename);
85  return createROOTObjectPyCopy(fileInfo.getFileMetaData());
86 }
87 
88 //-----------------------------------
89 // Define the pybasf2 python module
90 //-----------------------------------
91 BOOST_PYTHON_MODULE(pybasf2)
92 {
103 
104  //don't show c++ signature in python doc to keep it simple
105  docstring_options options(true, true, false);
106  def("update_file_metadata", &updateFileMetaData);
107  def("get_file_metadata", &getFileMetadata, R"DOC(
108 Return the FileMetaData object for the given output file.
109 )DOC");
110 }
111 
112 //register the module during library load
113 REGISTER_PYTHON_MODULE(pybasf2)
Belle2::Database::exposePythonAPI
static void exposePythonAPI()
Exposes setGlobalTag function of the Database class to Python.
Definition: Database.cc:284
Belle2::Path::exposePythonAPI
static void exposePythonAPI()
Exposes methods of the Path class to Python.
Definition: Path.cc:194
Belle2::FileCatalog::Instance
static FileCatalog & Instance()
Static method to get a reference to the FileCatalog instance.
Definition: FileCatalog.cc:25
Belle2::createROOTObjectPyCopy
boost::python::object createROOTObjectPyCopy(const T &instance)
Create a python wrapped copy from a class instance which has a ROOT dictionary.
Definition: PyObjROOTUtils.h:36
Belle2::Framework::exposePythonAPI
static void exposePythonAPI()
Exposes methods of the Framework class to Python.
Definition: Framework.cc:271
Belle2::Module::exposePythonAPI
static void exposePythonAPI()
Exposes methods of the Module class to Python.
Definition: Module.cc:327
Belle2::FileMetaData
Metadata information about a file.
Definition: FileMetaData.h:39
Belle2::FileMetaData::setLfn
void setLfn(const std::string &lfn)
Setter for LFN.
Definition: FileMetaData.h:145
Belle2::ModuleParamInfoPython::exposePythonAPI
static void exposePythonAPI()
Exposes methods of the ModuleParam class to Python.
Definition: ModuleParamInfoPython.cc:19
Belle2::ModuleCondition::exposePythonAPI
static void exposePythonAPI()
Exposes methods of the ModuleCondition class to Python.
Definition: ModuleCondition.cc:86
Belle2::RootIOUtilities::RootFileInfo
Helper class to factorize some necessary tasks when working with Belle2 output files.
Definition: RootFileInfo.h:28
Belle2::RandomNumbers::exposePythonAPI
static void exposePythonAPI()
Exposes methods of the RandomNumbers class to Python.
Definition: RandomNumbers.cc:152
Belle2::LogPythonInterface::exposePythonAPI
static void exposePythonAPI()
expose python API
Definition: LogPythonInterface.cc:190
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::ProcessStatisticsPython::exposePythonAPI
static void exposePythonAPI()
Define python wrappers to make functionality avaiable in python.
Definition: ProcessStatisticsPython.cc:113
Belle2::FileCatalog::registerFile
virtual bool registerFile(const std::string &fileName, FileMetaData &metaData, const std::string &oldLFN="")
Register a file in the (local) file catalog.
Definition: FileCatalog.cc:92
Belle2::FileMetaData::exposePythonAPI
static void exposePythonAPI()
Exposes methods of the FileMetaData class to Python.
Definition: FileMetaData.cc:50
Belle2::FileMetaData::getLfn
const std::string & getLfn() const
Logical file name getter.
Definition: FileMetaData.h:47
REGISTER_PYTHON_MODULE
#define REGISTER_PYTHON_MODULE(moduleName)
Register a python module to make available when loading the library.
Definition: RegisterPythonModule.h:46