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