BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
EventWriterTool.cxx
Go to the documentation of this file.
1#include <Gaudi/PluginServiceV2.h>
2#include <GaudiKernel/IAppMgrUI.h>
3#include <TTree.h>
4#include <fstream>
5#include <string>
6#include <vector>
7
8#include "DataInfoSvc/IDataInfoSvc.h"
9#include "RootEventData/TJobInfo.h"
10
11#include "EventWriterTool.h"
12
14
16 info() << "In initialize()" << endmsg;
17
18 StatusCode sc = service( "RootCnvSvc", m_cnvSvc, false );
19 if ( !sc.isSuccess() ) sc = service( "EventCnvSvc", m_cnvSvc, true );
20 if ( !sc.isSuccess() )
21 {
22 error() << "Unable to locate IRootCnvSvc interface" << endmsg;
23 return sc;
24 }
25
26 sc = service( "EventDataSvc", m_pDataProvider, true );
27 if ( !sc.isSuccess() )
28 {
29 error() << "Unable to locate IDataProviderSvc interface" << endmsg;
30 return sc;
31 }
32
33 if ( m_mode == 2 )
34 { // Offline
35 m_outputFile = new TFile( m_dofileName.value().c_str(), "RECREATE" );
36 if ( m_outputFile->IsZombie() || !m_outputFile->IsWritable() )
37 {
38 error() << "Can't open file " << m_dofileName.value() << endmsg;
39 return StatusCode::FAILURE;
40 }
41
42 m_outputTree = new TTree( "Event", "Event" );
43 m_jobInfoTree = new TTree( "JobInfoTree", "Job info" );
44 m_jobInfo = new TJobInfo;
45 }
46 else if ( m_mode == 3 )
47 {
48 error() << "The DistBossMode has been abandoned!" << endmsg;
49 return StatusCode::FAILURE;
50 }
51 else
52 {
53 error() << "Invalid RunMode @ initialize(): " << m_mode << endmsg;
54 return StatusCode::FAILURE;
55 }
56
57 // get services
58 if ( m_itemNames.empty() )
59 {
60 error() << "no ItemList from the jobOption" << endmsg;
61 return StatusCode::FAILURE;
62 }
63
64 // clear items
65 for ( auto& item : m_itemLists ) { delete item; }
66 m_itemLists.clear();
67
68 // add items
69 for ( const auto& item : m_itemNames ) { addItem( m_itemLists, item ); }
70
71 // reset status
72 m_st = 0;
73
74 return StatusCode::SUCCESS;
75}
76
77/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
78void EventWriterTool::addItem( std::vector<DataStoreItem*>& items,
79 const std::string& descriptor ) {
80 int level = 0;
81 size_t sep = descriptor.rfind( "#" );
82
83 std::string obj_path( descriptor, 0, sep );
84 std::string slevel( descriptor, sep + 1, descriptor.length() );
85
86 if ( slevel == "*" ) level = 9999999;
87 else level = std::atoi( slevel.c_str() );
88
89 size_t idx = obj_path.find( "/", 1 );
90 while ( idx != std::string::npos )
91 {
92 std::string sub_item = obj_path.substr( 0, idx );
93 if ( findItem( sub_item ) == nullptr ) addItem( items, sub_item + "#1" );
94 idx = obj_path.find( "/", idx + 1 );
95 }
96
97 DataStoreItem* item = new DataStoreItem( obj_path, level );
98 debug() << "Adding OutputStream item " << item->path() << " with " << item->depth()
99 << " level(s)." << endmsg;
100
101 items.push_back( item );
102}
103
104/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
105DataStoreItem* EventWriterTool::findItem( const std::string& path ) {
106 for ( const auto& item : m_itemLists )
107 {
108 if ( item->path() == path ) return item;
109 }
110 return nullptr;
111}
112
113/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
115 info() << "In write()" << endmsg;
116
117 /* Collect objects */
118 StatusCode sc;
119 for ( const auto& item : m_itemLists )
120 {
121 DataObject* obj = nullptr;
122 IOpaqueAddress* pAddress = nullptr;
123
124 sc = m_pDataProvider->retrieveObject( item->path(), obj );
125 if ( !sc.isSuccess() )
126 { error() << "Cannot write mandatory object(s) (Not found): " << item->path() << endmsg; }
127 else
128 {
129 sc = dynamic_cast<IConversionSvc*>( m_cnvSvc )->createRep( obj, pAddress );
130
131 // Only warn here because old version of `EventWriter` did so.
132 if ( !sc.isSuccess() )
133 warning() << "Failed to create rep for " << item->path() << endmsg;
134 }
135 }
136
137 /* Write to file */
138 if ( m_mode == 2 )
139 {
140 if ( m_st != 1 )
141 {
142 if ( m_commonData.m_rectrackEvt )
143 m_outputTree->Branch( "TRecEvent", "TRecTrackEvent", &m_commonData.m_rectrackEvt,
144 3200000, 1 );
145 if ( m_commonData.m_evtRecObject )
146 m_outputTree->Branch( "TEvtRecObject", "TEvtRecObject", &m_commonData.m_evtRecObject,
147 3200000, 1 );
148 if ( m_commonData.m_dstEvt )
149 m_outputTree->Branch( "TDstEvent", "TDstEvent", &m_commonData.m_dstEvt, 3200000, 1 );
150 if ( m_commonData.m_recEvt )
151 m_outputTree->Branch( "TDigiEvent", "TDigiEvent", &m_commonData.m_recEvt, 3200000, 1 );
152 if ( m_commonData.m_EvtHeader )
153 m_outputTree->Branch( "TEvtHeader", "TEvtHeader", &m_commonData.m_EvtHeader, 3200000,
154 1 );
155 if ( m_commonData.m_EvtNavigator )
156 m_outputTree->Branch( "TEvtNavigator", "TEvtNavigator", &m_commonData.m_EvtNavigator,
157 3200000, 1 );
158 if ( m_commonData.m_hltEvt )
159 m_outputTree->Branch( "THltEvent", "THltEvent", &m_commonData.m_hltEvt, 3200000, 1 );
160 if ( m_commonData.m_mcEvt )
161 m_outputTree->Branch( "TMcEvent", "TMcEvent", &m_commonData.m_mcEvt, 3200000, 1 );
162 if ( m_commonData.m_trigEvt )
163 m_outputTree->Branch( "TTrigEvent", "TTrigEvent", &m_commonData.m_trigEvt, 3200000,
164 1 );
165 m_jobInfoTree->Branch( "JobInfo", &m_jobInfo );
166 m_st = 1;
167 }
168
169 if ( m_outputFile->IsZombie() || !m_outputFile->IsOpen() )
170 {
171 error() << "Can not write to the file " << m_dofileName << endmsg;
172 return StatusCode::FAILURE;
173 }
174
175 int nb = m_outputTree->Fill();
176 if ( nb == -1 )
177 {
178 error() << "Error in fill tree (EventWriter) " << m_outputTree->GetName() << " with "
179 << nb << " bytes" << endmsg;
180 return StatusCode::FAILURE;
181 }
182
183 m_outputFile = m_outputTree->GetCurrentFile();
184 }
185 else if ( m_mode == 3 )
186 {
187 error() << "The DistBossMode has been deprecated!" << endmsg;
188 return StatusCode::FAILURE;
189 }
190
191 m_commonData.clear();
192
193 return StatusCode::SUCCESS;
194}
195
196/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
198 info() << "In finalize()" << endmsg;
199
200 if ( m_mode == 2 )
201 {
202 char* bossVer = getenv( "BES_RELEASE" );
203 info() << "fill boss version: " << bossVer << endmsg;
204
205 /* Get jobOptions */
206 std::string path;
207 auto mgrProp = SmartIF<IProperty>( Gaudi::createApplicationMgr() );
208 mgrProp->getProperty( "JobOptionsPath", path ).ignore();
209
210 info() << "JobOptions file for current job: " << path << endmsg;
211
212 ifstream fin( path.c_str() );
213 string jobOptions;
214 string tempString;
215 while ( getline( fin, tempString ) )
216 {
217 if ( tempString.size() > 0 && tempString.find( "//" ) > tempString.size() )
218 {
219 jobOptions += tempString;
220 jobOptions += "\n";
221 }
222 }
223 // info() << "JobOptions: " << endmsg << jobOptions << endmsg;
224
225 vector<string> jobOptionsVec = { jobOptions };
226
227 auto jobInfoSvc = service<IDataInfoSvc>( "DataInfoSvc" );
228 string decayOptions;
229 if ( !jobInfoSvc ) warning() << "Could not get DataInfoSvc" << endmsg;
230 else
231 {
232 decayOptions = jobInfoSvc->getDecayOptions();
233 info() << "Get decay options: " << decayOptions << endmsg;
234 }
235
236 m_jobInfo->setBossVer( bossVer );
237 m_jobInfo->setJobOptions( jobOptionsVec );
238 m_jobInfo->setDecayOptions( decayOptions );
239
240 m_jobInfoTree->Fill();
241
242 m_st = m_outputFile->Write();
243 if ( m_st == 0 )
244 {
245 error() << "Can not write to the file " << m_dofileName << endmsg;
246 return StatusCode::FAILURE;
247 }
248 m_outputFile->Close();
249
250 delete m_outputFile;
251 delete m_jobInfo;
252
253 m_outputFile = nullptr;
254 m_outputTree = nullptr;
255 }
256
257 if ( m_mode == 3 )
258 {
259 error() << "The DistBossMode has been deprecated!" << endmsg;
260 return StatusCode::FAILURE;
261 }
262
263 return StatusCode::SUCCESS;
264}
DECLARE_COMPONENT(BesBdkRc)
StatusCode initialize() override
Overriding initialize / finalize.
StatusCode finalize() override
StatusCode write() override