14 #include <boost/program_options.hpp>
15 #include <boost/algorithm/string.hpp>
21 #include <TDirectory.h>
23 #include <TObjString.h>
27 #include "GaudiKernel/StatusCode.h"
28 #include "GaudiKernel/Bootstrap.h"
29 #include "GaudiKernel/IMessageSvc.h"
30 #include "GaudiKernel/ISvcLocator.h"
38 #include "../Variable.h"
39 #include "../CodeGenerator_v2.h"
40 #include "../RootObjectMetadata.h"
43 static const char*
const PROGRAM_DESC =
44 "This application can be used to create all the D3PDReader classes\n"
45 "from a D3PD file that stores the metadata of the D3PDObjects that\n"
46 "were used to create it";
49 static const char*
const PROGRAM_GREETING =
50 "*******************************************************************\n"
52 "* D3PDReader standalone code generator *\n"
54 "*******************************************************************";
67 template<
typename T >
70 typename std::vector< T >::const_iterator itr =
vec.begin();
71 typename std::vector< T >::const_iterator
end =
vec.end();
72 for( ; itr !=
end; ++itr ) {
85 std::set< D3PD::ObjectMetadata >&
objects );
87 std::vector< D3PD::ObjectMetadata >
91 namespace po = boost::program_options;
99 SmartIF<IMessageSvc> msvc{Gaudi::svcLocator()->service(
"MessageSvc")};
102 <<
"Couldn't set up the Gaudi message service";
110 po::options_description
desc( PROGRAM_DESC );
112 (
"help,h",
"Give some help with the program usage" )
113 (
"d3pd-files,f",
po::value< std::vector< std::string > >()->multitoken(),
114 "Input D3PD file(s)" )
115 (
"event-class-name,n", po::value< std::string >()->default_value(
"Event" ),
116 "Name for the D3PDReader event class" )
117 (
"output,o", po::value< std::string >()->default_value(
"." ),
118 "Output directory for the generated source files" )
119 (
"verbosity,v", po::value< int >()->default_value( 3 ),
120 "Verbosity level of the application. (1:VERBOSE, 2:DEBUG, 3:INFO, ...)" );
125 po::variables_map vm;
131 <<
"There was a problem with interpreting the command line options.";
133 <<
"Message: " << ex.what();
140 if( vm.count(
"help" ) ) {
141 std::cout <<
desc << std::endl;
148 msvc->setOutputLevel( vm[
"verbosity" ].as< int >() );
154 std::cout << PROGRAM_GREETING << std::endl;
157 if( ! vm.count(
"d3pd-files" ) ) {
159 <<
"You have to specify at least one D3PD file!";
162 const std::vector< std::string > file_names =
163 vm[
"d3pd-files" ].as< std::vector< std::string > >();
164 std::cout <<
" D3PD file(s): " << file_names << std::endl;
167 const std::string event_name = vm[
"event-class-name" ].as< std::string >();
168 std::cout <<
" Event class name: " << event_name << std::endl;
171 const std::string
output = vm[
"output" ].as< std::string >();
172 std::cout <<
" Output directory: " <<
output << std::endl;
177 std::set< D3PD::ObjectMetadata >
objects;
182 std::vector< std::string >::const_iterator file_itr = file_names.begin();
183 std::vector< std::string >::const_iterator file_end = file_names.end();
184 for( ; file_itr != file_end; ++file_itr ) {
188 <<
"Reading file: " << *file_itr;
189 TFile*
ifile = TFile::Open( file_itr->c_str(),
"READ" );
192 <<
"Failed to open D3PD file: " << *file_itr;
197 std::vector< std::string > metaDirectories;
198 const TList*
keys =
ifile->GetListOfKeys();
199 for( Int_t
i = 0;
i <
keys->GetSize(); ++
i ) {
200 const TKey*
key =
dynamic_cast< TKey*
>(
keys->At(
i ) );
202 std::string key_name =
key->GetName();
203 if( ( key_name.find(
"Meta" ) != key_name.npos ) &&
204 (
key->GetClassName() == std::string(
"TDirectoryFile" ) ) ) {
205 metaDirectories.push_back( key_name );
210 std::vector< std::string >::const_iterator dir_itr = metaDirectories.begin();
211 std::vector< std::string >::const_iterator dir_end = metaDirectories.end();
212 for( ; dir_itr != dir_end; ++dir_itr ) {
214 TDirectory*
dir =
ifile->GetDirectory( dir_itr->c_str() );
217 <<
"Failed to access directory: " << *dir_itr;
223 <<
"Failed to collect variable definitions from metadata directory: "
238 if( ! merged_objects.size() ) {
240 <<
"Didn't find any metadata about the D3PD variables in the "
241 <<
"specified file(s)!";
250 <<
"Couldn't create the D3PDObjectBase class source";
259 <<
"Couldn't create the VarHandle class source";
268 <<
"Couldn't create the VarProxy class source";
277 <<
"Couldn't create the UserD3PDObject class source";
286 <<
"Couldn't create the D3PDReadStats class source";
295 <<
"Couldn't create the D3PDPerfStats class source";
304 <<
"Couldn't create the Utils source";
311 std::vector< D3PD::ObjectMetadata >::const_iterator obj_itr = merged_objects.begin();
312 std::vector< D3PD::ObjectMetadata >::const_iterator obj_end = merged_objects.end();
313 for( ; obj_itr != obj_end; ++obj_itr ) {
319 *obj_itr ).isFailure() ) {
321 <<
"Couldn't generate the D3PDReader class header";
329 *obj_itr ).isFailure() ) {
331 <<
"Couldn't generate the D3PDReader class source";
342 <<
"Couldn't generate the D3PDReader event class header";
348 <<
"Couldn't generate the D3PDReader event class source";
365 std::set< D3PD::ObjectMetadata >&
objects ) {
368 const TList*
keys =
dir->GetListOfKeys();
369 for( Int_t
i = 0;
i <
keys->GetSize(); ++
i ) {
372 const TKey*
key =
dynamic_cast< TKey*
>(
keys->At(
i ) );
374 if(
key->GetClassName() != std::string(
"TObjString" ) )
continue;
385 <<
"Reading object '" << objName <<
"'";
388 TObjString* ostring =
389 dynamic_cast< TObjString*
>(
dir->Get( TString(
key->GetName() ) +
";" +
393 <<
"Couldn't access object: " <<
key->GetName() <<
";"
395 return StatusCode::FAILURE;
401 if(
metadata.read( ostring->GetString().Data() ).isFailure() ) {
403 <<
"Couldn't collect variables from object: " <<
key->GetName();
404 return StatusCode::FAILURE;
408 if(
metadata.checkPrefixes().isFailure() ) {
410 <<
"Couldn't fix prefixes in metadata object with name: "
412 return StatusCode::FAILURE;
421 <<
"Merging objects with name '" << objName <<
"'";
428 <<
"Added object with name '" << objName <<
"'";
433 return StatusCode::SUCCESS;
444 std::vector< D3PD::ObjectMetadata >
448 std::map< std::string, D3PD::ObjectMetadata > tmp_result;
451 std::set< D3PD::ObjectMetadata >::const_iterator set_itr =
objects.begin();
452 std::set< D3PD::ObjectMetadata >::const_iterator set_end =
objects.end();
453 for( ; set_itr != set_end; ++set_itr ) {
454 tmp_result[ set_itr->name() ].setName( set_itr->name() );
455 tmp_result[ set_itr->name() ].setPrefix( set_itr->prefix() );
456 tmp_result[ set_itr->name() ].setContainer( set_itr->container() );
457 tmp_result[ set_itr->name() ].merge( *set_itr );
461 std::vector< D3PD::ObjectMetadata >
result;
462 std::map< std::string, D3PD::ObjectMetadata >::const_iterator map_itr =
464 std::map< std::string, D3PD::ObjectMetadata >::const_iterator map_end =
466 for( ; map_itr != map_end; ++map_itr ) {
467 result.push_back( map_itr->second );