12 #include "GaudiKernel/System.h"
19 static const char*
const CODE_COMMENT =
20 "// Dear emacs, this is -*- c++ -*-\n"
21 "// -------------------------------------------------------------\n"
22 "// Code produced by D3PDMakerReader\n"
24 "// author: Attila Krasznahorkay <Attila.Krasznahorkay@cern.ch>\n"
25 "// -------------------------------------------------------------";
28 static const char*
const CODE_NAMESPACE =
"D3PDReader";
45 const std::string&
dir )
const {
53 return StatusCode::SUCCESS;
57 const std::string&
dir )
const {
61 <<
"Generating file: " <<
dir <<
"/" << classname <<
".h";
64 std::fstream
header( (
dir +
"/" + classname +
".h" ).c_str(),
68 header << CODE_COMMENT << std::endl;
69 header <<
"#ifndef D3PDREADER_" << classname <<
"_H" << std::endl;
70 header <<
"#define D3PDREADER_" << classname <<
"_H" << std::endl << std::endl;
79 header <<
"#include <TNamed.h>" << std::endl;
80 header <<
"#include <TString.h>" << std::endl << std::endl;
83 header <<
"class TTree;" << std::endl;
84 header <<
"class TBranch;" << std::endl << std::endl;
89 header <<
"namespace " << CODE_NAMESPACE <<
" {" << std::endl << std::endl;
90 header <<
" /**" << std::endl;
91 header <<
" * Code generated by RootD3PDReader_v1 on:" << std::endl;
92 header <<
" * host = " << System::hostName() << std::endl;
93 header <<
" * OS = " << System::osName() <<
" / " << System::osVersion()
95 header <<
" * user = " << System::accountName() << std::endl;
96 time_t rawtime = time( NULL );
98 header <<
" * time = " << ctime_r( &rawtime, buf );
99 header <<
" */" << std::endl;
100 header <<
" class " << classname <<
" : public TNamed {" << std::endl << std::endl;
101 header <<
" public:" << std::endl;
106 header <<
" /// Constructor specifying the needed parameters" << std::endl;
107 header <<
" " << classname <<
"( Long64_t* master, const char* prefix = \""
113 header <<
" /// Get the currently configured prefix value" << std::endl;
114 header <<
" const char* getPrefix() const;" << std::endl;
115 header <<
" /// Set the prefix to the ntuple variables" << std::endl;
116 header <<
" void setPrefix( const char* prefix );" << std::endl << std::endl;
117 header <<
" /// Connect the object to a new TTree" << std::endl;
118 header <<
" void connect( TTree* tree );" << std::endl << std::endl;
123 std::set< D3PD::ObjectMetadata::Variable >::const_iterator itr =
125 std::set< D3PD::ObjectMetadata::Variable >::const_iterator
end =
127 for( ; itr !=
end; ++itr ) {
128 if( itr->doc() !=
"" ) {
129 header <<
" /// " << itr->doc() << std::endl;
131 header <<
" " << itr->type()
132 << ( itr->primitive() ?
" " :
"* " ) << itr->name() <<
"();"
136 header << std::endl <<
" private:" << std::endl;
141 header <<
" // Variables used in the TTree reading:" << std::endl;
144 for( ; itr !=
end; ++itr ) {
145 header <<
" " << itr->type()
146 << ( itr->primitive() ?
"" :
"*" ) <<
" m_"
147 << itr->name() <<
";" << std::endl;
155 header <<
" // TBranch variables used in the TTree reading:" << std::endl;
158 for( ; itr !=
end; ++itr ) {
159 header <<
" ::TBranch* m_b_" << itr->name() <<
"; //!" << std::endl;
163 header << std::endl <<
" ::TTree* m_tree;" << std::endl;
164 header <<
" Long64_t* m_master;" << std::endl;
165 header <<
" ::TString m_prefix;" << std::endl << std::endl;
168 header <<
" ClassDef( " << classname <<
", 0 )" << std::endl << std::endl;
169 header <<
" }; // class " << classname << std::endl << std::endl;
170 header <<
"} // namespace " << CODE_NAMESPACE << std::endl << std::endl;
171 header <<
"#endif // D3PDREADER_" << classname <<
"_H" << std::endl;
175 return StatusCode::SUCCESS;
179 const std::string&
dir )
const {
183 <<
"Generating file: " <<
dir <<
"/" << classname <<
".cxx";
186 std::fstream
source( (
dir +
"/" + classname +
".cxx" ).c_str(),
190 source << CODE_COMMENT << std::endl << std::endl;
193 source <<
"#include <TTree.h>" << std::endl;
194 source <<
"#include <TBranch.h>" << std::endl << std::endl;
197 source <<
"#include \"" << classname <<
".h\"" << std::endl << std::endl;
199 source <<
"ClassImp( D3PDReader::" << classname <<
" )" << std::endl << std::endl;
201 source <<
"namespace " << CODE_NAMESPACE <<
" {" << std::endl << std::endl;
206 source <<
" /**" << std::endl;
207 source <<
" * @param master Pointer to the variable holding the current "
208 <<
"event number" << std::endl;
209 source <<
" * @param prefix Prefix of the variables in the D3PD" << std::endl;
210 source <<
" */" << std::endl;
211 source <<
" " << classname <<
"::" << classname
212 <<
"( Long64_t* master, const char* prefix )" << std::endl;
213 source <<
" : TNamed( \"" << classname <<
"\", \"D3PDReader class\" ),"
215 std::set< D3PD::ObjectMetadata::Variable >::const_iterator itr =
217 std::set< D3PD::ObjectMetadata::Variable >::const_iterator
end =
219 for( ; itr !=
end; ++itr ) {
220 if( itr->primitive() )
continue;
221 source <<
" m_" << itr->name() <<
"( 0 )," << std::endl;
225 for( ; itr !=
end; ++itr ) {
226 source <<
" m_b_" << itr->name() <<
"( 0 )," << std::endl;
228 source <<
" m_tree( 0 ), m_master( master )," << std::endl;
229 source <<
" m_prefix( prefix ) {" << std::endl << std::endl;
230 source <<
" }" << std::endl << std::endl;
235 source <<
" const char* " << classname <<
"::getPrefix() const {" << std::endl
237 source <<
" return m_prefix;" << std::endl;
238 source <<
" }" << std::endl << std::endl;
239 source <<
" void " << classname <<
"::setPrefix( const char* prefix ) {"
240 << std::endl << std::endl;
241 source <<
" m_prefix = prefix;" << std::endl;
242 source <<
" return;" << std::endl;
243 source <<
" }" << std::endl << std::endl;
248 source <<
" /**" << std::endl;
249 source <<
" * This function should be called every time a new TFile is opened"
251 source <<
" * by your analysis code." << std::endl;
252 source <<
" *" << std::endl;
253 source <<
" * @param tree Pointer to the TTree with the variables" << std::endl;
254 source <<
" */" << std::endl;
255 source <<
" void " << classname <<
"::connect( TTree* tree ) {" << std::endl
259 for( ; itr !=
end; ++itr ) {
260 if( ! itr->primitive() ) {
261 source <<
" if( m_" << itr->name() <<
" ) delete m_" << itr->name()
263 source <<
" m_" << itr->name() <<
" = 0;" << std::endl;
265 source <<
" m_b_" << itr->name() <<
" = 0;" << std::endl << std::endl;
267 source <<
" m_tree = tree;" << std::endl << std::endl;
268 source <<
" return;" << std::endl;
269 source <<
" }" << std::endl << std::endl;
276 for( ; itr !=
end; ++itr ) {
277 source <<
" " << itr->type() << ( itr->primitive() ?
" " :
"* " )
278 << classname <<
"::" << itr->name()
279 <<
"() {" << std::endl << std::endl;
280 source <<
" if( ! m_b_" << itr->name() <<
" ) {" << std::endl;
281 source <<
" if( ! m_tree ) {" << std::endl;
282 source <<
" Error( \"Connect\", \"Object not connected yet!\" );"
284 source <<
" return 0;" << std::endl;
285 source <<
" }" << std::endl;
286 source <<
" if( ! m_tree->GetBranch( m_prefix + \"" << itr->name()
287 <<
"\" ) ) {" << std::endl;
288 source <<
" Error( \"Connect\", "
289 <<
"\"The following variable doesn't exist: %s\", "
290 <<
"( m_prefix + \"" << itr->name() <<
"\" ).Data() );" << std::endl;
291 source <<
" return 0;" << std::endl;
292 source <<
" }" << std::endl;
293 source <<
" m_tree->SetBranchAddress( m_prefix + \"" << itr->name()
294 <<
"\", &m_" << itr->name() <<
", &m_b_" << itr->name() <<
" );"
296 source <<
" }" << std::endl;
297 source <<
" if( *m_master != m_b_" << itr->name() <<
"->GetReadEntry() ) {"
299 source <<
" m_b_" << itr->name() <<
"->GetEntry( *m_master );"
301 source <<
" }" << std::endl;
302 source <<
" return m_" << itr->name() <<
";" << std::endl;
303 source <<
" }" << std::endl << std::endl;
306 source <<
"} // namespace " << CODE_NAMESPACE << std::endl;
310 return StatusCode::SUCCESS;