Back to home page

EIC code displayed by LXR

 
 

    


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

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 // Original Author: Matevz Tadel 2009 (MultiView.C)
0012 //
0013 //==========================================================================
0014 
0015 // Framework include files
0016 #include <DDEve/View.h>
0017 #include <DDEve/Display.h>
0018 #include <DDEve/ElementList.h>
0019 #include <DDEve/Utilities.h>
0020 #include <DDEve/Annotation.h>
0021 #include <DD4hep/InstanceCount.h>
0022 
0023 // Eve include files
0024 #include <TEveManager.h>
0025 #include <TEveWindow.h>
0026 #include <TEveCalo.h>
0027 #include <TGLViewer.h>
0028 
0029 using namespace dd4hep;
0030 
0031 template <typename T>
0032 static inline typename T::const_iterator find(const T& cont,const std::string& str)  {
0033   for(typename T::const_iterator i=cont.begin(); i!=cont.end(); ++i)  
0034     if ( (*i).name == str ) return i;
0035   return cont.end();
0036 }
0037 
0038 /// Initializing constructor
0039 View::View(Display* eve, const std::string& nam)
0040   : m_eve(eve), m_view(0), m_geoScene(0), m_eveScene(0), m_global(0), m_name(nam), m_showGlobal(false)
0041 {
0042   m_config = m_eve->GetViewConfiguration(m_name);
0043   InstanceCount::increment(this);
0044 }
0045 
0046 /// Default destructor
0047 View::~View() {
0048   m_eve->UnregisterEvents(this);
0049   for(Topics::iterator i=m_geoTopics.begin(); i!=m_geoTopics.end(); ++i)
0050     (*i).second->DestroyElements();
0051   if ( m_geoScene ) m_geoScene->DestroyElements();
0052   if ( m_eveScene ) m_eveScene->DestroyElements();
0053   if ( m_global ) m_global->DestroyElements();
0054   InstanceCount::decrement(this);
0055 }
0056 
0057 /// Build the projection view and map it to the given slot
0058 View& View::Build(TEveWindow* /* slot */)   {
0059   return *this;
0060 }
0061 
0062 /// Initialize the view port
0063 void View::Initialize()  {
0064   TEveScene *evt = this->eveScene();
0065   TEveScene *geo = this->geoScene();
0066   if ( evt ) evt->Repaint(kTRUE);
0067   if ( geo ) geo->Repaint(kTRUE);
0068   /// Update color set
0069   TGLViewer* glv = viewer()->GetGLViewer();
0070   glv->PostSceneBuildSetup(kTRUE);
0071   glv->SetSmartRefresh(kFALSE);
0072   if ( m_eve->manager().GetViewers()->UseLightColorSet() )
0073     glv->UseLightColorSet();
0074   else
0075     glv->UseDarkColorSet();
0076   glv->RequestDraw(TGLRnrCtx::kLODHigh);
0077   glv->SetSmartRefresh(kTRUE);
0078 }
0079 
0080 /// Add the view to the global list of eve objects
0081 TEveElementList* View::AddToGlobalItems(const std::string& nam)   {
0082   if ( 0 == m_global )   {
0083     m_global = new ElementList(nam.c_str(), nam.c_str(), true, true);
0084     if ( m_geoScene ) m_global->AddElement(geoScene());
0085     if ( m_eveScene ) m_global->AddElement(eveScene());
0086     if ( m_showGlobal ) m_eve->manager().AddToListTree(m_global,kFALSE);
0087   }
0088   return m_global;
0089 }
0090 
0091 /// Call an element to a event element list
0092 TEveElement* View::ImportGeoElement(TEveElement* el, TEveElementList* list)  { 
0093   TEveScene* scene = dynamic_cast<TEveScene*>(el);
0094   if ( scene )   {
0095     printf("ERROR: Adding a Scene [%s] to a list. This is BAD and causes crashes!\n",scene->GetName());
0096   }
0097   if ( el ) list->AddElement(el);
0098   return el;
0099 }
0100 
0101 /// Call an element to a geometry element list
0102 TEveElement* View::ImportGeoTopic(TEveElement* element, TEveElementList* list)   {
0103   return ImportGeoElement(element, list);
0104 }
0105 
0106 /// Call an element to a event element list
0107 TEveElement* View::ImportEventElement(TEveElement* el, TEveElementList* list)  { 
0108   TEveScene* scene = dynamic_cast<TEveScene*>(el);
0109   if ( scene )   {
0110     printf("ERROR: Adding a Scene [%s] to a list. This is BAD and causes crashes!\n",scene->GetName());
0111   }
0112   if ( el )   {
0113     printout(INFO,"View","ImportElement %s [%s] into list: %s",
0114              Utilities::GetName(el), el->IsA()->GetName(), list->GetName());
0115     list->AddElement(el);
0116   }
0117   return el;
0118 }
0119 
0120 /// Access the global instance of the subdetector geometry
0121 std::pair<bool,TEveElement*> 
0122 View::GetGlobalGeometry(DetElement de, const DisplayConfiguration::Config& /* cfg */)   {
0123   SensitiveDetector sd = m_eve->detectorDescription().sensitiveDetector(de.name());
0124   TEveElementList& global = m_eve->GetGeoTopic(sd.isValid() ? "Sensitive" : "Structure");
0125   TEveElement* elt = global.FindChild(de.name());
0126   return std::pair<bool,TEveElement*>(true,elt);
0127 }
0128 
0129 /// Create a new instance of the geometry of a sub-detector
0130 std::pair<bool,TEveElement*> 
0131 View::CreateGeometry(DetElement de, const DisplayConfiguration::Config& cfg)   {
0132   SensitiveDetector sd = m_eve->detectorDescription().sensitiveDetector(de.name());
0133   TEveElementList& topic = GetGeoTopic(sd.isValid() ? "Sensitive" : "Structure");
0134   return Utilities::LoadDetElement(de,cfg.data.defaults.load_geo,&topic);
0135 }
0136 
0137 /// Configure a view using the view's name and a proper ViewConfiguration if present
0138 void View::ConfigureGeometryFromInfo()     {
0139   printout(INFO,"View","+++ Configure Geometry for view %s Config=%p.",c_name(),m_config);
0140   (m_config) ? ConfigureGeometry(*m_config) : ConfigureGeometryFromGlobal();
0141   ImportGeoTopics(name());
0142 }
0143 
0144 /// Configure a single geometry view by default from the global geometry scene
0145 void View::ConfigureGeometryFromGlobal()    {
0146   TEveElementList* l = &m_eve->GetGeoTopic("Sensitive");
0147   TEveElementList* t = &GetGeoTopic("Sensitive");
0148   for(TEveElementList::List_i i=l->BeginChildren(); i!=l->EndChildren(); ++i)
0149     ImportGeo(*t,*i);
0150     
0151   l = &m_eve->GetGeoTopic("Structure");
0152   t = &GetGeoTopic("Structure");
0153   for(TEveElementList::List_i i=l->BeginChildren(); i!=l->EndChildren(); ++i) 
0154     ImportGeo(*t,*i);
0155 }
0156 
0157 /// Configure a single geometry view
0158 void View::ConfigureGeometry(const DisplayConfiguration::ViewConfig& config)    {
0159   std::string dets;
0160   DisplayConfiguration::Configurations::const_iterator ic;
0161   float legend_y = Annotation::DefaultTextSize()+Annotation::DefaultMargin();
0162   DetElement world = m_eve->detectorDescription().world();
0163   const DetElement::Children& c = world.children();
0164   for( ic=config.subdetectors.begin(); ic != config.subdetectors.end(); ++ic)   {
0165     const DisplayConfiguration::Config& cfg = *ic;
0166     std::string nam = cfg.name;
0167     if ( nam == "global" ) {
0168       m_view->AddScene(m_eve->manager().GetGlobalScene());
0169       m_view->AddScene(m_eve->manager().GetEventScene());
0170       dets += nam;
0171     }
0172     else if ( cfg.type == DisplayConfiguration::CALODATA )   {
0173       // Note: The histogram grid must be handled like a geometry item.
0174       const Display::CalodataContext& ctx = m_eve->GetCaloHistogram(nam);
0175       if ( ctx.config.use.empty() ) ImportGeo(ctx.calo3D);
0176       printout(INFO,"View","+++ %s: add detector %s  %s",name().c_str(),nam.c_str(),ctx.config.use.c_str());
0177       Color_t col = ctx.calo3D->GetDataSliceColor(ctx.slice);
0178       Annotation* a = new Annotation(viewer(),nam.c_str(),Annotation::DefaultMargin(),legend_y,col);
0179       legend_y += a->GetTextSize();
0180       dets += nam + "(Calo3D)  ";
0181     }
0182     else if ( cfg.type == DisplayConfiguration::DETELEMENT )    {
0183       DetElement::Children::const_iterator i = c.find(nam);
0184       if ( i != c.end() )   {
0185         DetElement de = (*i).second;
0186         SensitiveDetector sd = m_eve->detectorDescription().sensitiveDetector(nam);
0187         TEveElementList& topic = GetGeoTopic(sd.isValid() ? "Sensitive" : "Structure");
0188         std::pair<bool,TEveElement*> e(false,0);
0189         if ( cfg.data.defaults.load_geo > 0 )       // Create a new instance
0190           e = CreateGeometry(de,cfg);               // with the given number of levels
0191         else if ( cfg.data.defaults.load_geo < 0 )  // Use the global geometry instance
0192           e = GetGlobalGeometry(de,cfg);            // with the given number of levels
0193         if ( e.first && e.second )   {
0194           ImportGeo(topic,e.second);
0195         }
0196         dets += nam + "(Geo)  ";
0197       }
0198     }
0199   }
0200   printout(INFO,"ViewConfiguration","+++ Configured geometry for view %s.",c_name());
0201   printout(INFO,"ViewConfiguration","+++ Detectors:%s", dets.c_str());
0202 }
0203 
0204 /// Call to import geometry topics
0205 void View::ImportGeoTopics(const std::string& title)   {
0206   printout(INFO,"View","+++ %s: Import geometry topics.",c_name());
0207   for(Topics::iterator i=m_geoTopics.begin(); i!=m_geoTopics.end(); ++i)  {
0208     printout(INFO,"ViewConfiguration","+++     Add topic %s",(*i).second->GetName());
0209     ImportGeoTopic((*i).second,m_geoScene);
0210   }
0211   if ( !title.empty() ) AddToGlobalItems(title);
0212 }
0213 
0214 /// Call to import geometry elements by topic
0215 void View::ImportGeo(const std::string& topic, TEveElement* element)  { 
0216   ImportGeoElement(element,&GetGeoTopic(topic));
0217 }
0218 
0219 /// Call to import geometry elements into topics
0220 void View::ImportGeo(TEveElementList& topic, TEveElement* element)   {
0221   ImportGeoElement(element,&topic);
0222 }
0223 
0224 /// Call to import geometry elements
0225 void View::ImportGeo(TEveElement* el)  { 
0226   ImportGeoElement(el, m_geoScene);
0227 }
0228 
0229 /// Configure the adding of event data 
0230 void View::ConfigureEventFromInfo()    {
0231   printout(INFO,"View","+++ Import event data into view %s Config=%p.",c_name(),m_config);
0232   (m_config) ? ConfigureEvent(*m_config) : ConfigureEventFromGlobal();
0233   ImportEventTopics();
0234 }
0235 
0236 /// Configure an event view by default from the global event scene
0237 void View::ConfigureEventFromGlobal()    {
0238   TEveElementList* l = m_eve->manager().GetEventScene();
0239   for(TEveElementList::List_i i=l->BeginChildren(); i!=l->EndChildren(); ++i) 
0240     ImportEvent(*i);
0241 }
0242 
0243 /// Configure a single view
0244 void View::ConfigureEvent(const DisplayConfiguration::ViewConfig& config)  {
0245   DetElement world = m_eve->detectorDescription().world();
0246   const DetElement::Children& c = world.children();
0247   DisplayConfiguration::Configurations::const_iterator ic;
0248   for( ic=config.subdetectors.begin(); ic != config.subdetectors.end(); ++ic)  {
0249     const DisplayConfiguration::Config& cfg = *ic;
0250     std::string nam = cfg.name;
0251     if ( nam == "global" )  {
0252       continue;
0253     }
0254     else if ( cfg.type == DisplayConfiguration::CALODATA )  {
0255       continue;
0256     }
0257     else if ( cfg.type == DisplayConfiguration::COLLECTION )  {
0258       // Not using the global scene!
0259       if ( cfg.data.defaults.show_evt>0 )   {
0260         TEveElement* child = m_eve->manager().GetEventScene()->FindChild(nam);
0261         printout(INFO,"View","+++     Add collection:%s data:%p scene:%p",nam.c_str(),child,m_eveScene);
0262         if ( child ) ImportEvent(child);
0263       }
0264     }
0265     else if ( cfg.type == DisplayConfiguration::DETELEMENT )  {
0266       // Not using the global scene!
0267       DetElement::Children::const_iterator i = c.find(nam);
0268       if ( i != c.end() && cfg.data.defaults.show_evt>0 )  {
0269         SensitiveDetector sd = m_eve->detectorDescription().sensitiveDetector(nam);
0270         if ( sd.isValid() )  {
0271           // This should be configurable!
0272           const char* coll = sd.readout().name();
0273           TEveElement* child = m_eve->manager().GetEventScene()->FindChild(coll);
0274           printout(INFO,"View","+++     Add detector event %s collection:%s data:%p scene:%p",
0275                    nam.c_str(),coll,child,m_eveScene);
0276           if ( child ) ImportEvent(child);
0277         }
0278       }
0279     }
0280   }
0281 }
0282 
0283 /// Import event typics after creation
0284 void View::ImportEventTopics()  {
0285 }
0286 
0287 /// Call to import event elements into the main event scene
0288 void View::ImportEvent(TEveElement* el)  { 
0289   if ( m_eveScene )   {
0290     ImportEventElement(el, m_eveScene);
0291   }
0292 }
0293 
0294 /// Access/Create a topic by name
0295 TEveElementList& View::GetGeoTopic(const std::string& nam)    {
0296   Topics::iterator i=m_geoTopics.find(nam);
0297   if ( i == m_geoTopics.end() )  {
0298     TEveElementList* topic = new ElementList(nam.c_str(), nam.c_str(), true, true);
0299     m_geoTopics[nam] = topic;
0300     return *topic;
0301   }
0302   return *((*i).second);
0303 }
0304 
0305 /// Create the geometry and the event scene
0306 View& View::CreateScenes()  {
0307   // Scenes
0308   CreateGeoScene();
0309   CreateEventScene();
0310   return *this;
0311 }
0312 
0313 /// Create the event scene
0314 View& View::CreateEventScene()   {
0315   if ( 0 == m_eveScene ) {
0316     std::string nam  = m_name+" - Event Data";
0317     std::string tool = m_name+" - Scene holding projected event-data for the view.";
0318     m_eveScene = m_eve->manager().SpawnNewScene(nam.c_str(), tool.c_str());
0319   }
0320   return *this;
0321 }
0322 
0323 /// Create the geometry scene
0324 View& View::CreateGeoScene()  {
0325   if ( 0 == m_geoScene )   {
0326     std::string nam  = m_name+" - Geometry";
0327     std::string tool = m_name+" - Scene holding projected geometry for the view.";
0328     m_geoScene = m_eve->manager().SpawnNewScene(nam.c_str(), tool.c_str());
0329   }
0330   return *this;
0331 }
0332  
0333 /// Map the projection view to the slot
0334 View& View::Map(TEveWindow* slot)  {
0335   slot->MakeCurrent();
0336   m_view = m_eve->manager().SpawnNewViewer(m_name.c_str(), "");
0337   if ( m_geoScene ) m_view->AddScene(m_geoScene);
0338   if ( m_eveScene ) m_view->AddScene(m_eveScene);
0339   return *this;
0340 }