Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:14:53

0001 //==========================================================================
0002 //  AIDA Detector description implementation 
0003 //--------------------------------------------------------------------------
0004 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
0005 // All rights reserved.
0006 //
0007 // For the licensing terms see $DD4hepINSTALL/LICENSE.
0008 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
0009 //
0010 // Author     : M.Frank
0011 //
0012 //==========================================================================
0013 //
0014 // DDDB is a detector description convention developed by the LHCb experiment.
0015 // For further information concerning the DTD, please see:
0016 // http://lhcb-comp.web.cern.ch/lhcb-comp/Frameworks/DetDesc/Documents/lhcbDtd.pdf
0017 //
0018 //==========================================================================
0019 
0020 // Framework includes
0021 #include "DD4hep/Detector.h"
0022 #include "DD4hep/Plugins.h"
0023 #include "DD4hep/Printout.h"
0024 #include "DD4hep/Factories.h"
0025 #include "DD4hep/ConditionsData.h"
0026 #include "DD4hep/ConditionsPrinter.h"
0027 #include "DD4hep/AlignmentsProcessor.h"
0028 #include "DD4hep/ConditionsProcessor.h"
0029 #include "DD4hep/detail/DetectorInterna.h"
0030 
0031 #include "DDCond/ConditionsOperators.h"
0032 #include "DDCond/ConditionsManager.h"
0033 #include "DDCond/ConditionsSlice.h"
0034 #include "DDDB/DDDBConversion.h"
0035 
0036 // C/C++ include files
0037 #include <memory>
0038 
0039 using namespace std;
0040 using namespace dd4hep;
0041 
0042 /// Anonymous namespace for plugins
0043 namespace {
0044   PrintLevel s_PrintLevel = INFO;
0045 }
0046 
0047 //==========================================================================
0048 /// Plugin function
0049 static long dddb_plugin_print_level(Detector& /* description */, int argc, char** argv) {
0050   for(int i=0; i<argc; ++i)  {
0051     if ( ::strcmp(argv[i],"--print")==0 )  {
0052       s_PrintLevel = dd4hep::printLevel(argv[++i]);
0053       printout(INFO,"DDDB","Setting print level for %s to %s [%d]",__FILE__,argv[i-1],s_PrintLevel);
0054       return 1;
0055     }
0056   }
0057   return 0;
0058 }
0059 DECLARE_APPLY(DDDB_PluginLevel,dddb_plugin_print_level)
0060 
0061 //==========================================================================
0062 /// Anonymous namespace for plugins
0063 namespace {
0064 
0065   using namespace dd4hep::cond;
0066 
0067   /// Basic entry point to print out the detector element hierarchy
0068   /**
0069    *  \author  M.Frank
0070    *  \version 1.0
0071    *  \date    01/04/2014
0072    */
0073   long dump_det_tree(Detector& description, int flag, int argc, char** argv) {
0074 
0075     using align::deltaCollector;    
0076     using DDDB::DDDBCatalog;
0077 
0078     /// Access the test's name
0079     /**
0080      *  \author  M.Frank
0081      *  \version 1.0
0082      *  \date    01/04/2014
0083      */
0084     struct naming  {
0085       /// Access the name of the test
0086       static const char* get(int flg)   {
0087         const char* n = "DDDBDetectorDump";
0088         if ( flg == 1 )
0089           n = "DDDBDetVolumeDump";
0090         else if ( flg == 2 )
0091           n = "DDDBDetConditionKeyDump";
0092         else if ( flg == 3 )
0093           n = "DDDBDetConditionDump";
0094         else if ( flg == 4 )
0095           n = "DDDBDetectorDump";
0096         else if ( flg == 5 )
0097           n = "DetElementConditionDump";
0098         else if ( flg == 6 )
0099           n = "DDDBDetectorDumpAll";
0100         return n;
0101       }
0102     };
0103     /// Callback object to print selective information
0104     /**
0105      *  \author  M.Frank
0106      *  \version 1.0
0107      *  \date    01/04/2014
0108      */
0109     struct DumpActor {
0110       struct Counters  final {
0111         long totConditions = 0;
0112         long numConditions = 0;
0113         long numAlignments = 0;
0114         long numDetElements = 0;
0115         long numDetAlignmentKeys = 0;
0116         long numDetConditionKeys = 0;
0117         long numNoCatalogs = 0;
0118         long numDetPlacements = 0;
0119         Counters() = default;
0120         ~Counters() = default;
0121         void reset() {
0122           totConditions=numConditions=numAlignments=numNoCatalogs=0;
0123           numDetElements=numDetConditionKeys=numDetAlignmentKeys=0;
0124           numDetPlacements=0;
0125         }
0126       };
0127 
0128       /// Container with all known conditions
0129       vector<pair<int,Condition> > m_allConditions;
0130       shared_ptr<ConditionsSlice>  m_slice;
0131       ConditionsManager            m_manager;
0132       ConditionsPrinter            m_detElementPrinter;
0133       ConditionsPrinter            m_catalogPrinter;
0134       ConditionsPrinter            m_alignPrinter;
0135       Counters                     m_counters;
0136       int                          m_flag;
0137       bool                         m_sensitivesOnly;
0138       bool                         m_dumpConditions;
0139       string                       m_name;
0140       Detector&                        m_detDesc;
0141 
0142       /// Standard constructor
0143       DumpActor(Detector& l, int flg, bool sens, bool dmp)
0144         : m_detElementPrinter(0,"DDDBDetectors"), m_catalogPrinter(0,"DDDBConditions"),
0145           m_alignPrinter(0,"DDDBAlignments"),
0146           m_flag(flg), m_sensitivesOnly(sens), m_dumpConditions(dmp), m_detDesc(l)
0147       {
0148         m_name = naming::get(flg);
0149         m_manager = ConditionsManager::from(m_detDesc);
0150         m_slice.reset(new ConditionsSlice(m_manager,shared_ptr<ConditionsContent>(new ConditionsContent())));
0151         m_detElementPrinter.printLevel   = s_PrintLevel;
0152         m_detElementPrinter.name         = "DetElement-Info";
0153         m_catalogPrinter.printLevel      = s_PrintLevel;
0154         m_catalogPrinter.name            = "Cat.condition";
0155         m_alignPrinter.printLevel        = s_PrintLevel;
0156         m_alignPrinter.name              = "Cat.alignment";
0157         m_alignPrinter.lineLength        = 80;
0158       }
0159 
0160       /// Standard destructor
0161       ~DumpActor()  {
0162         printout(INFO,m_name,"++ DDDB: Number of DetElements in the geometry: %8ld",m_counters.numDetElements);
0163         printout(INFO,m_name,"++ DDDB: Number of DetElement placements:       %8ld",m_counters.numDetPlacements);
0164         printout(INFO,m_name,"++ DDDB: Number of DetElement condition keys:   %8ld",m_counters.numDetConditionKeys);
0165         printout(INFO,m_name,"++ DDDB: Number of DetElement alignment keys:   %8ld",m_counters.numDetAlignmentKeys);
0166         if ( m_flag > 1 )  {
0167           printout(INFO,m_name,"++ DDDB: Number of DetElements without catalog: %8ld",m_counters.numNoCatalogs);
0168           printout(INFO,m_name,"++ DDDB: Number of attached conditions:         %8ld",m_counters.numConditions);
0169           printout(INFO,m_name,"++ DDDB: Number of attached alignments:         %8ld",m_counters.numAlignments);
0170           if ( m_flag > 2 )  {
0171             printout(INFO,m_name,"++ DDDB: Total number of parameters:            %8ld",
0172                      m_catalogPrinter.numParam+m_alignPrinter.numParam);
0173             printout(INFO,m_name,"++ DDDB: Total number of DetElement parameters: %8ld",m_detElementPrinter.numParam);
0174             printout(INFO,m_name,"++ DDDB: Total number of conditions:            %8ld",m_counters.totConditions);
0175           }
0176         }
0177         printout(INFO,m_name,"*********************************************************************************");
0178       }
0179 
0180       /// Initialization
0181       DumpActor& init()  {
0182         RangeConditions rc;
0183         const IOVType*  iov_typ  = m_manager.registerIOVType(0,"epoch").second;
0184         IOV iov(iov_typ);
0185         if ( 0 == iov_typ )  {
0186           except(m_name,"++ Unknown IOV type 'epoch' supplied.");
0187         }
0188         m_counters.reset();
0189         m_allConditions.clear();
0190         Operators::collectAllConditions(m_detDesc, rc);
0191         iov.reset().invert();
0192         iov.iovType = 0;
0193         for ( Condition cond : rc )   {
0194           m_allConditions.push_back(make_pair(0,cond));
0195           if ( !iov.iovType ) iov = cond.iov();
0196           else  iov.iov_intersection(cond.iov());
0197         }
0198         iov.set(iov.keyData.first);
0199 
0200         IOV req_iov(iov.iovType, iov.keyData.first);
0201 
0202         cond::fill_content(m_manager,*m_slice->content,*iov_typ);
0203         ConditionsManager::Result r = m_manager.prepare(req_iov,*m_slice);
0204         // Now compute the tranformation matrices
0205         printout(INFO,m_name,"Prepare: Total %ld conditions (S:%ld,L:%ld,C:%ld,M:%ld) of IOV %s",
0206                  r.total(), r.selected, r.loaded, r.computed, r.missing,
0207                  req_iov.str().c_str());
0208 
0209         if ( m_dumpConditions )   {
0210           printout(INFO,m_name,"**************** DDDB Detector dump: ALL Conditions *****************************");
0211           for(Condition cond : rc ) m_detElementPrinter(cond);
0212           printout(INFO,m_name,"*********************************************************************************");
0213         }
0214         return *this;
0215       }
0216 
0217       /// __________________________________________________________________________________
0218       RangeConditions findCond(const string& match)   {
0219         RangeConditions result;
0220         if ( !match.empty() )  {
0221           for ( auto& ic : m_allConditions )  {
0222             Condition cond = ic.second;
0223             size_t idx = cond->value.find(match);
0224             if ( idx == 0 )  {
0225               if (cond->value.length() == match.length() )   {
0226                 ic.first++;
0227                 result.push_back(cond);
0228               }
0229               else if ( cond->value[match.length()] == '/' )  {
0230                 size_t idq = cond->value.find('/',match.length()+1);
0231                 if ( idq == string::npos )  {
0232                   ic.first++;
0233                   result.push_back(cond);
0234                 }
0235               }
0236             }
0237           }
0238         }
0239         return result;
0240       }
0241       /// __________________________________________________________________________________
0242       void printDetElement(int level, DetElement de,
0243                            bool with_placement = false,
0244                            bool with_keys      = false,
0245                            bool with_values    = false)
0246       {
0247         char fmt[128];
0248         const DetElement::Children& c = de.children();
0249         ++m_counters.numDetElements;
0250         ::sprintf(fmt,"%03d %%-%ds Detector: %%s #Dau:%%d VolID:%%p Place:%%p",level+1,2*level+1);
0251         printout(s_PrintLevel, m_detElementPrinter.name, fmt, "", de.path().c_str(), int(c.size()),
0252                  (void*)de.volumeID(), (void*)de.placement().ptr());
0253         if ( de.placement().isValid() ) {
0254           ++m_counters.numDetPlacements;
0255         }
0256         std::vector<Condition> conditions;
0257         conditionsCollector(*m_slice,conditions)(de);
0258         m_counters.numDetConditionKeys += conditions.size();
0259 
0260         std::vector<Delta> deltas;
0261         deltaCollector(*m_slice,deltas)(de);
0262         m_counters.numDetAlignmentKeys += deltas.size();
0263         if ( with_placement )  {
0264           ::sprintf(fmt,"%03d %%-%ds Placement: %%s",level+1,2*level+3);
0265           printout(s_PrintLevel,m_detElementPrinter.name,fmt,"",de.placementPath().c_str());
0266         }
0267         if ( (with_keys || with_values) && !conditions.empty() )  {
0268           ::sprintf(fmt,"%03d %%-%ds Key: %%16llX -> %%s # %%s",level+1,2*level+3);
0269           for(const auto cond : conditions )  {
0270             if ( with_keys )   {
0271               printout(s_PrintLevel,m_detElementPrinter.name,fmt,"",cond.key(), de.path().c_str(), cond.name());
0272             }
0273             if ( with_values )   {
0274               m_detElementPrinter(cond);
0275             }
0276           }
0277         }
0278       }
0279       /// __________________________________________________________________________________
0280       void printCatalog_ConditionInfo(int level, DetElement de, bool with_elements=false)   {
0281         DDDBCatalog* cat = de.extension<DDDBCatalog>();
0282         if ( cat && !cat->conditioninfo.empty() )   {
0283           char fmt[128];
0284           ++m_counters.numConditions;
0285           ::sprintf(fmt,"%03d %%-%ds Cond:%%-20s -> %%s",level+1,2*level+3);
0286           for(const auto& i : cat->conditioninfo )  {
0287             const string& cond_name = i.second;
0288             if ( with_elements )  {
0289               RangeConditions rc = findCond(cond_name);
0290               printout(s_PrintLevel,m_catalogPrinter.name,fmt,"",i.first.c_str(), 
0291                        rc.empty() ? (cond_name+"  !!!UNRESOLVED!!!").c_str() : cond_name.c_str());
0292               for(Condition cond : rc ) m_catalogPrinter(cond);
0293               continue;
0294             }
0295             printout(s_PrintLevel,m_catalogPrinter.name,fmt,"",i.first.c_str(),cond_name.c_str());
0296           }
0297         }
0298       }
0299       /// __________________________________________________________________________________
0300       void printCatalog_Alignment(int level, DDDBCatalog* cat, bool with_values=false)   {
0301         if ( cat && !cat->condition.empty() )  {
0302           char fmt[128];
0303           ::sprintf(fmt,"%03d %%-%ds %%-20s -> %%s",level+1,2*level+3);
0304           ++m_counters.numAlignments;
0305           if ( with_values )  {
0306             RangeConditions rc = findCond(cat->condition);
0307             printout(s_PrintLevel,m_alignPrinter.name,fmt,"","Alignment:", 
0308                      rc.empty() ? (cat->condition+"  !!!UNRESOLVED!!!").c_str() : cat->condition.c_str());
0309             for(const auto& cond : rc) m_alignPrinter(cond);
0310             return;
0311           }
0312           printout(s_PrintLevel,m_alignPrinter.name,fmt,"","Alignment:",cat->condition.c_str());
0313         }
0314       }
0315       /// __________________________________________________________________________________
0316       long dump(DetElement de, int level)  {
0317         char fmt[64], text[512];
0318         DDDBCatalog* cat = 0;
0319         const DetElement::Children& c = de.children();
0320         ::snprintf(fmt,sizeof(fmt),"%%-%ds-> ",2*level+5);
0321         ::snprintf(text,sizeof(text),fmt,"");
0322         m_detElementPrinter.setPrefix(text);
0323         try  {
0324           if ( !m_sensitivesOnly || 0 != de.volumeID() )  {
0325             switch(m_flag)  {
0326             case 0:
0327               printDetElement(level, de, false, false);
0328               break;
0329             case 1:
0330               printDetElement(level, de, false, false);
0331               cat = de.extension<DDDBCatalog>();
0332               printCatalog_Alignment(level, cat, false);
0333               printCatalog_ConditionInfo(level, de, false);
0334               break;
0335             case 2:
0336               printDetElement(level, de, true, true);
0337               cat = de.extension<DDDBCatalog>();
0338               printCatalog_Alignment(level, cat, false);
0339               printCatalog_ConditionInfo(level, de, false);
0340               break;
0341             case 3:
0342               printDetElement(level, de, false, false);
0343               cat = de.extension<DDDBCatalog>();
0344               printCatalog_Alignment(level, cat, true);
0345               break;
0346             case 4:
0347               printDetElement(level, de, true, true);
0348               cat = de.extension<DDDBCatalog>();
0349               printCatalog_Alignment(level, cat, true);
0350               printCatalog_ConditionInfo(level, de, true);
0351               break;
0352             case 5:
0353               printDetElement(level, de, true, true, true);
0354               break;
0355             case 6:
0356               printDetElement(level, de, true, true, true);
0357               cat = de.extension<DDDBCatalog>();
0358               printCatalog_Alignment(level, cat, true);
0359               printCatalog_ConditionInfo(level, de, true);
0360               break;
0361             default:
0362               break;
0363             }
0364           }
0365         }
0366         catch(const exception& e)  {
0367           ::sprintf(fmt,"%03d %%-%ds WARNING from: %%s %%-20s %%s",level+1,2*level+3);
0368           printout(INFO, m_name, fmt, "", de.path().c_str(), "[NO CATALOG availible]",e.what());
0369           ++m_counters.numNoCatalogs;
0370         }
0371         catch(...)  {
0372           ::sprintf(fmt,"%03d %%-%ds WARNING from: %%s %%-20s",level+1,2*level+3);
0373           printout(INFO, m_name, fmt, "", de.path().c_str(), "[NO CATALOG availible]");
0374           ++m_counters.numNoCatalogs;
0375         }
0376         for (const auto& i : c)
0377           dump(i.second,level+1);
0378         return 1;
0379       }
0380     };
0381     bool dump_sensitive_only = false, dump_all_cond = false;
0382     for(int i=0; i<argc; ++i)  {
0383       if ( ::strcmp(argv[i],"-sensitive")==0 )   { dump_sensitive_only = true; }
0384       else if ( ::strcmp(argv[i],"-dump")==0 )   { dump_all_cond = true;       }
0385       else if ( ::strcmp(argv[i],"-print")==0 )  {
0386         s_PrintLevel = dd4hep::printLevel(argv[++i]);
0387         printout(INFO,"DDDB","Setting print level for %s to %s [%d]",__FILE__,argv[i-1],s_PrintLevel);
0388       }
0389       else if ( ::strcmp(argv[i],"--help")==0 )      {
0390         printout(INFO,"Plugin-Help","Usage: DDDBDetectorDump --opt [--opt]            ");
0391         printout(INFO,"Plugin-Help","  -sensitive          Only use sensitive elements");
0392         printout(INFO,"Plugin-Help","  -dump               Print conditions inventory ");
0393         printout(INFO,"Plugin-Help","  -print <value>      Printlevel for output      ");
0394         printout(INFO,"Plugin-Help","  -help               Print this help message    ");
0395         return 0;
0396       }
0397     }
0398     DumpActor actor(description, flag, dump_sensitive_only, dump_all_cond);
0399     printout(INFO,actor.m_name,"**************** DDDB Detector dump *****************************");
0400     return actor.init().dump(description.world(), 0);
0401   }
0402   
0403   template <int flag> long dump_detelement_tree(Detector& description, int argc, char** argv)
0404   {  return dump_det_tree(description,flag,argc,argv);    }
0405 } /* End anonymous namespace  */
0406 
0407 DECLARE_APPLY(DDDB_DetectorDump,dump_detelement_tree<0>)
0408 DECLARE_APPLY(DDDB_DetectorVolumeDump,dump_detelement_tree<1>)
0409 DECLARE_APPLY(DDDB_DetectorConditionKeysDump,dump_detelement_tree<2>)
0410 DECLARE_APPLY(DDDB_DetectorAlignmentDump,dump_detelement_tree<3>)
0411 DECLARE_APPLY(DDDB_DetectorConditionDump,dump_detelement_tree<4>)
0412 DECLARE_APPLY(DDDB_DetElementConditionDump,dump_detelement_tree<5>)
0413 DECLARE_APPLY(DDDB_DetectorDumpAll,dump_detelement_tree<6>)
0414 //==========================================================================