Warning, file /DD4hep/DDCond/src/plugins/ConditionsPlugins.cpp was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <DD4hep/Detector.h>
0016 #include <DD4hep/Plugins.h>
0017 #include <DD4hep/Printout.h>
0018 #include <DD4hep/Conditions.h>
0019 #include <DD4hep/PluginCreators.h>
0020 #include <DD4hep/DetFactoryHelper.h>
0021 #include <DD4hep/ConditionsPrinter.h>
0022 #include <DD4hep/DetectorProcessor.h>
0023 #include <DD4hep/ConditionsProcessor.h>
0024 #include <DD4hep/ConditionsPrinter.h>
0025 #include <DD4hep/AlignmentsPrinter.h>
0026 #include <DD4hep/PluginTester.h>
0027
0028 #include <DDCond/ConditionsPool.h>
0029 #include <DDCond/ConditionsSlice.h>
0030 #include <DDCond/ConditionsManager.h>
0031 #include <DDCond/ConditionsIOVPool.h>
0032 #include <DDCond/ConditionsRepository.h>
0033 #include <DDCond/ConditionsManagerObject.h>
0034
0035
0036 #include <memory>
0037
0038 using namespace dd4hep;
0039 using namespace dd4hep::cond;
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049 static int ddcond_install_cond_mgr (Detector& description, int argc, char** argv) {
0050 Handle<NamedObject>* h = 0;
0051 Handle<ConditionsManagerObject> mgr(description.extension<ConditionsManagerObject>(false));
0052 if ( !mgr.isValid() ) {
0053 bool arg_error = false;
0054 std::string factory = "DD4hep_ConditionsManager_Type1";
0055 for(int i = 0; i < argc && argv[i]; ++i) {
0056 if ( 0 == ::strncmp("-type",argv[i],4) )
0057 factory = argv[++i];
0058 else if ( 0 == ::strncmp("-handle",argv[i],5) )
0059 h = (Handle<NamedObject>*)argv[++i];
0060 else
0061 arg_error = true;
0062 }
0063 if ( arg_error ) {
0064
0065 std::cout <<
0066 "Usage: -plugin <name> -arg [-arg] \n"
0067 " name: factory name DD4hep_ConditionsManagerInstaller \n"
0068 " -type <string> Manager type. \n"
0069 " Default: ConditionsManagerObject_Type1_t \n"
0070 " -handle <pointer> Pointer to Handle<NamedObject> to pass pointer \n"
0071 " to the caller. \n"
0072 "\tArguments given: " << arguments(argc,argv) << std::endl << std::flush;
0073 ::exit(EINVAL);
0074 }
0075 ConditionsManagerObject* obj = createPlugin<ConditionsManagerObject>(factory,description);
0076 if ( !obj ) {
0077 except("ConditionsManagerInstaller","Failed to create manager object of type %s",
0078 factory.c_str());
0079 }
0080 description.addExtension<ConditionsManagerObject>(obj);
0081 printout(INFO,"DDCond",
0082 "+++ Successfully installed conditions manager instance '%s' to Detector.",
0083 factory.c_str());
0084 mgr = obj;
0085 }
0086 else if ( argc > 0 ) {
0087 for(int i=0; i<argc && argv[i]; ++i) {
0088 if ( 0 == ::strncmp("-handle",argv[i],5) ) {
0089 h = (Handle<NamedObject>*)argv[++i];
0090 break;
0091 }
0092 }
0093 }
0094 if ( h ) *h = mgr;
0095 return 1;
0096 }
0097 DECLARE_APPLY(DD4hep_ConditionsManagerInstaller,ddcond_install_cond_mgr)
0098
0099
0100
0101
0102
0103
0104
0105
0106 static ConditionsSlice* ddcond_prepare(Detector& description, const std::string& iov_typ, long iov_val, int argc, char** argv) {
0107 const IOVType* iovtype = 0;
0108 long iovvalue = iov_val;
0109 ConditionsManager manager = ConditionsManager::from(description);
0110
0111 for(int i = 0; i < argc; ++i) {
0112 if ( ::strncmp(argv[i],"-iov_type",7) == 0 )
0113 iovtype = manager.iovType(argv[++i]);
0114 else if ( ::strncmp(argv[i],"-iov_value",7) == 0 )
0115 iovvalue = ::atol(argv[++i]);
0116 }
0117 if ( 0 == iovtype )
0118 iovtype = manager.iovType(iov_typ);
0119 if ( 0 == iovtype )
0120 except("ConditionsPrepare","++ Unknown IOV type supplied.");
0121 if ( 0 > iovvalue )
0122 except("ConditionsPrepare",
0123 "++ Unknown IOV value supplied for iov type %s.",iovtype->str().c_str());
0124
0125 IOV iov(iovtype,iovvalue);
0126 std::shared_ptr<ConditionsContent> content(new ConditionsContent());
0127 std::unique_ptr<ConditionsSlice> slice(new ConditionsSlice(manager,content));
0128 cond::fill_content(manager,*content,*iovtype);
0129 manager.prepare(iov, *slice);
0130 printout(INFO,"Conditions",
0131 "+++ ConditionsUpdate: Collected %ld conditions of type %s [iov-value:%ld].",
0132 long(slice->size()), iovtype ? iovtype->str().c_str() : "???", iovvalue);
0133 return slice.release();
0134 }
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145 static int ddcond_conditions_pool_processor(Detector& description, bool process_pool, bool process_conditions, int argc, char** argv) {
0146 std::unique_ptr<Condition::Processor> proc(createProcessor<Condition::Processor>(description,argc,argv));
0147 ConditionsManager manager = ConditionsManager::from(description);
0148 const auto types = manager.iovTypesUsed();
0149
0150 if ( !proc.get() ) {
0151 printout(WARNING,"Conditions","+++ Conditions processor of type %s is invalid!",argv[0]);
0152 }
0153 if ( process_conditions && !proc.get() ) {
0154 except("Conditions","+++ Conditions processor of type %s is invalid!",argv[0]);
0155 }
0156 for( const IOVType* type : types ) {
0157 if ( type ) {
0158 ConditionsIOVPool* pool = manager.iovPool(*type);
0159 if ( pool ) {
0160 const ConditionsIOVPool::Elements& e = pool->elements;
0161 if ( process_pool ) {
0162 printout(INFO,"CondPoolProcessor","+++ ConditionsIOVPool for type %s [%d IOV element%s]",
0163 type->str().c_str(), int(e.size()),e.size()==1 ? "" : "s");
0164 }
0165 for ( const auto& cp : e ) {
0166 if ( process_pool ) {
0167 cp.second->print("");
0168 }
0169 if ( process_conditions ) {
0170 RangeConditions rc;
0171 cp.second->select_all(rc);
0172 for( auto c : rc ) {
0173 if ( proc.get() ) { (*proc)(c); }
0174 }
0175 }
0176 }
0177 }
0178 }
0179 }
0180 return 1;
0181 }
0182 static int ddcond_conditions_pool_process(Detector& description, int argc, char** argv) {
0183 return ddcond_conditions_pool_processor(description, false, true, argc, argv);
0184 }
0185 DECLARE_APPLY(DD4hep_ConditionsPoolProcessor,ddcond_conditions_pool_process)
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197 static int ddcond_conditions_pool_print(Detector& description, bool print_conditions, int argc, char** argv) {
0198 if ( argc > 0 ) {
0199 for(int i = 0; i < argc; ++i) {
0200 if ( argv[i] && 0 == ::strncmp(argv[i],"-processor",3) ) {
0201 std::vector<char*> args;
0202 for(int j=i; j<argc && argv[j] && 0 != ::strncmp(argv[i],"-end-processor",8); ++j)
0203 args.emplace_back(argv[j]);
0204 args.emplace_back(nullptr);
0205 return ddcond_conditions_pool_processor(description,true,print_conditions,int(args.size()-1),&args[0]);
0206 }
0207 }
0208 printout(WARNING,"DDCondProcessor","++ Found arguments in plugin call, "
0209 "but could not make any sense of them....");
0210 }
0211 const void* args[] = { "-processor", "DD4hep_ConditionsPrinter", 0};
0212 return ddcond_conditions_pool_processor(description,true,print_conditions,2,(char**)args);
0213 }
0214
0215 static int ddcond_dump_pools(Detector& description, int argc, char** argv) {
0216 return ddcond_conditions_pool_print(description, false, argc, argv);
0217 }
0218 static int ddcond_dump_conditions(Detector& description, int argc, char** argv) {
0219 return ddcond_conditions_pool_print(description, true, argc, argv);
0220 }
0221 DECLARE_APPLY(DD4hep_ConditionsPoolDump,ddcond_dump_pools)
0222 DECLARE_APPLY(DD4hep_ConditionsDump,ddcond_dump_conditions)
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233 static int ddcond_detelement_dump(Detector& description, int argc, char** argv) {
0234
0235
0236
0237
0238
0239
0240
0241 struct Actor : public DetectorProcessor {
0242 ConditionsPrinter& printer;
0243
0244 Actor(ConditionsPrinter& p) : printer(p) { }
0245
0246 virtual ~Actor() { }
0247
0248 virtual int operator()(DetElement de,int level) const {
0249 const DetElement::Children& children = de.children();
0250 PlacedVolume place = de.placement();
0251 char sens = place.volume().isSensitive() ? 'S' : ' ';
0252 char fmt[128], tmp[32];
0253 ::snprintf(tmp,sizeof(tmp),"%03d/",level+1);
0254 ::snprintf(fmt,sizeof(fmt),"%03d %%-%ds %%s #Dau:%%d VolID:%%08X %%c",level+1,2*level+1);
0255 printout(INFO,"DetectorDump",fmt,"",de.path().c_str(),int(children.size()),
0256 (unsigned long)de.volumeID(), sens);
0257 printer.prefix = std::string(tmp)+de.name();
0258 (printer)(de, level);
0259 return 1;
0260 }
0261 };
0262 dd4hep_ptr<ConditionsSlice> slice(ddcond_prepare(description,"run",1500,argc,argv));
0263 ConditionsPrinter printer(slice.get(),"");
0264 UserPool* pool = slice->pool.get();
0265 pool->print("User pool");
0266 Actor actor(printer);
0267 int ret = actor.process(description.world(),0,true);
0268 slice->manager.clean(pool->validity().iovType, 20);
0269 return ret > 0 ? 1 : 0;
0270 }
0271 DECLARE_APPLY(DD4hep_DetElementConditionsDump,ddcond_detelement_dump)
0272
0273
0274
0275
0276
0277
0278
0279
0280
0281
0282 static void* ddcond_prepare_plugin(Detector& description, int argc, char** argv) {
0283 dd4hep_ptr<ConditionsSlice> slice(ddcond_prepare(description,"",-1,argc,argv));
0284 UserPool* p = slice->pool.get();
0285 return p && p->size() > 0 ? slice.release() : 0;
0286 }
0287 DECLARE_DD4HEP_CONSTRUCTOR(DD4hep_ConditionsPrepare,ddcond_prepare_plugin)
0288 #if 0
0289
0290
0291
0292
0293
0294
0295
0296
0297
0298 static int ddcond_detelement_processor(Detector& description, int argc, char** argv) {
0299
0300
0301
0302
0303
0304
0305
0306 struct Actor : public DetElementProcessor<ConditionsProcessor> {
0307
0308 Actor(ConditionsProcessor* p) : DetElementProcessor<ConditionsProcessor>(p){}
0309
0310
0311 virtual ~Actor() { }
0312
0313 virtual int operator()(DetElement de, int)
0314 { return de.hasConditions() ? processor->processElement(de) : 1; }
0315 };
0316 ConditionsProcessor* processor = 0;
0317 if ( argc > 0 ) {
0318 processor = createProcessor<ConditionsProcessor>(description, argc, argv);
0319 }
0320 else {
0321 const void* args[] = { "-processor", "DD4hep_ConditionsPrinter", 0};
0322 processor = createProcessor<ConditionsProcessor>(description, 2, (char**)args);
0323 }
0324 dd4hep_ptr<ConditionsSlice> slice(ddcond_prepare(description,"run",1500,argc,argv));
0325 UserPool* pool = slice->pool.get();
0326 Actor actor(processor);
0327 pool->print("User pool");
0328 processor->setPool(pool);
0329 int ret = Actor(processor).process(description.world(),0,true);
0330 slice->manager.clean(pool->validity().iovType, 20);
0331 return ret > 0 ? 1 : 0;
0332 }
0333 DECLARE_APPLY(DD4hep_DetElementConditionsProcessor,ddcond_detelement_processor)
0334 #endif
0335
0336
0337
0338
0339
0340
0341
0342
0343
0344 static long ddcond_synchronize_conditions(Detector& description, int argc, char** argv) {
0345 if ( argc >= 2 ) {
0346 std::string iov_typ = argv[0];
0347 IOV::Key::first_type iov_key = *(IOV::Key::first_type*)argv[1];
0348 dd4hep_ptr<ConditionsSlice> slice(ddcond_prepare(description,iov_typ,iov_key,argc,argv));
0349 UserPool* pool = slice->pool.get();
0350 pool->print("User pool");
0351 slice->manager.clean(pool->validity().iovType, 20);
0352 pool->clear();
0353 return 1;
0354 }
0355 except("Conditions","+++ Failed update conditions. Arguments were: '%s'",
0356 arguments(argc,argv).c_str());
0357 return 0;
0358 }
0359 DECLARE_APPLY(DD4hep_ConditionsSynchronize,ddcond_synchronize_conditions)
0360
0361
0362
0363
0364
0365
0366
0367
0368
0369
0370 static long ddcond_clean_conditions(Detector& description, int argc, char** argv) {
0371 if ( argc > 0 ) {
0372 std::string iov_type = argv[0];
0373 int max_age = *(int*)argv[1];
0374 printout(INFO,"Conditions",
0375 "+++ ConditionsUpdate: Cleaning conditions... type:%s max age:%d",
0376 iov_type.c_str(), max_age);
0377 ConditionsManager manager = ConditionsManager::from(description);
0378 const IOVType* iov_typ = manager.iovType(iov_type);
0379 manager.clean(iov_typ, max_age);
0380 return 1;
0381 }
0382 except("Conditions","+++ Failed cleaning conditions. Insufficient arguments!");
0383 return 0;
0384 }
0385 DECLARE_APPLY(DD4hep_ConditionsClean,ddcond_clean_conditions)
0386
0387
0388
0389
0390
0391
0392
0393
0394
0395
0396 template <typename WRAPPER,typename PRINTER>
0397 static void* create_printer(Detector& description, int argc,char** argv) {
0398 PrintLevel print_level = INFO;
0399 std::string prefix = "", name = "";
0400 int flags = 0, have_pool = 0, arg_error = false;
0401 for(int i=0; i<argc && argv[i]; ++i) {
0402 if ( 0 == ::strncmp("-prefix",argv[i],4) )
0403 prefix = argv[++i];
0404 else if ( 0 == ::strncmp("-name",argv[i],5) )
0405 name = argv[++i];
0406 else if ( 0 == ::strncmp("-flags",argv[i],5) )
0407 flags = ::atol(argv[++i]);
0408 else if ( 0 == ::strncmp("-pool",argv[i],5) )
0409 have_pool = 1;
0410 else if ( 0 == ::strncmp("-print",argv[i],5) )
0411 print_level = dd4hep::printLevel(argv[++i]);
0412 else
0413 arg_error = true;
0414 }
0415 if ( arg_error ) {
0416
0417 std::cout <<
0418 "Usage: -plugin <name> -arg [-arg] \n"
0419 " name: factory name(s) DD4hep_ConditionsPrinter, \n"
0420 " dd4hep_AlignmentsPrinter \n"
0421 " dd4hep_AlignedVolumePrinter \n"
0422 " -prefix <string> Printout prefix for user customized output. \n"
0423 " -flags <number> Printout processing flags. \n"
0424 " -pool Attach conditions user pool from \n"
0425 " PluginTester's slice instance attached. \n\n"
0426 " -print <value> Printout level for the printer object. \n"
0427 "\tArguments given: " << arguments(argc,argv) << std::endl << std::flush;
0428 ::exit(EINVAL);
0429 }
0430 DetElement world = description.world();
0431 printout(INFO,"Printer","World=%s [%p]",world.path().c_str(),world.ptr());
0432 ConditionsSlice* slice = 0;
0433 if ( have_pool ) {
0434 PluginTester* test = description.extension<PluginTester>();
0435 slice = test->extension<ConditionsSlice>("ConditionsTestSlice");
0436 }
0437 PRINTER* p = (flags) ? new PRINTER(slice, prefix, flags) : new PRINTER(slice, prefix);
0438 p->printLevel = print_level;
0439 if ( !name.empty() ) p->name = std::move(name);
0440 return (void*)dynamic_cast<WRAPPER*>(createProcessorWrapper(p));
0441 }
0442
0443 static void* create_cond_printer(Detector& description, int argc,char** argv)
0444 { return create_printer<Condition::Processor,ConditionsPrinter>(description,argc,argv); }
0445
0446 DECLARE_DD4HEP_CONSTRUCTOR(DD4hep_ConditionsPrinter,create_cond_printer)
0447
0448
0449
0450
0451
0452
0453
0454
0455
0456
0457
0458
0459 static long ddcond_create_repository(Detector& description, int argc, char** argv) {
0460 bool arg_error = false;
0461 std::string output = "";
0462 for(int i=0; i<argc && argv[i]; ++i) {
0463 if ( 0 == ::strncmp("-output",argv[i],4) )
0464 output = argv[++i];
0465 else
0466 arg_error = true;
0467 }
0468 if ( arg_error || output.empty() ) {
0469
0470 std::cout <<
0471 "Usage: -plugin <name> -arg [-arg] \n"
0472 " name: factory name DD4hep_ConditionsCreateRepository \n\n"
0473 " -output <string> Output file name. \n\n"
0474 "\tArguments given: " << arguments(argc,argv) << std::endl << std::flush;
0475 ::exit(EINVAL);
0476 }
0477 printout(INFO,"Conditions",
0478 "+++ ConditionsRepository: Creating %s",output.c_str());
0479 ConditionsManager manager = ConditionsManager::from(description);
0480 ConditionsRepository().save(manager,output);
0481 return 1;
0482 }
0483 DECLARE_APPLY(DD4hep_ConditionsCreateRepository,ddcond_create_repository)
0484
0485
0486
0487
0488
0489
0490
0491
0492
0493
0494 static long ddcond_dump_repository(Detector& , int argc, char** argv) {
0495 typedef ConditionsRepository::Data Data;
0496 bool arg_error = false;
0497 std::string input = "";
0498 Data data;
0499 for(int i=0; i<argc && argv[i]; ++i) {
0500 if ( 0 == ::strncmp("-input",argv[i],4) )
0501 input = argv[++i];
0502 else
0503 arg_error = true;
0504 }
0505 if ( arg_error || input.empty() ) {
0506
0507 std::cout <<
0508 "Usage: -plugin <name> -arg [-arg] \n"
0509 " name: factory name DD4hep_ConditionsDumpRepository \n\n"
0510 " -input <string> Input file name. \n\n"
0511 "\tArguments given: " << arguments(argc,argv) << std::endl << std::flush;
0512 ::exit(EINVAL);
0513 }
0514 printout(INFO,"Conditions","+++ ConditionsRepository: Dumping %s",input.c_str());
0515 if ( ConditionsRepository().load(input, data) ) {
0516 printout(INFO,"Repository","%-8s %-60s %-60s","Key","Name","Address");
0517 for(Data::const_iterator i=data.begin(); i!=data.end(); ++i) {
0518 const ConditionsRepository::Entry& e = *i;
0519 std::string add = e.address;
0520 if ( add.length() > 80 ) add = e.address.substr(0,60) + "...";
0521 printout(INFO,"Repository","%16llX %s",e.key,e.name.c_str());
0522 printout(INFO,"Repository"," -> %s",e.address.c_str());
0523 }
0524 }
0525 return 1;
0526 }
0527 DECLARE_APPLY(DD4hep_ConditionsDumpRepository,ddcond_dump_repository)
0528
0529
0530
0531
0532
0533
0534
0535
0536
0537
0538
0539
0540
0541 static long ddcond_load_repository(Detector& , int argc, char** argv) {
0542 if ( argc > 0 ) {
0543 std::string input = argv[0];
0544 printout(INFO,"Conditions","+++ ConditionsRepository: Loading %s",input.c_str());
0545 ConditionsRepository::Data data;
0546 ConditionsRepository().load(input, data);
0547 return 1;
0548 }
0549 except("Conditions","+++ Failed loading conditions repository. Insufficient arguments!");
0550 return 0;
0551 }
0552 DECLARE_APPLY(DD4hep_ConditionsLoadRepository,ddcond_load_repository)
0553