DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
DetectorData.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : M.Frank
11 //
12 //==========================================================================
13 
14 // Framework include files
15 #include <DD4hep/Grammar.h>
16 #include <DD4hep/DetectorData.h>
17 #include <DD4hep/InstanceCount.h>
20 
21 // ROOT include files
22 #include <TGeoManager.h>
23 #include <TClassStreamer.h>
24 #include <TDataMember.h>
25 #include <TDataType.h>
26 #include <TClass.h>
27 #include <TROOT.h>
28 
29 namespace dd4hep { namespace detail { class DetectorImp; }}
30 
31 using namespace dd4hep;
32 
33 namespace {
34 
35  union FundamentalData {
36  Char_t c;
37  Short_t s;
38  Int_t i;
39  Long_t l;
40  Float_t f;
41  Double_t d;
42  UChar_t uc;
43  UShort_t us;
44  UInt_t ui;
45  ULong_t ul;
46  Long64_t ll;
47  ULong64_t ull;
48  Bool_t b;
49  void read(TBuffer& buff,int dtyp) {
50  switch(dtyp) {
51  case 1: buff >> c; break;
52  case 2: buff >> s; break;
53  case 3: buff >> i; break;
54  case 4: buff >> l; break;
55  case 5: buff >> f; break;
56  case 6: buff >> i; break;
57  //case 7: buff >> d; break; // char*
58  case 8: buff >> d; break;
59  case 9: buff >> d; break;
60  case 11: buff >> uc; break;
61  case 12: buff >> us; break;
62  case 13: buff >> ui; break;
63  case 14: buff >> ul; break;
64  case 15: buff >> ui; break;
65  case 16: buff >> ll; break;
66  case 17: buff >> ull; break;
67  case 18: buff >> b; break;
68  case 19: buff >> f; break;
69  default:
70  printout(ERROR,"OpaqueData","Unknown fundamental data type: [%X]",dtyp);
71  break;
72  }
73  }
74  void write(TBuffer& buff,int dtyp) const {
75  switch(dtyp) {
76  case 1: buff << c; break;
77  case 2: buff << s; break;
78  case 3: buff << i; break;
79  case 4: buff << l; break;
80  case 5: buff << f; break;
81  case 6: buff << i; break;
82  //case 7: buff << d; break; // char*
83  case 8: buff << d; break;
84  case 9: buff << d; break;
85  case 11: buff << uc; break;
86  case 12: buff << us; break;
87  case 13: buff << ui; break;
88  case 14: buff << ul; break;
89  case 15: buff << ui; break;
90  case 16: buff << ll; break;
91  case 17: buff << ull; break;
92  case 18: buff << b; break;
93  case 19: buff << f; break;
94  default:
95  printout(ERROR,"OpaqueData","Unknown fundamental data type: [%X]",dtyp);
96  break;
97  }
98  }
99  };
100 
101  void stream_opaque_datablock(TBuffer& b, void* obj) {
102  UInt_t R__s = 0; // Start of object.
103  UInt_t R__c = 0; // Count of bytes.
104  TClass* cl = BasicGrammar::instance<OpaqueDataBlock>().clazz();//gROOT->GetClass("dd4hep::OpaqueDataBlock");
105 
106  OpaqueDataBlock* block = (OpaqueDataBlock*)obj;
107 
108  if ( b.IsReading() ) {
109  b.ReadVersion(&R__s, &R__c, cl);
111  b >> key;
112  const BasicGrammar& gr = BasicGrammar::get(key);
113  //printout(INFO,"OpaqueData"," Data type:%s [%016llX]",gr.name.c_str(),key);
114  void* ptr = block->ptr();
115  if ( !ptr ) { // Some blocks are already bound. Skip those.
116  if ( !gr.specialization.bind ) {
117  except("stream_opaque_datablock","Object cannot be handled by ROOT persistency. "
118  "Grammar %s does not allow for ROOT persistency.",gr.type_name().c_str());
119  }
120  ptr = block->bind(&gr);
121  gr.specialization.bind(ptr);
122  }
124  if ( gr.type() == typeid(std::string) )
125  b.ReadStdString(*(std::string*)ptr);
126  else if ( gr.clazz() )
127  b.ReadClassBuffer(gr.clazz(),ptr);
128  else
129  ((FundamentalData*)ptr)->read(b,gr.data_type());
130  b.CheckByteCount(R__s, R__c, cl);
131  }
132  else if ( 0 == block->grammar ) {
133  printout(ERROR,"OpaqueData","+++ ERROR +++ Opaque data block has no grammar attached. Cannot be saved!");
134  }
135  else {
136  const std::type_info& typ_info = typeid(*block);
137  const BasicGrammar& gr = *block->grammar;
138  std::string typ = gr.type_name();
139 
140  printout(DEBUG,"OpaqueData","Saving object %p type: %s [%s] [%s]",
141  obj, typ_info.name(), typ.c_str(), block->grammar->name.c_str());
142  R__c = b.WriteVersion(cl,kTRUE);
143  b << gr.hash();
144 
146  if ( gr.type() == typeid(std::string) )
147  b.WriteStdString(*(std::string*)block->ptr());
148  else if ( gr.clazz() )
149  b.WriteClassBuffer(gr.clazz(),block->ptr());
150  else
151  ((const FundamentalData*)block->ptr())->write(b,gr.data_type());
152  b.SetByteCount(R__c, kTRUE);
153  }
154  }
155 }
156 
157 
160  : m_manager(0), m_world(), m_trackers(), m_worldVol(),
161  m_trackingVol(), m_field(),
162  m_buildType(BUILD_DEFAULT), m_extensions(typeid(DetectorData)), m_volManager(),
163  m_inhibitConstants(false)
164 {
165  static bool first = true;
166  if ( first ) {
167  first = false;
168  TClass* cl = gROOT->GetClass("dd4hep::OpaqueDataBlock");
169  if ( 0 == cl ) {
170  except("PersistencyIO","+++ Missing TClass for 'dd4hep::OpaqueDataBlock'.");
171  }
172  if ( 0 == cl->GetStreamer() ) {
173  cl->AdoptStreamer(new TClassStreamer(stream_opaque_datablock));
174  printout(INFO,"PersistencyIO","+++ Set Streamer to %s",cl->GetName());
175  }
176  }
178 }
179 
182  clearData();
184 }
185 
188  TDataMember* dm = 0;
189  printout(INFO,"PersistencyIO",
190  "+++ Set data member %s.fUserExtension as PERSISTENT.",
191  cl->GetName());
192  dm = cl->GetDataMember("fUserExtension");
193  dm->SetTitle(dm->GetTitle()+2);
194  dm->SetBit(BIT(2));
195 }
196 
199  TDataMember* dm = 0;
200  printout(INFO,"PersistencyIO",
201  "+++ Set data member %s.fUserExtension as TRANSIENT.",
202  cl->GetName());
203  dm = cl->GetDataMember("fUserExtension");
204  dm->SetTitle((std::string("! ")+dm->GetTitle()).c_str());
205  dm->ResetBit(BIT(2));
206 }
207 
209 void DetectorData::destroyData(bool destroy_mgr) {
211  m_detectorParents.clear();
212 
224 #if 0
225  for(const auto& def : m_define) {
226  auto c = def;
227  std::cout << "Delete " << def.first << std::endl;
228  //if ( def.first == "world_side" ) continue;
229  delete def.second.ptr();
230  }
231 #endif
233  m_properties.clear();
234  m_trackers.clear();
237  m_worldVol.clear();
241  m_inhibitConstants = false;
242  if ( destroy_mgr ) {
243  gGeoManager = m_manager;
244  detail::deletePtr(m_manager);
245  gGeoManager = 0;
246  }
247  else {
248  gGeoManager = m_manager;
249  m_manager = 0;
250  }
251 }
252 
253 
256  m_extensions.clear(false);
257  m_detectorParents.clear();
258  m_world.clear();
259  m_field.clear();
260  m_header.clear();
261  m_properties.clear();
262  m_readouts.clear();
263  m_idDict.clear();
264  m_limits.clear();
265  m_regions.clear();
266  m_sensitive.clear();
267  m_display.clear();
268  m_fields.clear();
269  m_define.clear();
270  m_trackers.clear();
271  m_worldVol.clear();
278  m_manager = 0;
279  m_inhibitConstants = false;
280 }
281 
283 void DetectorData::adoptData(DetectorData& source, bool clr) {
286  m_manager = source.m_manager;
287  m_readouts = source.m_readouts;
288  m_idDict = source.m_idDict;
289  m_limits = source.m_limits;
290  m_regions = source.m_regions;
291  m_detectors = source.m_detectors;
292  m_sensitive = source.m_sensitive;
293  m_display = source.m_display;
294  m_fields = source.m_fields;
295  m_define = source.m_define;
296 
298  m_world = source.m_world;
299  World w = m_world;
300  w->description = dynamic_cast<Detector*>(this);
301  m_trackers = source.m_trackers;
302  m_worldVol = source.m_worldVol;
303  m_trackingVol = source.m_trackingVol;
305  m_materialAir = source.m_materialAir;
308  m_field = source.m_field;
309  m_header = source.m_header;
310  m_properties = source.m_properties;
311  //m_extensions = source.m_extensions;
312  m_volManager = source.m_volManager;
313 
314  // Update world element
315  m_world.setPlacement(m_manager->GetTopNode());
316  // Need to update some global stuff
317  if ( gGeoManager != m_manager ) delete gGeoManager;
318  gGeoManager = m_manager;
319  if ( clr ) source.clearData();
320 }
dd4hep::World
Handle class to hold the information of the top DetElement object 'world'.
Definition: World.h:31
dd4hep::BasicGrammar::name
const std::string name
Instance type name.
Definition: Grammar.h:61
dd4hep::BasicGrammar::key_type
unsigned long long int key_type
Definition: Grammar.h:59
dd4hep::BasicGrammar::hash
key_type hash() const
Access the hash value for this grammar type.
Definition: Grammar.h:128
dd4hep::DetectorData::m_sensitive
ObjectHandleMap m_sensitive
The map of top level sub-detector sensitive detector objects indexed by the detector name.
Definition: DetectorData.h:110
dd4hep::DetectorData::m_detectorParents
std::map< std::string, DetElement > m_detectorParents
Definition: DetectorData.h:120
dd4hep::DetectorData::m_display
ObjectHandleMap m_display
The map of display attributes in use.
Definition: DetectorData.h:114
dd4hep::detail::destroyHandle
void destroyHandle(T &handle)
Helper to delete objects from heap and reset the handle.
Definition: Handle.h:185
dd4hep::DetectorData::clearData
void clearData()
Clear data content: DOES NOT RELEASEW ALLOCATED RESOURCES!
Definition: DetectorData.cpp:255
dd4hep::DetectorData::m_trackingVol
Volume m_trackingVol
Definition: DetectorData.h:126
dd4hep::DetectorData::m_world
DetElement m_world
Definition: DetectorData.h:122
dd4hep::OpaqueDataBlock
Class describing an opaque conditions data block.
Definition: OpaqueData.h:92
DetectorInterna.h
dd4hep::InstanceCount::increment
static void increment(T *)
Increment count according to type information.
Definition: InstanceCount.h:98
dd4hep::DetectorData::m_header
Header m_header
Definition: DetectorData.h:132
dd4hep::DetectorData::m_field
OverlayedField m_field
Definition: DetectorData.h:131
dd4hep::DetectorData::m_idDict
ObjectHandleMap m_idDict
Map of readout IDDescriptors indexed by hit collection name.
Definition: DetectorData.h:102
dd4hep::DetectorData::DetectorData
DetectorData()
Default constructor.
Definition: DetectorData.cpp:159
dd4hep::DetectorData::patchRootStreamer
static void patchRootStreamer(TClass *cl)
Assignment operator.
Definition: DetectorData.cpp:187
dd4hep::DetectorData::m_inhibitConstants
bool m_inhibitConstants
Flag to inhibit the access to global constants. Value set by constants section 'Detector_InhibitConst...
Definition: DetectorData.h:145
dd4hep::DetectorData::m_worldVol
Volume m_worldVol
Definition: DetectorData.h:124
dd4hep::BasicGrammar::data_type
int data_type() const
Access ROOT data type for fundamentals.
Definition: Grammar.h:132
dd4hep::Handle::clear
Handle< T > & clear()
Release the object held by the handle.
Definition: Handle.h:136
dd4hep::BasicGrammar::type_name
const std::string & type_name() const
Access to the type information name.
Definition: Grammar.h:130
dd4hep::DetectorData::m_define
ObjectHandleMap m_define
Definition: DetectorData.h:118
dd4hep::OpaqueDataBlock::bind
void * bind(const BasicGrammar *grammar)
Bind data value.
Definition: OpaqueData.cpp:124
dd4hep::DetectorData::m_regions
ObjectHandleMap m_regions
Map of regions settings for the simulation.
Definition: DetectorData.h:106
dd4hep::BasicGrammar
Base class describing string evaluation to C++ objects using boost::spirit.
Definition: Grammar.h:56
dd4hep::DetectorData::m_volManager
VolumeManager m_volManager
Volume manager reference.
Definition: DetectorData.h:139
dd4hep::DetectorData::m_readouts
ObjectHandleMap m_readouts
Map of readout descriptors indexed by subdetector name.
Definition: DetectorData.h:108
dd4hep::DetectorData::m_detectors
ObjectHandleMap m_detectors
The map of top level sub-detector objects indexed by name.
Definition: DetectorData.h:112
dd4hep::InstanceCount::decrement
static void decrement(T *)
Decrement count according to type information.
Definition: InstanceCount.h:102
dd4hep::detail::destroyHandles
void destroyHandles(M &arg)
Functional created of map destruction functors.
Definition: Handle.h:205
dd4hep::BasicGrammar::get
static const BasicGrammar & get(const std::type_info &info)
Access grammar by type info.
Definition: Grammar.cpp:184
dd4hep::DetectorData::m_materialAir
Material m_materialAir
Definition: DetectorData.h:128
dd4hep::DetectorData::destroyData
void destroyData(bool destroy_mgr=true)
Clear data content: releases all allocated resources.
Definition: DetectorData.cpp:209
dd4hep::OpaqueDataBlock::ptr
void * ptr() const
Write access to the data buffer. Is only valid after call to bind<T>()
Definition: OpaqueData.h:133
dd4hep::BasicGrammar::specialization_t::bind
void(* bind)(void *pointer)=0
Bind opaque address to object.
Definition: Grammar.h:76
dd4hep::ObjectExtensions::move
void move(ObjectExtensions &copy)
Move extensions to target object.
Definition: ObjectExtensions.cpp:41
dd4hep::DetectorData::m_properties
Detector::Properties m_properties
Definition: DetectorData.h:133
dd4hep::DetectorData::m_fields
ObjectHandleMap m_fields
The map of electro magnet field components for the global overlay field.
Definition: DetectorData.h:116
dd4hep::DetectorData::~DetectorData
virtual ~DetectorData()
Default destructor.
Definition: DetectorData.cpp:181
DetectorData.h
dd4hep::DetElement::setPlacement
DetElement & setPlacement(const PlacedVolume &volume)
Set the physical volumes of the detector element.
Definition: DetElement.cpp:330
dd4hep::BasicGrammar::specialization
struct dd4hep::BasicGrammar::specialization_t specialization
dd4hep::BasicGrammar::type
virtual const std::type_info & type() const =0
Access to the type information.
dd4hep::BasicGrammar::clazz
TClass * clazz() const
Access the ROOT class for complex objects.
Definition: Grammar.h:137
key
unsigned char key
Definition: AlignmentsCalculator.cpp:69
dd4hep::DetectorData::m_trackers
DetElement m_trackers
Definition: DetectorData.h:123
dd4hep::DetectorData::m_parallelWorldVol
Volume m_parallelWorldVol
Definition: DetectorData.h:125
dd4hep::DetectorData::m_materialVacuum
Material m_materialVacuum
Definition: DetectorData.h:129
ObjectsInterna.h
dd4hep::DetectorData::adoptData
void adoptData(DetectorData &source, bool CLR=true)
Adopt all data from source structure.
Definition: DetectorData.cpp:283
dd4hep::DetectorData::m_extensions
ObjectExtensions m_extensions
Definition of the extension type.
Definition: DetectorData.h:137
dd4hep::ObjectExtensions::clear
void clear(bool destroy=true)
Clear all extensions.
Definition: ObjectExtensions.cpp:47
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::DetectorData
Data implementation class of the Detector interface.
Definition: DetectorData.h:38
dd4hep::Detector
The main interface to the dd4hep detector description package.
Definition: Detector.h:90
dd4hep::DetectorData::m_manager
TGeoManager * m_manager
Reference to the geometry manager object from ROOT.
Definition: DetectorData.h:100
dd4hep::OpaqueData::grammar
const BasicGrammar * grammar
Data type.
Definition: OpaqueData.h:53
dd4hep::DetectorData::m_invisibleVis
VisAttr m_invisibleVis
Definition: DetectorData.h:130
dd4hep::BUILD_DEFAULT
@ BUILD_DEFAULT
Definition: BuildType.h:36
InstanceCount.h
dd4hep::DetectorData::unpatchRootStreamer
static void unpatchRootStreamer(TClass *cl)
UNPatch the ROOT streamers to adapt for DD4hep (set fUserExtension transient)
Definition: DetectorData.cpp:198
dd4hep::DetectorImp
Concrete implementation class of the Detector interface.
Definition: DetectorImp.h:59
dd4hep::DetectorData::m_limits
ObjectHandleMap m_limits
Map of limit sets.
Definition: DetectorData.h:104
Grammar.h