MarlinUtil  1.12.1
DDMarlinCED.h
1 /***********************************************************************************************
2 Minimized copy of MarlinCED specified for the use by DDCEDViewer processor (CEDViewer).
3 Debug comments and unnecessary functionality (for that processor) as well as the GEAR dependence
4 are removed from the original code.
5 It includes helper functions to draw LCIO and the complete draw procedure for the geometry.
6 
7 author: Thorben Quast (CERN Summer Student 2015)
8 date: 12 August 2015
9 ***********************************************************************************************/
10 
11 #ifndef DDMarlinCED_h
12 #define DDMarlinCED_h 1
13 
14 #include <cmath>
15 
16 #include <vector>
17 #include <iostream>
18 #include <iomanip>
19 #include <string>
20 #include "marlin/Processor.h"
21 #include "marlin/Global.h"
22 
23 #include "ced_cli.h"
24 
25 #include <lcio.h>
26 #include <UTIL/LCTypedVector.h>
27 #include <UTIL/LCTOOLS.h>
28 #include <UTIL/Operators.h>
29 #include <EVENT/LCCollection.h>
30 #include <EVENT/MCParticle.h>
31 #include <EVENT/Track.h>
32 #include <EVENT/Cluster.h>
33 #include <EVENT/Vertex.h>
34 #include <EVENT/ReconstructedParticle.h>
35 #include <EVENT/SimTrackerHit.h>
36 #include <EVENT/SimCalorimeterHit.h>
37 #include <EVENT/TrackerHit.h>
38 #include <EVENT/CalorimeterHit.h>
39 
40 #include "IMPL/ClusterImpl.h"
41 
42 #include <MarlinDrawUtil.h>
43 #include <Trajectory.h>
44 
45 #include <ctime> //hauke
46 
47 
48 using namespace marlin ;
49 
50 //Includes for detector drawing
51 #include "DD4hep/LCDD.h"
52 #include "DD4hep/DD4hepUnits.h"
53 #include "DDRec/DetectorData.h"
54 #include "DDRec/SurfaceManager.h"
55 #include "DDRec/Surface.h"
56 using namespace DD4hep::Geometry ;
57 using namespace DD4hep;
58 using namespace DD4hep::DDRec ;
59 namespace DD4hep{
60  namespace Geometry{
61  class LCDD ;
62  }
63 }
64 
65 
66 struct DDCEDMapParticleObject{const LCObject *obj; void (*function)(const LCObject *);};
67 
68 typedef std::map<const int, DDCEDMapParticleObject> DDCEDPickingMap;
69 typedef std::map<std::string, void (*)(const LCObject *)> DDCEDFunctionMap;
70 
71 
72 template <class T>
73  void printDefault(const LCObject *raw){const T* obj = (T*) raw; std::cout << obj;}
74 
82  private:
85  static DDCEDPickingHandler *instance;
86 
90  static DDCEDPickingMap map;
91 
95  static DDCEDFunctionMap funcMap;
96  //static struct termios orig_termios;
97 
98  //DDCEDPickingHandler(); //is not allow to instance this
99  //DDCEDPickingHandler(const DDCEDPickingHandler& cc); //not allows to make copies
100  //~DDCEDPickingHandler();
101 
102 
103  public:
104  //class
107  static DDCEDPickingHandler& getInstance();
108 
112  template <class T>
113  static void defaultOutputFunction(const LCObject *obj){
114 
115  streamlog_out(MESSAGE) << " Picked object " << obj << " with id " << obj->id() << std::endl;
116  streamlog_out(MESSAGE) << *(T*)obj;
117 
118  }
119 
123  static int kbhit(void);
124  /*
125  static void set_conio_terminal_mode(void);
126  static void reset_terminal_mode(void);
127  static int getch(void);
128  */
129 
130 
140  void update(LCEvent *);
141 
144  void printID(int);
145 
150  void registerFunction(std::string, void (*)(const LCObject *));
151 };
152 //end hauke hoelbe
153 
154 
159 class DDMarlinCED {
160 
161  public:
162  static DDMarlinCED* instance() ;
163 
164  LCEvent* _currEvent;
165 
168  static void init( Processor* proc ) ;
169 
170  static void newEvent( Processor* proc , LCEvent* evt=0 ) ;
171 
176  static void draw( Processor* proc , int waitForKeyboard=1 ) ;
177 
178  static void getParticleFromID(int, LCEvent*);
179 
184  template <class In>
185  static void drawObjectsWithPosition(In first, In last, int marker, int size ,unsigned int color, unsigned int layer=0, const char * PickingMessage="") {
186  while( first != last ) {
187  int id = (*first)->id();
188  ced_hit_ID( (*first)->getPosition()[0],
189  (*first)->getPosition()[1],
190  (*first)->getPosition()[2],
191  marker,layer, size , color, id ) ;
192 
193  ++first ;
194  }
195  }
196 
197 
198 //hauke hoelbe
199  template <class In>
200  static void drawObjectsWithPositionID(LCCollection* col,In first, In last, int marker, int size ,unsigned int color, unsigned int layer=0) {
201  int i=0;
202  while( first != last ) {
203  int id = (*first)->id();
204  std::cout << "test!!! " << std::endl;
205  ced_hit_ID( (*first)->getPosition()[0],
206  (*first)->getPosition()[1],
207  (*first)->getPosition()[2],
208  marker, layer, size , color, id ) ;
209 
210  ++first ;
211  i++;
212  }
213  }
214 
215 
218  static void drawHelix(float b, float charge, float x, float y, float z,
219  float px, float py, float pz, int marker, int size,
220  unsigned int col,
221  float rmin=10.0, float rmax=3000.0, float zmax=4500.0, unsigned int id = 0);
222 
223 /*************************/
224  static int getIDfromIndex(LCCollection* col, int index);
225 
226  static void set_layer_description(const std::string& desc, int layerID);
227  static void add_layer_description(const std::string& desc, int layerID);
228  static void write_layer_description(void);
229 
230  /* Draws the detector geometry for CLIC and ILD.
231  * features:
232  * - improved, i.e. more exact, placements
233  * - generic
234  * - no GEAR dependence
235  * - surface (optionally) drawn as set of lines
236  *
237  * author: Thorben Quast, CERN Summer Student 2015
238  * date: 31/07/2015
239  */
240  //TODO: rename
241  static void drawDD4hepDetector( DD4hep::Geometry::LCDD& lcdd, bool _surfaces, StringVec _detailled);
242 
243 private:
244  static int _int_count;
245  static std::vector<std::string> _descs;
246 
247 
248 protected:
249 
250  //hauke hoelbe: 08.02.2010
251  DDMarlinCED() : _first(0) , _last(0){ _currEvent=0; }
252 
253  static DDMarlinCED* _me ;
254 
255  Processor* _first ;
256  Processor* _last ;
257  // helper method to draw hit collections by type
258  static void drawHitCollectionsByType(LCEvent* event, const char* type, int marker, int size,
259  unsigned int color, unsigned int layer=0) {
260 
261  try {
262 
263  std::vector< std::string >::const_iterator iter;
264  const std::vector< std::string >* ColNames = event->getCollectionNames();
265 
266  for( iter = ColNames->begin() ; iter != ColNames->end() ; iter++) {
267 
268  LCCollection* col = event->getCollection( *iter ) ;
269 
270  if ( col->getTypeName() == type ) {
271 
272  if ( type == LCIO::SIMTRACKERHIT ) {
273 
274  LCTypedVector<SimTrackerHit> v(col);
275  drawObjectsWithPosition(v.begin(),v.end(),marker,size,color,layer);
276 
277  }
278 
279  if ( type == LCIO::SIMCALORIMETERHIT ) {
280 
281  LCTypedVector<SimCalorimeterHit> v(col);
282  drawObjectsWithPosition(v.begin(),v.end(),marker,size,color,layer);
283 
284  }
285 
286  if ( type == LCIO::TRACKERHIT ) {
287 
288  LCTypedVector<TrackerHit> v(col);
289  drawObjectsWithPosition(v.begin(),v.end(),marker,size,color,layer);
290 
291  }
292 
293  if ( type == LCIO::CALORIMETERHIT ) {
294 
295  LCTypedVector<CalorimeterHit> v(col);
296  drawObjectsWithPosition(v.begin(),v.end(),marker,size,color,layer);
297 
298  }
299  }
300  }
301  }
302  catch(DataNotAvailableException &e){}
303 
304  }
305 
306  // FIXME: Not so elegant, refine! Use iterators, templates etc. See drawHitCollectionsByType(...).
307  // helper method to draw hit collections by MC Contribution
308  static void drawHitCollectionsByMCContribution(LCEvent* event, MCParticle* MCP, int marker, int size,
309  unsigned int color, unsigned int layer=0) {
310 
311  std::vector< std::string >::const_iterator iter;
312  const std::vector< std::string >* ColNames = event->getCollectionNames();
313 
314  for( iter = ColNames->begin() ; iter != ColNames->end() ; iter++) {
315 
316  LCCollection* col = event->getCollection( *iter ) ;
317 
318  if ( col->getTypeName() == LCIO::SIMTRACKERHIT ) {
319 
320  int n = col->getNumberOfElements();
321 
322  for (int i = 0; i < n; ++i) {
323 
324  SimTrackerHit* hit = dynamic_cast<SimTrackerHit*>(col->getElementAt(i));
325 
326  if (hit->getMCParticle() == MCP) {
327  double x = hit->getPosition()[0];
328  double y = hit->getPosition()[1];
329  double z = hit->getPosition()[2];
330  ced_hit_ID(x,y,z,marker, layer,size,color, MCP->id());
331 
332 
333  }
334  }
335  }
336 
337  if ( col->getTypeName() == LCIO::SIMCALORIMETERHIT ) {
338 
339  int n = col->getNumberOfElements();
340 
341  for (int i = 0; i < n; ++i) {
342 
343  SimCalorimeterHit* hit = dynamic_cast<SimCalorimeterHit*>(col->getElementAt(i));
344 
345  int nMC = hit->getNMCContributions();
346 
347  bool found = false;
348  for (int j = 0; j < nMC; ++j) {
349  if (hit->getParticleCont(j) == MCP) {
350  found = true;
351  break;
352  }
353  }
354 
355  if (found) {
356  double x = hit->getPosition()[0];
357  double y = hit->getPosition()[1];
358  double z = hit->getPosition()[2];
359  ced_hit_ID(x,y,z,marker,layer,size,color, MCP->id());
360  }
361  }
362  }
363  }
364  }
365 } ;
366 
367 extern "C"
368 void DDdraw_helix( float b, float charge, float x, float y, float z,
369  float px, float py, float pz,
370  int marker, int size, unsigned int col,
371  float rmin=10.0, float rmax=3000.0, float zmax=4500.0, unsigned int id = 0) ;
372 
373 
374 /******* HELPERS ********/
375 
376 //read out of the "_detailled" parameter
377 bool detailledDrawing(StringVec _detailled, std::string detName);
378 
379 //Set of geometric parameters for initialization of a CEDGeoBox class object
380 struct CEDGeoBox {
381  double sizes[3] ;
382  double center[3] ;
383  double rotate[3];
384 };
385 //Set of geometric parameters for initialization of a CEDGeoTube class object
387  double Rmax; double Rmin; double inner_symmetry; double outer_symmetry; double phi0; double delta_phi; double delta_z; double z0;
388  //boolean that decides if the GeoTube is drawn twice at two different zPositions
389  bool isBarrel;
390 };
391 
392 //Convenient summary of both parameter sets above as (tracker) layers may be drawn as one tube or as a sequence of staves (-->GeoBox)
394  CEDGeoTubeParams tube;
395  std::vector<CEDGeoBox> staves;
396 };
397 
398 
399 
400 void getVisAttributes(DD4hep::Geometry::DetElement det, unsigned &color, bool &visible);
401 
402 /***detector draw helpers***/
403 
404 //converts the parameters in LayeredCalorimeterData given by the appropriate drivers
405 //into those required by the CEDGeoTube
406 CEDGeoTubeParams CalorimeterParameterConversion (LayeredCalorimeterData *calo);
407 
408 //converts the parameters in ZDiskPetalsData given by the appropriate drivers
409 //into those required by the CEDGeoTube
410 CEDGeoTubeParams PetalParameterConversion (std::vector<DDRec::ZDiskPetalsData::LayerLayout>::iterator thisLayer);
411 
412 //converts the parameters from a LayeredCalorimeterData layer given by the appropriate drivers
413 //into those required by the CEDGeoTube
414 CEDGeoTubeParams CalorimeterLayerParameterConversion(std::vector<DDRec::LayeredCalorimeterData::Layer>::iterator thisLayer);
415 
416 //converts the parameters from a FixedPadSizeTPCData given by the appropriate drivers
417 //into those required by the CEDGeoTube
418 CEDGeoTubeParams TPCParameterConversion(FixedPadSizeTPCData *tpc);
419 
420 //converts the parameters from a ZPlanarData::LayerLayout layer given by the appropriate drivers
421 //into those required by the CEDGeoBox (for drawing of staves) or by CEDGeoTube (for approximation of the set of staves into tubes)
422 LayerGeometry TrackerLayerParameterConversion(std::vector<DDRec::ZPlanarData::LayerLayout>::iterator thisLayer);
423 
424 //draws the given surfaces as a set of individual lines
425 bool DrawSurfaces(const DD4hep::DDRec::SurfaceManager &surfMan, std::string detName, unsigned color, int layer);
426 
427 #endif
428 
429 
430 
431 
Definition: DDMarlinCED.h:393
Singleton class to manage access to CED from several processors.
Definition: DDMarlinCED.h:159
Definition: DDMarlinCED.h:380
static void drawObjectsWithPosition(In first, In last, int marker, int size, unsigned int color, unsigned int layer=0, const char *PickingMessage="")
Draw all objects in iterator range [first,last) which have a method getPosition() with the given colo...
Definition: DDMarlinCED.h:185
Definition: DDMarlinCED.h:386
Bring the feature to print a LCIO-Objekt given by his ID.
Definition: DDMarlinCED.h:81
Definition: DDMarlinCED.h:66
static void defaultOutputFunction(const LCObject *obj)
The default print method for all LCIO objects.
Definition: DDMarlinCED.h:113