EICrecon
JANA based reconstruction for the EPIC detector
Loading...
Searching...
No Matches
RootFile_service.h
Go to the documentation of this file.
1#pragma once
2
3#include <iostream>
4#include <vector>
5#include <string>
6#include <mutex>
7
8#include <JANA/JApplicationFwd.h>
10#include <JANA/Services/JGlobalRootLock.h>
11#include <JANA/Services/JServiceLocator.h>
12
13#include <TFile.h>
14
18class RootFile_service : public JService {
19public:
20 explicit RootFile_service(JApplication* app) : m_app(app) {}
22
23 void acquire_services(JServiceLocator* /* locayor */) override {
24 auto log_service = m_app->GetService<Log_service>();
25 m_log = log_service->logger("RootFile");
26 }
27
46 TDirectory* GetHistFile(bool create_if_needed = true) {
47
48 if (create_if_needed) {
49 std::call_once(init_flag, &RootFile_service::CreateHistFile, this);
50 }
51 return m_histfile;
52 }
53
65 if (m_histfile) {
66 std::string filename = m_histfile->GetName();
67 m_histfile->Write();
68 delete m_histfile;
69 m_log->info("Closed user histogram file: {}", filename);
70 }
71 m_histfile = nullptr;
72 }
73
74private:
77 void CreateHistFile() {
78 // Get root file name
79 std::string filename = "eicrecon.root";
80 m_app->SetDefaultParameter("histsfile", filename,
81 "Name of root file to be created for plugin histograms/trees");
82 if (!m_histfile) {
83 try {
84 m_histfile = new TFile(filename.c_str(), "RECREATE", "user histograms/trees");
85 m_log->info("Created file: {} for user histograms", filename);
86 } catch (std::exception& ex) {
87 m_log->error("Problem opening root file for histograms: {}", ex.what());
88 throw ex;
89 }
90 }
91 }
92
93 RootFile_service() = default;
94
95 JApplication* m_app = nullptr;
96 std::shared_ptr<spdlog::logger> m_log;
97 TFile* m_histfile = nullptr;
98 std::once_flag init_flag;
99};
Definition Log_service.h:15
Definition RootFile_service.h:18
void acquire_services(JServiceLocator *) override
Definition RootFile_service.h:23
void CloseHistFile()
Definition RootFile_service.h:64
RootFile_service(JApplication *app)
Definition RootFile_service.h:20
~RootFile_service() override
Definition RootFile_service.h:21
TDirectory * GetHistFile(bool create_if_needed=true)
Definition RootFile_service.h:46