Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:13:40

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 // Framework include files
0015 #include <DD4hep/Detector.h>
0016 #include <DD4hep/Memory.h>
0017 #include <DD4hep/DD4hepUI.h>
0018 #include <DD4hep/Factories.h>
0019 #include <DD4hep/Printout.h>
0020 #include <DD4hep/DetectorData.h>
0021 #include <DD4hep/DetectorTools.h>
0022 
0023 // ROOT includes
0024 #include <TInterpreter.h>
0025 #include <TGeoElement.h>
0026 #include <TGeoManager.h>
0027 #include <TFile.h>
0028 #include <TUri.h>
0029 
0030 using namespace std;
0031 using namespace dd4hep;
0032 
0033 /// ROOT geometry dump plugin
0034 /**
0035  *  Factory: DD4hep_PlainROOTDump
0036  *
0037  *  \author  M.Frank
0038  *  \version 1.0
0039  *  \date    01/07/2014
0040  */
0041 static long plain_root_dump(Detector& description, int argc, char** argv) {
0042   struct TGeoManip  {
0043     DetectorData* detector;
0044     size_t        num_nodes = 0;
0045     size_t        num_volume_patches = 0;
0046     size_t        num_placement_patches = 0;
0047     int           max_level = 9999;
0048     bool          import = false;
0049     PrintLevel    printLevel = DEBUG;
0050     TGeoManip(DetectorData* dsc, int mx, bool imp, PrintLevel p)
0051       : detector(dsc), max_level(mx), import(imp), printLevel(p)
0052     {
0053     }
0054     ~TGeoManip()   {
0055       printout(INFO,  "PlainROOTDump","+++ Scanned a total of %9ld NODES",num_nodes);
0056       if ( import )   {
0057         printout(INFO,"PlainROOTDump","+++ Scanned a total of %9ld VOLUMES",num_volume_patches);
0058         printout(INFO,"PlainROOTDump","+++ Scanned a total of %9ld PLACEMENTS",num_placement_patches);
0059       }
0060     }
0061     void operator()(int lvl, TGeoNode* n)    {
0062       char fmt[255];
0063       int  npatch = 0;
0064       TGeoVolume* v = n->GetVolume();
0065       bool v_ext = false, a_ext = false, p_ext = false;
0066       if ( import )  {
0067         if ( !v->GetUserExtension() )   {
0068           if ( v->IsA() == TGeoVolume::Class() )  {
0069             v->SetUserExtension(new Volume::Object());
0070             v_ext = true;
0071           }
0072           else   {
0073             v->SetUserExtension(new Assembly::Object());
0074             a_ext = true;
0075           }
0076           ++npatch;
0077           ++num_volume_patches;
0078         }
0079         if ( !n->GetUserExtension() )   {
0080           n->SetUserExtension(new PlacedVolume::Object());
0081           ++num_placement_patches;
0082           p_ext = true;
0083           ++npatch;
0084         }
0085         if ( lvl == 0 )   {
0086         }
0087       }
0088       ++num_nodes;
0089       if ( lvl <= max_level )  {
0090         if ( !import || (import && npatch > 0) )  {
0091           ::snprintf(fmt,sizeof(fmt),"%-5d %%-%ds  %%s  NDau:%%d Ext:%%p  Vol:%%s Mother:%%s Ext:%%p Mat:%%s",lvl,lvl);
0092           TGeoVolume* mother = n->GetMotherVolume();
0093           printout(printLevel,"PlainROOTDump",fmt,"",
0094                    n->GetName(), n->GetNdaughters(), n->GetUserExtension(),
0095                    v->GetName(), mother ? mother->GetName() : "-----",
0096                    v->GetUserExtension(), v->GetMedium()->GetName());
0097           if ( import )  {
0098             if ( v_ext )   {
0099               ::snprintf(fmt,sizeof(fmt),"%-5d %%-%ds     -->  Adding VOLUME extension object",lvl,lvl);
0100               printout(printLevel,"PlainROOTDump",fmt,"");
0101             }
0102             else if ( a_ext )  {
0103               ::snprintf(fmt,sizeof(fmt),"%-5d %%-%ds     -->  Adding VOLUME ASSEMBLY extension object",lvl,lvl);
0104               printout(printLevel,"PlainROOTDump",fmt,"");
0105             }
0106             else if ( p_ext )   {
0107               ::snprintf(fmt,sizeof(fmt),"%-5d %%-%ds     -->  Adding PLACEMENT extension object",lvl,lvl);
0108               printout(printLevel,"PlainROOTDump",fmt,"");
0109             }
0110             if ( lvl == 0 )   {
0111             }
0112           }
0113         }
0114       }
0115       for (Int_t idau = 0, ndau = n->GetNdaughters(); idau < ndau; ++idau)  {
0116         TGeoNode*   daughter = n->GetDaughter(idau);
0117         this->operator()(lvl+1, daughter);
0118       }
0119     }
0120   };
0121   if ( argc > 0 )   {
0122     int    level = 99999;
0123     PrintLevel prt = DEBUG;
0124     bool   do_import = false;
0125     string input, in_obj = "Geometry", air, vacuum;
0126     for(int i = 0; i < argc && argv[i]; ++i)  {
0127       if ( 0 == ::strncmp("-input",argv[i],5)       && (i+1)<argc )
0128         input = argv[++i];
0129       else if ( 0 == ::strncmp("-object",argv[i],5) && (i+1)<argc )
0130         in_obj = argv[++i];
0131       else if ( 0 == ::strncmp("-air",argv[i],5)    && (i+1)<argc )
0132         air = argv[++i];
0133       else if ( 0 == ::strncmp("-vacuum",argv[i],5) && (i+1)<argc )
0134         vacuum = argv[++i];
0135       else if ( 0 == ::strncmp("-level",argv[i],5)  && (i+1)<argc )
0136         level = ::atol(argv[++i]);
0137       else if ( 0 == ::strncmp("-print",argv[i],5)  && (i+1)<argc )
0138         prt = decodePrintLevel(argv[++i]);
0139       else if ( 0 == ::strncmp("-import",argv[i],5) )
0140         do_import = true;
0141       else
0142         goto Error;
0143     }
0144     if ( input.empty() || in_obj.empty() )   {
0145     Error:
0146       cout <<
0147         "Usage: -plugin <name> -arg [-arg]                                            \n"
0148         "     name:   factory name     DD4hep_PlainROOT                               \n"
0149         "     -input  <string>         Input file name.                               \n"
0150         "     -object <string>         Name of geometry object in file. Default: \"Geometry\"\n"
0151         "\tArguments given: " << arguments(argc,argv) << endl << flush;
0152       ::exit(EINVAL);
0153     }
0154     printout(INFO,"ImportROOT","+++ Read geometry from GDML file file:%s",input.c_str());
0155     DetectorData::unpatchRootStreamer(TGeoVolume::Class());
0156     DetectorData::unpatchRootStreamer(TGeoNode::Class());
0157     TFile* f = TFile::Open(input.c_str());
0158     if ( f && !f->IsZombie() )   {
0159       DetectorData* det = dynamic_cast<DetectorData*>(&description);
0160       TGeoManager* mgr = (TGeoManager*)f->Get(in_obj.c_str());
0161       if ( det && mgr )   {
0162         TGeoManip manip(det, level, do_import, prt);
0163         DetectorData::patchRootStreamer(TGeoVolume::Class());
0164         DetectorData::patchRootStreamer(TGeoNode::Class());
0165         det->m_manager = mgr;
0166         manip(0, mgr->GetTopNode());
0167         det->m_worldVol = mgr->GetTopNode()->GetVolume();
0168         if ( !air.empty() )  {
0169           description.addConstant(Constant("Air",air));
0170         }
0171         if ( !vacuum.empty() )  {
0172           description.addConstant(Constant("Vacuum",vacuum));
0173         }
0174         description.init();
0175         description.endDocument();
0176         detail::deleteObject(f);
0177         return 1;
0178       }
0179     }
0180     detail::deleteObject(f);
0181   }
0182   DetectorData::patchRootStreamer(TGeoVolume::Class());
0183   DetectorData::patchRootStreamer(TGeoNode::Class());
0184   except("ImportROOT","+++ No input file name given.");
0185   return 0;
0186 }
0187 DECLARE_APPLY(DD4hep_PlainROOT,plain_root_dump)