BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
DetVerSvc.cxx
Go to the documentation of this file.
1#include "Gaudi/Interfaces/IOptionsSvc.h"
2#include "GaudiKernel/MsgStream.h"
3#include "GaudiKernel/Parsers.h"
4#include "GaudiKernel/SmartIF.h"
5#include "TFile.h"
6#include "TTree.h"
7#include <dlfcn.h>
8#include <fstream>
9
10#include "RawDataCnvSvc/IRawDataInputSvc.h"
11#include "RealizationSvc/IRealizationSvc.h"
12
13#include "DetVerSvc.h"
14
15using Gaudi::Interfaces::IOptionsSvc;
16using PropsVec = std::vector<std::tuple<std::string, std::string>>;
17
19
20namespace DetVerSvcPack {
21 int ( *pf_helper )( ISvcLocator* );
22}
23
24DetVerSvc::DetVerSvc( const std::string& name, ISvcLocator* svcloc )
25 : base_class( name, svcloc ) {
26 if ( getenv( "DETVERSVCROOT" ) )
27 m_conf = std::string( getenv( "DETVERSVCROOT" ) ) + "/share/config.txt";
28 else m_conf = "";
29
30 declareProperty( "Config", m_conf );
31 m_phase = -1;
32}
33
35
37 Service::initialize();
38
39 MsgStream log( msgSvc(), name() );
40 info() << name() << ": Start of run initialisation" << endmsg;
41
42 // assert m_conf is a valid file
43 if ( access( m_conf.c_str(), F_OK ) < 0 )
44 {
45 log << MSG::FATAL << "Cann't find config file: " << m_conf << endmsg;
46 return StatusCode::FAILURE;
47 }
48
49 return StatusCode::SUCCESS;
50}
51
52StatusCode DetVerSvc::finalize() {
53 MsgStream log( msgSvc(), name() );
54 info() << name() << ": End of Run finalize" << endmsg;
55
56 return StatusCode::SUCCESS;
57}
58
60 if ( m_phase < 1 || m_phase > 3 ) // phase has not been retrieved
61 {
62 if ( serviceLocator()->existsService( "RawDataInputSvc" ) ) // case: Input RAW
63 {
64 // Try to get runNo from RawDataInputSvc, then get phase from runNo
65 SmartIF<IRawDataInputSvc> raw_svc = service<IRawDataInputSvc>( "RawDataInputSvc" );
66 if ( !raw_svc )
67 {
68 error() << "Failed to get RawDataInputSvc" << endmsg;
69 exit( 1 );
70 }
71 m_phase = fromRun( raw_svc->runNo() );
72 }
73 else if ( serviceLocator()->existsService( "EventCnvSvc" ) ) // case: Input root
74 {
75 // ==========================================================
76 // Total procedure:
77 // [1] Get "EventCnvSvc.digiRootInputFile" from JobOptionsSvc
78 // [2] Parse and get first input file by std::regex
79 // [3] Open the file and get run number from the first entry
80 // ==========================================================
81
82 // Get digiRootInputFile from JobOptionsSvc
83 SmartIF<IOptionsSvc> opt_svc = service<IOptionsSvc>( "JobOptionsSvc" );
84 if ( !opt_svc )
85 {
86 error() << "Failed to get JobOptionsSvc" << endmsg;
87 exit( 1 );
88 }
89 std::vector<std::string> digi_inputs;
90 Gaudi::Parsers::parse( digi_inputs,
91 opt_svc->get( "EventCnvSvc.digiRootInputFile", "" ) );
92 if ( digi_inputs.size() == 0 )
93 {
94 error() << "Failed to get digiRootInputFile" << endmsg;
95 exit( 1 );
96 }
97
98 debug() << "Match result: " << digi_inputs[0] << std::endl;
99
100 // Open the file and get run number from the first entry
101 TFile* file = TFile::Open( digi_inputs[0].c_str(), "READ" );
102 TTree* tree = (TTree*)file->Get( "Event" );
103 tree->SetMakeClass( 1 );
104 int run;
105 tree->SetBranchAddress( "m_runId", &run );
106 tree->GetEntry( 0 );
107
108 m_phase = fromRun( abs( run ) );
109 }
110 else if ( serviceLocator()->existsService( "RealizationSvc" ) ) // case: Simulation
111 {
112 // Try to get runNo from RealizationSvc, then get phase from runNo
113 SmartIF<IRealizationSvc> real_svc = service<IRealizationSvc>( "RealizationSvc" );
114 if ( !real_svc )
115 {
116 error() << "Failed to get RealizationSvc" << endmsg;
117 exit( 1 );
118 }
119 m_phase = fromRun( abs( real_svc->getRunId() ) );
120 }
121 }
122
123 debug() << "m_phase: " << m_phase << std::endl;
124
125 // This check seems stupid, but just let it be here. :)
126 if ( m_phase < 1 || m_phase > 3 )
127 {
128 error() << "Phase is not in {1, 2, 3}" << endmsg;
129 exit( 1 );
130 }
131
132 return m_phase;
133}
134
135int DetVerSvc::fromRun( unsigned int run ) {
136 std::vector<int> runList;
137 // parse m_conf
138 int iTmp;
139 std::ifstream fs( m_conf.c_str() );
140 fs >> iTmp;
141 while ( !fs.eof() )
142 {
143 runList.push_back( iTmp );
144 fs >> iTmp;
145 }
146
147 // return phase from run
148 int phase = 1;
149 for ( int sep : runList )
150 {
151 if ( run < sep ) { break; }
152 ++phase;
153 }
154 return phase;
155}
#define fs
DECLARE_COMPONENT(BesBdkRc)
char * file
Definition DQA_TO_DB.cxx:16
std::vector< std::tuple< std::string, std::string > > PropsVec
Definition DetVerSvc.cxx:16
IMessageSvc * msgSvc()
virtual ~DetVerSvc()
Definition DetVerSvc.cxx:34
virtual StatusCode finalize()
Definition DetVerSvc.cxx:52
int fromRun(unsigned int run)
int phase()
Definition DetVerSvc.cxx:59
DetVerSvc(const std::string &name, ISvcLocator *svcloc)
Definition DetVerSvc.cxx:24
virtual StatusCode initialize()
Definition DetVerSvc.cxx:36
int(* pf_helper)(ISvcLocator *)
Definition DetVerSvc.cxx:21