Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 09:20:18

0001 //==============================================================================
0002 //  AIDA Detector description implementation for LHCb
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   Markus Frank
0011 //  \date     2018-03-08
0012 //  \version  1.0
0013 //
0014 //==============================================================================
0015 
0016 // Framework include files
0017 #include "Detector/DeVeloConditionCalls.h"
0018 #include "DD4hep/DetectorProcessor.h"
0019 #include "DDDB/DDDBConversion.h"
0020 
0021 using namespace std;
0022 using namespace gaudi;
0023 namespace {
0024   void _cond_error(const char* func, int line, const std::exception& e)   {
0025     dd4hep::printout(dd4hep::ERROR,"Cond-Callback",
0026              "%s:%d Load Condition failed: %s",func,line,e.what());
0027   }
0028 }
0029 #define LOAD_COND(expr) try{ expr; }catch(const std::exception& e) {_cond_error(__func__,__LINE__,e); throw; }
0030 
0031 
0032 /// Interface to client Callback in order to update the condition
0033 dd4hep::Condition DeVeloStaticConditionCall::operator()(const ConditionKey& key, Context& context)  {
0034   VeloUpdateContext* ctxt = dynamic_cast<VeloUpdateContext*>(context.parameter);
0035   KeyMaker   km(key.hash);
0036   auto       ide = ctxt->detectors.find(km.values.det_key);
0037   auto*      cat = (*ide).second.second;
0038   DetElement det = (*ide).second.first;
0039   DeStatic s;
0040   if ( cat->classID == DeVeloSensor::classID() || cat->classID == 1008102 || cat->classID == 1008103 )      // DeVeloSensor
0041     s = DeStatic(new detail::DeVeloSensorStaticObject());
0042   else if ( cat->classID == DeVelo::classID()  )     // DeVelo Velo main element
0043     s = DeStatic(new detail::DeVeloStaticObject());
0044   else                                               // All other in the hierarchy
0045     s = DeStatic(new detail::DeVeloGenericStaticObject());
0046   return s->fill_info(det, cat);
0047 }
0048 
0049 /// Interface to client callback for resolving references or to use data from other conditions
0050 void DeVeloStaticConditionCall::resolve(Condition c, Context& context)    {
0051   DeStatic s(c);
0052   if ( s->clsID == DeVelo::classID() )  {   // Velo main element
0053     DeVeloStatic vp = s;
0054     DeVeloSensorStatic sens;
0055     DeVeloGenericStatic side, module, support;
0056     vector<pair<DetElement, int> > elts;
0057     dd4hep::DetectorScanner(dd4hep::detElementsCollector(elts), s->detector);
0058 
0059     vp->de_user |= DeVeloFlags::MAIN;
0060     vp->sides.resize(2);
0061     
0062     for ( const auto& i : elts )   {
0063       DetElement de = i.first;
0064       KeyMaker   key(de.key(), Keys::staticKey);
0065       DeStatic   cond; LOAD_COND(cond = context.condition(key.hash));
0066       const string& path = de.path();
0067       bool   left  = path.find("/VeloLeft/Module") != string::npos   || path.find("/VeloLeft")  == path.length()-9;
0068       bool   right = path.find("/VeloRight/Module") != string::npos  || path.find("/VeloRight") == path.length()-10;
0069       size_t sideNo  = left ? DeVeloFlags::LEFT : right ? DeVeloFlags::RIGHT : 99999;
0070 
0071       printout(DEBUG,"DeVelo"," %03d DetElement: %s",i.second, de.path().c_str());
0072       if ( left || right )   {
0073         switch( i.second )   {
0074         case 0:  // Self!
0075           break;
0076         case 1:
0077           side = cond;
0078           side->parent = vp.access();
0079           side->de_user |= DeVeloFlags::SIDE;
0080           vp->sides[sideNo] = side;
0081           printout(DEBUG,"DeVeloStatic","Add Side[%03ld]:    %s",vp->sides.size()-1,path.c_str());
0082           break;
0083         case 2:
0084           module = cond;
0085           module->parent = side.access();
0086           module->de_user |= DeVeloFlags::MODULE;
0087           side->children.push_back(module.ptr());
0088           vp->modules.push_back(module);
0089           printout(DEBUG,"DeVeloStatic","Add Module[%03ld]:  %s",vp->modules.size()-1,path.c_str());
0090           break;
0091         case 3:
0092           support = cond;
0093           support->parent = module.access();
0094           support->de_user |= DeVeloFlags::SUPPORT;
0095           vp->supports.push_back(support);
0096           module->children.push_back(support.ptr());
0097           printout(DEBUG,"DeVeloStatic","Add Support[%03ld]: %s",vp->supports.size()-1,path.c_str());
0098           break;
0099         case 4:
0100           sens = cond;
0101           if ( !sens.isValid() )   {
0102             except("DeVeloStatic","Problem: Unknown sensor type or invalid object encountered!");
0103           }
0104           sens->parent = support.access();
0105           vp->sensors[DeVeloFlags::ALL].push_back(sens);
0106           vp->sensors[sideNo].push_back(sens);
0107           // ---- Contiguous array of sensors
0108           vp->rSensors[sideNo].push_back(sens);
0109           vp->phiSensors[sideNo].push_back(sens);
0110           vp->puSensors[sideNo].push_back(sens);
0111           vp->rphiSensors[sideNo].push_back(sens);
0112           support->sensors.push_back(sens);
0113           module->sensors.push_back(sens);
0114           side->sensors.push_back(sens);
0115           printout(DEBUG,"DeVeloStatic","Add Sensor[%03ld]:  %s",long(sens->sensorNumber),path.c_str());
0116           break;
0117         default:
0118           break;
0119         }
0120       }
0121       else   {
0122         printout(DEBUG,"DeVeloStatic","Aux.DetElmenet:   %s",path.c_str());
0123       }
0124     }
0125   }
0126   s->initialize();
0127 }
0128 
0129 /// Interface to client Callback in order to update the condition
0130 dd4hep::Condition DeVeloIOVConditionCall::operator()(const ConditionKey&, Context&)   {
0131   DeIOV iov;
0132   if ( catalog->classID == DeVeloSensor::classID() || catalog->classID == 1008102 || catalog->classID == 1008103 )     // DeVeloSensor
0133     iov = DeIOV(new detail::DeVeloSensorObject());
0134   else if ( catalog->classID == DeVelo::classID()  )     // DeVelo Velo main element
0135     iov = DeIOV(new detail::DeVeloObject());
0136   else                                     // All other in the hierarchy
0137     iov = DeIOV(new detail::DeVeloGenericObject());
0138   return iov->fill_info(detector, catalog);
0139 }
0140 
0141 /// Interface to client callback for resolving references or to use data from other conditions
0142 void DeVeloIOVConditionCall::resolve(Condition cond, Context& context)   {
0143   DeIOV iov(cond);
0144   Condition::detkey_type det_key = iov->detector.key();
0145   KeyMaker kalign(det_key,dd4hep::align::Keys::alignmentKey);
0146   KeyMaker kstatic(det_key,Keys::staticKey);
0147 
0148   /// Check that the alignments are computed. We need them here!
0149   if ( !velo_context->alignments_done.isValid() )  {
0150     velo_context->alignments_done = context.condition(Keys::alignmentsComputedKey);
0151   }
0152   vector<Condition> conds = context.conditions(det_key);
0153   LOAD_COND(iov->de_static = context.condition(kstatic.hash));
0154   LOAD_COND(iov->detectorAlignment = context.condition(kalign.hash));
0155   for ( Condition c : conds )
0156     LOAD_COND(iov->conditions.insert(make_pair(c.item_key(),c)));
0157   LOAD_COND(iov->initialize());
0158 }
0159 
0160 namespace {
0161   /// Resolve generic parent-sensor dependencies
0162   void add_sensors( vector<DeVeloSensor>& cont,
0163                     const vector<DeVeloSensorStatic>& src,
0164                      map<DeVeloSensorStatic,DeVeloSensor>& mapping)    {
0165     if ( !src.empty() )  {
0166       cont.reserve(src.size());
0167       for (DeVeloSensorStatic i : src)
0168         cont.push_back(mapping[i]);
0169     }
0170   }
0171 
0172   /// Resolve generic parent-sensor dependencies
0173   void add_sensors( DeVeloGeneric                    gen,
0174                     DeVeloGenericStatic              src,
0175                     map<DeVeloSensorStatic,DeVeloSensor>& mapping,
0176                     dd4hep::cond::ConditionUpdateContext& context)
0177   {
0178     if ( !gen->sensors.empty() )  {
0179       gen->sensors.reserve(src->sensors.size());
0180       for (DeVeloSensorStatic i : src->sensors)   {
0181         DeVeloSensor sens = mapping[i];
0182         if ( !sens.isValid() )  {
0183           except("DeVelo","Problem: Invalid sensor refernce encountered.");
0184         }
0185         gen->sensors.push_back(sens);
0186       }
0187     }
0188     for (detail::DeVeloGenericStaticObject* i : src->children)   {
0189       dd4hep::ConditionKey::KeyMaker key(i->detector.key(), Keys::deKey);
0190       DeVeloGeneric child; LOAD_COND(child = context.condition(key.hash));
0191       gen->children.push_back(child.ptr());
0192       add_sensors(child, DeVeloGenericStatic(i), mapping, context);
0193     }
0194   }
0195 }
0196 
0197 /// Interface to client callback for resolving references or to use data from other conditions
0198 void DeVeloConditionCall::resolve(Condition cond, Context& context)  {
0199   DeIOV iov(cond);
0200   DeVelo  vp(cond);
0201   DeVeloIOVConditionCall::resolve(cond, context);
0202   DeVeloStatic s = vp.access()->vp_static;
0203   map<DeVeloSensorStatic,DeVeloSensor>   sensorMapping;
0204   
0205   sensorMapping[DeVeloSensorStatic()] = DeVeloSensor();
0206   for ( const auto& i : s->sensors[DeVeloFlags::ALL] )   {
0207     if ( i.isValid() )   {
0208       KeyMaker     key(i->detector.key(), Keys::deKey);
0209       DeVeloSensor sens; LOAD_COND(sens = context.condition(key.hash));
0210       if ( !sens.isValid() )  {
0211         except("DeVelo","Problem Mapping %p ---> %p [%s]",
0212                (void*)i.ptr(), (void*)sens.ptr(), i->detector.path().c_str());
0213       }
0214       sensorMapping[i] = sens;
0215       continue;
0216     }
0217   }
0218 
0219   for(size_t iside = 0; iside<2; ++iside)   {
0220     vp->sensors[iside].clear();
0221     vp->rSensors[iside].clear();
0222     vp->phiSensors[iside].clear();
0223     vp->rphiSensors[iside].clear();
0224     vp->puSensors[iside].clear();
0225     add_sensors(vp->sensors[iside],     s->sensors[iside],     sensorMapping);
0226     add_sensors(vp->rSensors[iside],    s->rSensors[iside],    sensorMapping);
0227     add_sensors(vp->phiSensors[iside],  s->phiSensors[iside],  sensorMapping);
0228     add_sensors(vp->rphiSensors[iside], s->rphiSensors[iside], sensorMapping);
0229     add_sensors(vp->puSensors[iside],   s->puSensors[iside],   sensorMapping);
0230   }
0231   for ( auto side : s->sides )   {
0232     dd4hep::ConditionKey::KeyMaker key(side->detector.key(), Keys::deKey);
0233     DeVeloGeneric child; LOAD_COND(child = context.condition(key.hash));
0234     vp->sides.push_back(child);
0235     add_sensors(child, side, sensorMapping, context);
0236   }
0237 }