Belle II Software  release-05-02-19
RootFileCreationManager.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2013-2018 Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Christian Pulvermacher *
7  * Simon Wehle *
8  * Martin Ritter *
9  * *
10  * This software is provided "as is" without any warranty. *
11  **************************************************************************/
12 
13 #include <framework/utilities/RootFileCreationManager.h>
14 #include <framework/logging/Logger.h>
15 #include <framework/core/MetadataService.h>
16 #include <iomanip>
17 
18 namespace {
20  void TFile_Deleter(TFile* file)
21  {
22  B2DEBUG(30, "Closing ROOT file");
23  if (file->IsOpen()) {
24  file->Close();
25  }
27  delete file;
28  }
29 }
30 
31 namespace Belle2 {
36  std::shared_ptr<TFile> RootFileCreationManager::getFile(std::string fileName, bool ignoreErrors)
37  {
38  std::shared_ptr<TFile> ptr = m_files[fileName].lock();
39  if (!ptr) {
40  // make sure stupid gDirectory is not modified ... that means that users
41  // have to ptr->cd() but plays nice with other modules which might not
42  // set their cd() correctly.
43  TDirectory::TContext gDirectoryGuard;
44  // Create shared ptr for the file which will correctly close it when the last user disconnects.
45  ptr = std::shared_ptr<TFile>(TFile::Open(fileName.c_str(), "RECREATE"), TFile_Deleter);
46  // Check if the file is actually open ... otherwise no use in returning it
47  if (!ptr || !ptr->IsOpen()) {
48  if (!ignoreErrors) B2ERROR("Could not create file " << std::quoted(fileName));
49  return nullptr;
50  }
51  //remember this ...
52  m_files[fileName] = ptr;
53  }
54  return ptr;
55  }
56 
58  {
59  static RootFileCreationManager instance;
60  return instance;
61  }
63 }
Belle2::MetadataService::addRootNtupleFile
void addRootNtupleFile(const std::string &fileName)
Add the metadata of a root ntuple file.
Definition: MetadataService.cc:59
Belle2::RootFileCreationManager::m_files
std::map< std::string, std::weak_ptr< TFile > > m_files
store for the open files
Definition: RootFileCreationManager.h:75
Belle2::RootFileCreationManager
This single instance class takes track of all open ROOT files open in "create" mode,...
Definition: RootFileCreationManager.h:49
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::RootFileCreationManager::getInstance
static RootFileCreationManager & getInstance()
Interface for the FileManager.
Definition: RootFileCreationManager.cc:57
Belle2::RootFileCreationManager::getFile
std::shared_ptr< TFile > getFile(std::string, bool ignoreErrors=false)
Get a file with a specific name, if is does not exist it will be created.
Definition: RootFileCreationManager.cc:36
Belle2::MetadataService::Instance
static MetadataService & Instance()
Static method to get a reference to the MetadataService instance.
Definition: MetadataService.cc:28