Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:18:03

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 "DDDB/DDDBConversion.h"
0022 #include "DD4hep/InstanceCount.h"
0023 #include "DD4hep/Primitives.h"
0024 #include "DD4hep/Printout.h"
0025 
0026 // C/C++ include files
0027 #include <cstring>
0028 #include <map>
0029 
0030 #define CHECK_OBJECT(obj)  if ( !obj )  {                             \
0031     printout(ERROR,"dddb_print",                                      \
0032              "++ Attempt to print invalid (null) object of type %s.",   \
0033              typeName(typeid(obj)).c_str());                          \
0034     return;                                                           \
0035   }
0036 
0037 /// Namespace for the AIDA detector description toolkit
0038 namespace dd4hep  {
0039 
0040   using namespace std;
0041   using namespace DDDB;
0042 
0043   /// Default constructor
0044   dddb::dddb() : world{0E0,0E0,0E0}, top(0), structure(0), geometry(0)  {
0045     InstanceCount::increment(this);
0046   }
0047 
0048   /// Default destructor
0049   dddb::~dddb()   {
0050     // need to release heare all allocated resources.
0051     detail::releaseObjects(isotopes);
0052     detail::releaseObjects(elements);
0053     detail::releaseObjects(elementPaths);
0054 
0055     detail::releaseObjects(materials);
0056     detail::releaseObjects(materialPaths);
0057 
0058     detail::releaseObjects(shapes);
0059 
0060     detail::releaseObjects(volumes);
0061     detail::releaseObjects(volumePaths);
0062 
0063     detail::releaseObjects(placements);
0064     detail::releaseObjects(placementPaths);
0065 
0066     detail::releaseObjects(tabproperties);
0067     detail::releaseObjects(tabpropertyPaths);
0068 
0069     conditions.clear();
0070     conditionPaths.clear();
0071     //detail::releaseObjects(conditions);
0072     //detail::releaseObjects(conditionPaths);
0073     detail::releaseObjects(catalogs);
0074     detail::releaseObjects(catalogPaths);
0075     detail::releaseObjects(documents);
0076     printout(DEBUG,"dddb","++ All intermediate objects deleted!");
0077     InstanceCount::decrement(this);
0078   }
0079 
0080   /// Initializing constructor
0081   DDDBNamed::DDDBNamed(const std::string& c)
0082     : name(c), id(), document(0), refCount(0)
0083   {
0084   }
0085 
0086   /// Copy constructor
0087   DDDBNamed::DDDBNamed(const DDDBNamed& c)
0088     : name(c.name), id(c.id), document(c.document), refCount(0)
0089   {
0090     if ( document ) document->addRef();
0091   }
0092 
0093   /// Default destructor
0094   DDDBNamed::~DDDBNamed() {
0095     if ( document ) document->release();
0096   }
0097 
0098   /// Assignment operator
0099   DDDBNamed& DDDBNamed::operator=(const DDDBNamed& c) {
0100     if ( this != &c )  {
0101       setDocument(c.document);
0102       name = c.name;
0103       id = c.id;
0104     }
0105     return *this;
0106   }
0107 
0108   /// Assign document
0109   void DDDBNamed::setDocument(DDDBDocument* doc)   {
0110     if ( doc ) doc->addRef();
0111     if ( document ) document->release();
0112     document = doc;
0113   }
0114 
0115   /// Default constructor
0116   DDDBDocument::DDDBDocument() : DDDBNamed(), context()  {
0117     InstanceCount::increment(this);
0118   }
0119 
0120   /// Default destructor
0121   DDDBDocument::~DDDBDocument()   {
0122     //printout(INFO,"Document","Delete doc %s",c_id());
0123     InstanceCount::decrement(this);
0124   }
0125 
0126   /// Default constructor
0127   DDDBIsotope::DDDBIsotope() : DDDBNamed() {
0128     InstanceCount::increment(this);
0129   }
0130 
0131   /// Default destructor
0132   DDDBIsotope::~DDDBIsotope()   {
0133     InstanceCount::decrement(this);
0134   }
0135 
0136   /// Default constructor
0137   DDDBElement::DDDBElement() : DDDBNamed(), density(0), ionization(0), state(UNKNOWN) {
0138     InstanceCount::increment(this);
0139   }
0140 
0141   /// Copy constructor
0142   DDDBElement::DDDBElement(const DDDBElement& e) 
0143     : DDDBNamed(e), isotopes(e.isotopes), path(e.path), symbol(e.symbol), 
0144       atom(e.atom), density(e.density), 
0145       ionization(e.ionization), state(e.state)
0146   {
0147     InstanceCount::increment(this);
0148   }
0149 
0150   /// Default destructor
0151   DDDBElement::~DDDBElement()  {
0152     InstanceCount::decrement(this);
0153   }
0154 
0155   /// Default constructor
0156   DDDBMaterial::DDDBMaterial() : density(0), pressure(-1), temperature(-1), radlen(0), lambda(0) {
0157     InstanceCount::increment(this);
0158   }
0159 
0160   /// Default destructor
0161   DDDBMaterial::~DDDBMaterial()  {
0162     InstanceCount::decrement(this);
0163   }
0164 
0165   /// Default constructor
0166   DDDBLogVol::DDDBLogVol() : DDDBNamed(), material(), shape(), physvols() {
0167     InstanceCount::increment(this);
0168   }
0169 
0170   /// Default destructor
0171   DDDBLogVol::~DDDBLogVol()  {
0172     InstanceCount::decrement(this);
0173   }
0174 
0175   /// Default constructor
0176   DDDBPhysVol::DDDBPhysVol() : type(PHYSVOL_REGULAR), logvol(), path(), trafo() {
0177     InstanceCount::increment(this);
0178   }
0179 
0180   /// Copy constructor
0181   DDDBPhysVol::DDDBPhysVol(const DDDBPhysVol& c) 
0182     : DDDBNamed(c), type(c.type), logvol(c.logvol), path(c.path), trafo(c.trafo) {
0183     InstanceCount::increment(this);
0184   }
0185 
0186   /// Default destructor
0187   DDDBPhysVol::~DDDBPhysVol()  {
0188     InstanceCount::decrement(this);
0189   }
0190 
0191   /// Default constructor
0192   DDDBShape::DDDBShape() : type(0), zplanes(), boolean_ops() {
0193     ::memset(&s,0,sizeof(s));
0194     InstanceCount::increment(this);
0195   }
0196   
0197   /// Default destructor
0198   DDDBShape::~DDDBShape()   {
0199     if ( type == DDDBBooleanUnion::type() ||
0200          type == DDDBBooleanSubtraction::type() ||
0201          type == DDDBBooleanIntersection::type() )   {
0202       DDDBShape* shape = s.boolean.first;
0203       if ( shape ) delete shape;
0204       for(Operations::iterator i=boolean_ops.begin(); i!=boolean_ops.end(); ++i)
0205         delete (*i).shape;
0206       boolean_ops.clear();
0207     }
0208     InstanceCount::decrement(this);
0209   }
0210  
0211   /// Default constructor
0212   DDDBCatalog::DDDBCatalog() {
0213     InstanceCount::increment(this);
0214   }
0215 
0216   /// Extension constructor
0217   DDDBCatalog::DDDBCatalog(const DDDBCatalog& copy, const DetElement&) {
0218     *this = copy;
0219   }
0220 
0221   /// Default destructor
0222   DDDBCatalog::~DDDBCatalog()   {
0223     InstanceCount::decrement(this);
0224   }
0225 
0226 
0227   /// Default constructor
0228   DDDBTabProperty::DDDBTabProperty()    {
0229     InstanceCount::increment(this);
0230   }
0231 
0232   /// Default destructor
0233   DDDBTabProperty::~DDDBTabProperty()    {
0234     InstanceCount::decrement(this);
0235   }
0236 
0237   pair<const DDDBCatalog*,string> DDDBCatalog::parent(const string& nam)  const  {
0238     const DDDBCatalog* cat = this;
0239     string rest = nam.substr(cat->path.length()+1);
0240     size_t idx = rest.find('/');
0241     string sub = rest.substr(0,idx);
0242     while( cat && idx != string::npos )  {
0243       dddb::Catalogs::const_iterator ic = cat->catalogs.find(sub);
0244       if ( ic != cat->catalogs.end() )  {
0245         rest = rest.substr(idx+1);
0246         idx = rest.find('/');
0247         sub = rest.substr(0,idx);
0248         cat = (*ic).second;
0249         continue;
0250       }
0251       cat = 0;
0252     }
0253     return make_pair(cat,sub);
0254   }
0255 
0256   template <> void dddb_print(const Position* p)   {
0257     CHECK_OBJECT(p);
0258     printout(INFO,"PosXYZ","    PosXYZ: x=%f y=%f z=%f",p->X(),p->Y(),p->Z());
0259   }
0260 
0261   template <> void dddb_print(const RotationZYX* p)   {
0262     CHECK_OBJECT(p);
0263     printout(INFO,"RotXYZ","    RotXYZ: z/phi=%f y/theta=%f x/psi=%f",p->Phi(),p->Theta(),p->Psi());
0264   }
0265 
0266   template <> void dddb_print(const Transform3D* tr)   {
0267     CHECK_OBJECT(tr);
0268     Rotation3D rot;
0269     Position   pos;
0270     tr->GetDecomposition(rot,pos);
0271     double dot_pos = pos.mag2();
0272     printout(INFO,"Transformation"," Position:%s",dot_pos>1e-10 ? "YES" : "NO");
0273     if ( dot_pos>1e-10 ) dddb_print(&pos);
0274   }
0275 
0276   template <> void dddb_print(const DDDBIsotope* i)   {
0277     CHECK_OBJECT(i);
0278     printout(INFO,"Isotope","++ %-20s A=%6.0f Z=%3.0f Density=%8.2g  id=%s",
0279              i->c_name(), i->A, i->Z, i->density,i->c_id());
0280   }
0281 
0282   template <> void dddb_print(const TGeoIsotope* i)   {
0283     CHECK_OBJECT(i);
0284     printout(INFO,"TGeoIsotope","++ %-20s N=%4d Z=%3d A=%8.2g",
0285              i->GetName(), i->GetN(), i->GetZ(), i->GetA());
0286   }
0287 
0288   template <> void dddb_print(const DDDBElement* e)   {
0289     CHECK_OBJECT(e);
0290     printout(INFO,"Element","++ %-20s A=%6.0f Z=%3.0f Density=%8g ioni:%8.2g (%d isotopes) id=%s",
0291              (e->name+"/"+e->symbol).c_str(), e->atom.A, e->atom.Zeff, 
0292              e->density, e->ionization,int(e->isotopes.size()), e->c_id());
0293   }
0294 
0295   template <> void dddb_print(const TGeoElement* e)   {
0296     CHECK_OBJECT(e);
0297     printout(INFO,"TGeoElement","++ %-20s N=%4d Z=%3d A=%8.2g",
0298              e->GetName(), e->N(), e->Z(), e->A());
0299   }
0300 
0301   template <> void dddb_print(const DDDBMaterial* m)   {
0302     CHECK_OBJECT(m);
0303     printout(INFO,"Material","++ %-20s Density=%8g P=%f T=%f %d components id:%s",
0304              ("'"+m->path+"'").c_str(), m->density, m->pressure, m->temperature, 
0305              int(m->components.size()),m->c_id());
0306   }
0307 
0308   template <> void dddb_print(const TGeoMedium* m)   {
0309     CHECK_OBJECT(m);
0310     TGeoMaterial* mat = m->GetMaterial();
0311     printout(INFO,"TGeoMedium","++ %-20s Material:%-20s A=%4.0f Z=%3.0f "
0312              "Radlen:%8.2g IntLen:%8.2g components:%d",
0313              m->GetName(), mat->GetName(), 
0314              mat->GetA(), mat->GetZ(), 
0315              mat->GetRadLen(), mat->GetIntLen(),
0316              mat->GetNelements());
0317   }
0318 
0319   template <> void dddb_print(const DDDBShape* s)   {
0320     CHECK_OBJECT(s);
0321     if (s-> type == DDDBBox::type() )  {
0322       printout(INFO,"Box","++ %3d/%-12s: x=%f y=%f z=%f",
0323                s->type,s->c_name(),
0324                s->s.box.x,s->s.box.y,s->s.box.z);
0325     }
0326     else if (s-> type == DDDBCons::type() )  {
0327       printout(INFO,"Cons","++ %3d/%-12s: z=%f innerMZ=%f innerPZ=%f outerMZ=%f outerPZ=%f",
0328                s->type,s->c_name(),s->s.cons.sizeZ,
0329                s->s.cons.innerRadiusMZ,s->s.cons.innerRadiusPZ,
0330                s->s.cons.outerRadiusMZ,s->s.cons.outerRadiusPZ);
0331     }
0332     else if (s-> type == DDDBTubs::type() )  {
0333       printout(INFO,"Tubs","++ %3d/%-12s: z=%f innerR=%f outerR=%f",
0334                s->type,s->c_name(),s->s.tubs.sizeZ,
0335                s->s.tubs.innerRadius,s->s.tubs.outerRadius);
0336     }
0337     else if (s-> type == DDDBPolycone::type() )  {
0338       printout(INFO,"Polycone","++ %3d/%-12s: %d zplanes",
0339                s->type,s->c_name(),int(s->zplanes.size()));
0340     }
0341     else if (s-> type == DDDBBooleanUnion::type() )  {
0342       printout(INFO,"union","++ %3d/%-12s: %d modifications",
0343                s->type,s->c_name(),int(s->boolean_ops.size()));
0344     }
0345     else if (s-> type == DDDBBooleanSubtraction::type() )  {
0346       printout(INFO,"subtraction","++ %3d/%-12s: %d modifications",
0347                s->type,s->c_name(),int(s->boolean_ops.size()));
0348     }
0349     else if (s-> type == DDDBBooleanIntersection::type() )  {
0350       printout(INFO,"intersection","++ %3d%-12s: %d modifications",
0351                s->type,s->path.c_str(),int(s->boolean_ops.size()));
0352     }
0353     printout(INFO,"Shape","++ %-15s  name:%s id:%s",
0354              "", s->path.c_str(), s->c_id());
0355   }
0356 
0357   template <> void dddb_print(const DDDBPhysVol* obj)   {
0358     CHECK_OBJECT(obj);
0359     printout(INFO,"PhysVol","++ %-12s: logvol:%s",
0360              obj->path.c_str(),obj->logvol.c_str());
0361     printout(INFO,"PhysVol","++ %-12s  name:%s id:%s",
0362              "",obj->c_name(),obj->c_id());
0363   }
0364 
0365   template <> void dddb_print(const DDDBLogVol* obj)   {
0366     CHECK_OBJECT(obj);
0367     printout(INFO,"LogVol", "++ %-12s: Material:%s Shape:%s",
0368              obj->path.c_str(),obj->material.c_str(),
0369              obj->shape.empty() ? "<INVALID>" : obj->shape.c_str());
0370     printout(INFO,"LogVol", "++ %-12s  name:%s id:%s",
0371              "",obj->c_name(),obj->c_id());
0372   }
0373 
0374   template <> void dddb_print(const DDDBTabProperty* obj)   {
0375     CHECK_OBJECT(obj);
0376     printout(INFO,"Detector", "++ %-12s: [%s] xunit:%s xaxis:%s yunit:%s yaxis:%s siz:%d",
0377              obj->path.c_str(), obj->type.c_str(), 
0378          obj->xunit.c_str(), obj->xaxis.c_str(),
0379          obj->yunit.c_str(), obj->yaxis.c_str(),
0380          int(obj->data.size()));
0381   }
0382 
0383   template <> void dddb_print(const DDDBCatalog* obj)   {
0384     CHECK_OBJECT(obj);
0385     printout(INFO,"Detector", "++ %-12s: [%s] %d children support:%s geo:%s",
0386              obj->path.c_str(), obj->type.c_str(), int(obj->catalogrefs.size()),
0387              obj->support.c_str(), obj->logvol.c_str());
0388     printout(INFO,"Detector", "++ %-12s  name:%s id:%s",
0389              "",obj->c_name(),obj->c_id());
0390   }
0391   template <> void dddb_print(const DDDBDocument* obj)   {
0392     CHECK_OBJECT(obj);
0393     char c_since[64], c_until[64], c_evt[64];
0394     struct tm since, until, evt;
0395     time_t t_evt   = obj->context.event_time;
0396     time_t t_since = obj->context.valid_since;
0397     time_t t_until = obj->context.valid_until;
0398 
0399     ::gmtime_r(&t_evt,&evt);
0400     ::gmtime_r(&t_since,&since);
0401     ::gmtime_r(&t_until,&until);
0402     ::strftime(c_evt,sizeof(c_evt),"%T %F",&evt);
0403     ::strftime(c_since,sizeof(c_since),"%T %F",&since);
0404     ::strftime(c_until,sizeof(c_until),"%T %F",&until);
0405 #if 0
0406     printout(INFO,"Document", "++ %8ld [%8ld - %8ld] %s",
0407          long(double(obj->context.event_time)/1e9),
0408          long(double(obj->context.valid_since)/1e9),
0409          long(double(obj->context.valid_until)/1e9),
0410          obj->c_id());
0411 #endif
0412     printout(INFO,"Document", "++ %s [%s - %s] %s",c_evt,c_since,c_until,obj->c_id());
0413   }
0414 }