CMS 3D CMS Logo

DisplayGeom.cc
Go to the documentation of this file.
1 
5 
12 
14 
17 
19 
21 
23 
25 
26 #include <boost/algorithm/string/case_conv.hpp>
27 
28 #include "TEveManager.h"
29 #include "TEveTrack.h"
30 #include "TEveTrackPropagator.h"
31 
32 #include "TGeoManager.h"
33 #include "TGeoMatrix.h"
34 #include "TEveGeoNode.h"
35 #include "TEveTrans.h"
36 #include "TEveScene.h"
37 #include "TEveViewer.h"
38 #include "TVector.h"
39 #include "TGLScenePad.h"
40 #include "TGLRnrCtx.h"
41 #include "TEvePointSet.h"
42 #include "TRandom.h"
43 #include "TEveUtil.h"
44 
45 #include "TEveQuadSet.h"
46 #include "TEveStraightLineSet.h"
47 #include "TEveRGBAPalette.h"
48 #include "TSystem.h"
49 #include "TStyle.h"
50 // class decleration
51 //
52 
54 public:
55  explicit DisplayGeom(const edm::ParameterSet&);
56  ~DisplayGeom() override;
57 
59 
60 protected:
61  TEveGeoTopNode* make_node(const TString& path, Int_t vis_level, Bool_t global_cs);
62 
63 private:
64  void beginJob() override;
65  void analyze(const edm::Event&, const edm::EventSetup&) override;
66 
67  void endJob() override;
68 
70 
71  TEveElement* m_geomList;
72 
73  int m_level;
74 
75  typedef std::vector<std::string> vstring;
77 
79  std::vector<double> m_MF_plane_d0;
80  std::vector<double> m_MF_plane_d1;
81  std::vector<double> m_MF_plane_d2;
86 
88 
91 
92  void remakeGeometry(const DisplayGeomRecord& dgRec);
93 };
94 
96 
98  : m_eve(),
99  m_geomList(nullptr),
100  m_level(iConfig.getUntrackedParameter<int>("level")),
101  m_nodes(iConfig.getUntrackedParameter<vstring>("nodes")),
102  m_MF_component(0),
103  m_geomWatcher(this, &DisplayGeom::remakeGeometry),
104  m_displayGeomToken(esConsumes()) {
105  std::string component = iConfig.getUntrackedParameter<std::string>("MF_component");
106  boost::algorithm::to_upper(component);
107 
108  if (component == "NONE") {
109  m_MF_component = -1;
110  } else if (component == "ABSBZ") {
111  m_MF_component = 1;
112  } else if (component == "ABSBR") {
113  m_MF_component = 2;
114  } else if (component == "ABSBPHI") {
115  m_MF_component = 3;
116  } else if (component == "BR") {
117  m_MF_component = 4;
118  } else if (component == "BPHI") {
119  m_MF_component = 5;
120  } else { // Anything else -> |B|
121  m_MF_component = 0;
122  }
123 
124  if (m_MF_component != -1) {
126 
127  m_MF_plane_d0 = iConfig.getUntrackedParameter<std::vector<double> >("MF_plane_d0");
128  m_MF_plane_d1 = iConfig.getUntrackedParameter<std::vector<double> >("MF_plane_d1");
129  m_MF_plane_d2 = iConfig.getUntrackedParameter<std::vector<double> >("MF_plane_d2");
130 
131  m_MF_plane_N1 = iConfig.getUntrackedParameter<int>("MF_plane_N");
132  m_MF_plane_N2 = iConfig.getUntrackedParameter<int>("MF_plane_N2");
133  if (m_MF_plane_N2 < 0)
135 
136  m_MF_plane_draw_dir = iConfig.getUntrackedParameter<int>("MF_plane_draw_dir");
137  m_MF_isPickable = iConfig.getUntrackedParameter<bool>("MF_pickable");
138  }
139 }
140 
142 
143 //==============================================================================
144 // Protected helpers
145 //==============================================================================
146 
147 TEveGeoTopNode* DisplayGeom::make_node(const TString& path, Int_t vis_level, Bool_t global_cs) {
148  if (!gGeoManager->cd(path)) {
149  Warning("make_node", "Path '%s' not found.", path.Data());
150  return nullptr;
151  }
152 
153  TEveGeoTopNode* tn = new TEveGeoTopNode(gGeoManager, gGeoManager->GetCurrentNode());
154  tn->SetVisLevel(vis_level);
155  if (global_cs) {
156  tn->RefMainTrans().SetFrom(*gGeoManager->GetCurrentMatrix());
157  }
158  m_geomList->AddElement(tn);
159  gEve->AddToListTree(tn, true);
160  return tn;
161 }
162 
163 //==============================================================================
164 // member functions
165 //==============================================================================
166 
168  if (m_eve) {
169  // Remake geometry if it has changed.
170  m_geomWatcher.check(iSetup);
171 
172  if (m_MF_component != -1) {
173  MagneticField const& field = iSetup.getData(m_magFieldToken);
174 
175  gStyle->SetPalette(1, nullptr);
176 
177  int minval = 0;
178  int maxval = 4000;
179  if (m_MF_component == 1) { //AbsBZ
180  minval = 0, maxval = 4000;
181  } else if (m_MF_component == 2) { //AbsBR
182  minval = 0, maxval = 4000;
183  } else if (m_MF_component == 3) { //AbsBphi
184  minval = 0, maxval = 1000;
185  } else if (m_MF_component == 4) { //BR
186  minval = -4000, maxval = 4000;
187  } else if (m_MF_component == 5) { //Bphi
188  minval = -200, maxval = 200;
189  }
190 
191  TEveRGBAPalette* pal = new TEveRGBAPalette(minval, maxval);
192 
193  TEveStraightLineSet* ls = nullptr;
194  if (m_MF_plane_draw_dir) {
195  ls = new TEveStraightLineSet("MF_line_direction");
196  ls->SetPickable(false);
197  ls->SetLineColor(kGreen);
198  ls->SetMarkerColor(kGreen);
199  ls->SetMarkerStyle(1);
200  }
201 
202  TEveQuadSet* q = new TEveQuadSet("MF_quad_values");
203  q->Reset(TEveQuadSet::kQT_RectangleXY, kFALSE, 32);
204  q->SetOwnIds(kTRUE);
205  q->SetAlwaysSecSelect(true);
206  q->SetPickable(m_MF_isPickable);
207  q->SetPalette(pal);
208 
209  TEveVectorD v0(m_MF_plane_d0[0], m_MF_plane_d0[1], m_MF_plane_d0[2]);
210  TEveVectorD v01(m_MF_plane_d1[0], m_MF_plane_d1[1], m_MF_plane_d1[2]);
211  TEveVectorD v02(m_MF_plane_d2[0], m_MF_plane_d2[1], m_MF_plane_d2[2]);
212 
213  TEveVectorD b01 = (v01 - v0);
214  TEveVectorD b02 = (v02 - v0);
215  TEveVectorD b03 = b01.Cross(b02);
216 
217  TEveTrans trans;
218  trans.SetBaseVec(1, b01.fX, b01.fY, b01.fZ);
219  trans.SetBaseVec(2, b02.fX, b02.fY, b02.fZ);
220  trans.SetBaseVec(3, b03.fX, b03.fY, b03.fZ);
221  trans.SetPos(v0.Arr());
222  trans.OrtoNorm3();
223  q->SetTransMatrix(trans.Array());
224 
225  double w_step = b01.Mag() / m_MF_plane_N1;
226  double h_step = b02.Mag() / m_MF_plane_N2;
227 
228  q->SetDefWidth(w_step);
229  q->SetDefHeight(h_step);
230  TEveVectorD d1;
231  trans.GetBaseVec(1).GetXYZ(d1);
232  d1 *= w_step;
233  TEveVectorD d2;
234  trans.GetBaseVec(2).GetXYZ(d2);
235  d2 *= h_step;
236 
237  //d1.Print();
238  d2.Dump();
239  double line_step_size = TMath::Min(w_step, h_step);
240 
241  for (int i = 0; i < m_MF_plane_N1; i++) {
242  for (int j = 0; j < m_MF_plane_N2; j++) {
243  TEveVectorD p = d1 * Double_t(i) + d2 * Double_t(j) + v0;
244  GlobalPoint pos(p.fX, p.fY, p.fZ);
245  GlobalVector b = field.inTesla(pos) * 1000.; // in mT
246  float value = 0.;
247  if (m_MF_component == 0) { //BMOD
248  value = b.mag();
249  } else if (m_MF_component == 1) { //BZ
250  value = fabs(b.z());
251  } else if (m_MF_component == 2) { //ABSBR
252  value = fabs(b.x() * cos(pos.phi()) + b.y() * sin(pos.phi()));
253  } else if (m_MF_component == 3) { //ABSBPHI
254  value = fabs(-b.x() * sin(pos.phi()) + b.y() * cos(pos.phi()));
255  } else if (m_MF_component == 2) { //BR
256  value = b.x() * cos(pos.phi()) + b.y() * sin(pos.phi());
257  } else if (m_MF_component == 5) { //BPHI
258  value = -b.x() * sin(pos.phi()) + b.y() * cos(pos.phi());
259  }
260 
261  q->AddQuad(w_step * i, h_step * j);
262  q->QuadValue(value);
263  if (m_MF_isPickable)
264  q->QuadId(new TNamed(Form("Mag (%f, %f, %f) val = %f", b.x(), b.y(), b.z(), b.mag()), "Dong!"));
265 
266  if (ls) {
267  if (b.mag() > 1e-6) {
268  b.unit();
269  b *= line_step_size;
270  ls->AddLine(p.fX, p.fY, p.fZ, p.fX + b.x(), p.fY + b.y(), p.fZ + b.z());
271  } else {
272  ls->AddLine(p.fX, p.fY, p.fZ, p.fX + b.x(), p.fY + b.y(), p.fZ + b.z());
273  }
274 
275  ls->AddMarker(ls->GetLinePlex().Size() - 1, 0);
276  ls->AddMarker(i * m_MF_plane_N1 + j, 0);
277  }
278  }
279  }
280 
281  TEveScene* eps = gEve->SpawnNewScene("MF Map");
282  gEve->GetDefaultViewer()->AddScene(eps);
283  eps->GetGLScene()->SetStyle(TGLRnrCtx::kFill);
284  eps->AddElement(q);
285  if (ls)
286  m_eve->AddElement(ls);
287  } else {
288  // // Add a test obj
289  // if (!gRandom)
290  // gRandom = new TRandom(0);
291  // TRandom& r= *gRandom;
292 
293  // Float_t s = 100;
294 
295  // TEvePointSet* ps = new TEvePointSet();
296  // ps->SetOwnIds(kTRUE);
297  // for(Int_t i = 0; i< 100; i++)
298  // {
299  // ps->SetNextPoint(r.Uniform(-s,s), r.Uniform(-s,s), r.Uniform(-s,s));
300  // ps->SetPointId(new TNamed(Form("Point %d", i), ""));
301  // }
302 
303  // ps->SetMarkerColor(TMath::Nint(r.Uniform(2, 9)));
304  // ps->SetMarkerSize(r.Uniform(1, 2));
305  // ps->SetMarkerStyle(4);
306  // m_eve->AddElement(ps);
307  }
308  }
309  m_eve->getManager()->FullRedraw3D(true, true);
310 }
311 
312 // ------------ method called once each job just before starting event loop ------------
314  if (m_eve) {
315  m_geomList = new TEveElementList("Display Geom");
317  // m_eve->getManager()->GetGlobalScene()->GetGLScene()->SetStyle(TGLRnrCtx::kWireFrame);
318  }
319 }
320 
321 // ------------ method called once each job just after ending the event loop ------------
323 
324 //------------------------------------------------------------------------------
326  m_geomList->DestroyElements();
327 
328  TEveGeoManagerHolder _tgeo(const_cast<TGeoManager*>(&dgRec.get(m_displayGeomToken)));
329 
330  for (std::string& aNode : m_nodes) {
331  make_node(aNode, m_level, kTRUE);
332  }
333 }
334 
337  desc.addUntracked<vstring>("nodes", vstring{"tracker:Tracker_1", "muonBase:MUON_1", "caloBase:CALO_1"})
338  ->setComment("List of nodes to visualize");
339  ;
340  desc.addUntracked<int>("level", 4)->setComment("Levels into the geometry hierarchy visualized at startup");
341  desc.addUntracked<std::string>("MF_component", "None")
342  ->setComment("Component of the MF to show: 'None', 'B', 'AbsBZ', 'AbsBR', 'AbsBphi', 'BR', 'Bphi'");
343  desc.addUntracked<std::vector<double> >("MF_plane_d0", std::vector<double>{0., -900., -2400.})
344  ->setComment("1st corner of MF map");
345  desc.addUntracked<std::vector<double> >("MF_plane_d1", std::vector<double>{0., -900., 2400.})
346  ->setComment("2nd corner of MF map");
347  desc.addUntracked<std::vector<double> >("MF_plane_d2", std::vector<double>{0., 900., -2400.})
348  ->setComment("3rd corner of MF map");
349  desc.addUntracked<int>("MF_plane_N", 200)->setComment("Number of bins for the MF map in 1st coord");
350  desc.addUntracked<int>("MF_plane_N2", -1)->setComment("Number of bins for the MF map in 2nd coord");
351  desc.addUntracked<int>("MF_plane_draw_dir", true)->setComment("Draw MF direction arrows (slow)");
352  desc.addUntracked<bool>("MF_pickable", false)->setComment("MF values are pickable (slow)");
353 
354  conf.add("DisplayGeom", desc);
355 }
std::vector< std::string > vstring
Definition: DisplayGeom.cc:75
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
int m_MF_plane_N2
Definition: DisplayGeom.cc:83
vstring m_nodes
Definition: DisplayGeom.cc:76
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
void remakeGeometry(const DisplayGeomRecord &dgRec)
Definition: DisplayGeom.cc:325
void beginJob() override
Definition: DisplayGeom.cc:313
DisplayGeom(const edm::ParameterSet &)
Definition: DisplayGeom.cc:97
virtual GlobalVector inTesla(const GlobalPoint &gp) const =0
Field value ad specified global point, in Tesla.
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
int m_MF_component
Definition: DisplayGeom.cc:78
int m_MF_plane_draw_dir
Definition: DisplayGeom.cc:84
bool m_MF_isPickable
Definition: DisplayGeom.cc:85
std::vector< double > m_MF_plane_d1
Definition: DisplayGeom.cc:80
edm::Service< EveService > m_eve
Definition: DisplayGeom.cc:69
T getUntrackedParameter(std::string const &, T const &) const
TEveGeoTopNode * make_node(const TString &path, Int_t vis_level, Bool_t global_cs)
Definition: DisplayGeom.cc:147
int iEvent
Definition: GenABIO.cc:224
std::vector< double > m_MF_plane_d2
Definition: DisplayGeom.cc:81
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
Definition: value.py:1
edm::ESGetToken< MagneticField, IdealMagneticFieldRecord > m_magFieldToken
Definition: DisplayGeom.cc:89
~DisplayGeom() override
Definition: DisplayGeom.cc:141
void endJob() override
Definition: DisplayGeom.cc:322
std::vector< double > m_MF_plane_d0
Definition: DisplayGeom.cc:79
TEveManager * getManager()
Definition: EveService.cc:249
double b
Definition: hdecay.h:120
void add(std::string const &label, ParameterSetDescription const &psetDescription)
bool check(const edm::EventSetup &iSetup)
Definition: ESWatcher.h:57
int m_MF_plane_N1
Definition: DisplayGeom.cc:82
void AddElement(TEveElement *el)
Definition: EveService.cc:261
edm::ESWatcher< DisplayGeomRecord > m_geomWatcher
Definition: DisplayGeom.cc:87
static constexpr float d1
ProductT const & get(ESGetToken< ProductT, DepRecordT > const &iToken) const
void analyze(const edm::Event &, const edm::EventSetup &) override
Definition: DisplayGeom.cc:167
static void fillDescriptions(edm::ConfigurationDescriptions &)
Definition: DisplayGeom.cc:335
TEveElement * m_geomList
Definition: DisplayGeom.cc:71
void AddGlobalElement(TEveElement *el)
Definition: EveService.cc:263
const edm::ESGetToken< TGeoManager, DisplayGeomRecord > m_displayGeomToken
Definition: DisplayGeom.cc:90