Class Reference for E1039 Core & Analysis Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mTrkEveDisplay.cxx
Go to the documentation of this file.
1 
9 // STL and BOOST includes
10 #include <iostream>
11 #include <algorithm>
12 #include <stdexcept>
13 #include <cmath>
14 #include <boost/bind.hpp>
15 
16 // PHENIX includes
17 #include <phool/PHCompositeNode.h>
18 //#include <PHPoint.h>
19 #include <phool/getClass.h>
20 
21 // ROOT and EVE includes
22 #include <TEveManager.h>
23 #include <TEveTrackPropagator.h>
24 #include <TEveTrack.h>
25 #include <TEvePointSet.h>
26 #include <TEveElement.h>
27 #include <TEveCaloData.h>
28 #include <TEveCalo.h>
29 #include <TEveQuadSet.h>
30 #include <TEveTrans.h>
31 #include <TH2F.h>
32 #include <TNamed.h>
33 
35 #include <interface_main/SQHit.h>
36 #include <ktracker/SRecEvent.h>
37 #include <geom_svc/GeomSvc.h>
38 
39 #include <pheve_display/PHEveDisplay.h>
40 
41 #include "mTrkEveDisplay.h"
42 
43 using boost::bind;
44 
45 
48  _evedisp(dispin),
49  _prop(NULL),
50  _reco_tracks(NULL)
51 {
52 
53  verbosity = _evedisp->get_verbosity();
54  _evemanager = _evedisp->get_eve_manager();
55  _prop = _evedisp->get_cnt_prop();
56  _reco_tracks = new TEveTrackList("Svtx Tracks");
57 
58  _geom_svc = GeomSvc::instance();
59  TEveRGBAPalette *pal = new TEveRGBAPalette(0, 100, true, true, true);
60  for(int i=0; i<NDETPLANES; ++i) {
61  _hit_wires[i] = new TEveQuadSet(std::to_string(i).c_str());
62  _hit_wires[i]->SetOwnIds(kTRUE);
63  //_hit_wires[i]->SetPalette(pal);
64  _hit_wires[i]->Reset(TEveQuadSet::kQT_LineXYFixedZ, true, 32);
65  _hit_wires[i]->RefitPlex();
66  _hit_wires[i]->SetPickable(true);
67  TEveTrans& t = _hit_wires[i]->RefMainTrans();
68  t.SetPos(0, 0, _geom_svc->getPlaneCenterZ(i));
69  if(i<=30) _evemanager->AddElement(_hit_wires[i],_evedisp->get_dc_list());
70  else if(i<=46) _evemanager->AddElement(_hit_wires[i],_evedisp->get_hodo_list());
71  else if(i<=54) _evemanager->AddElement(_hit_wires[i],_evedisp->get_prop_list());
72  else if(i<=62) _evemanager->AddElement(_hit_wires[i],_evedisp->get_dp_list());
73  }
74 
75  _evemanager->AddElement(_reco_tracks,_evedisp->get_top_list());
76 
77 }
78 
80 {
81 }
82 
83 void
85 {
86 }
87 
88 void
90 {
91  //_geom_svc = GeomSvc::instance();
92 }
93 
94 bool
96 {
97  clear();
98 
99  if (verbosity)
100  std::cout << "mTrkEveDisplay - event() begins." << std::endl;
101 
102  try {
103  get_nodes(topNode);
104 
105  if(verbosity > 0) LogInfo("draw_hits()");
106  draw_hits();
107 
108  if(verbosity > 0) LogInfo("draw_tracks()");
109  draw_tracks();
110  } catch (std::exception& e) {
111  static bool first(true);
112  if (first)
113  std::cout << "mTrkEveDisplay::event Exception: " << e.what() << std::endl;
114  first = false;
115  }
116 
117  return true;
118 
119 }
120 
121 void
123 {
124 }
125 
126 void
128 {
129  _sqhit_col = findNode::getClass<SQHitVector>(topNode,"SQHitVector");
130  if(!_sqhit_col) std::cout<< "SQHitVector node not found!!"<<std::endl;
131  if(verbosity) std::cout<<"mTrkEveDisplay - SQHitVector nodes found."<<std::endl;
132 
133  _recEvent = findNode::getClass<SRecEvent>(topNode, "SRecEvent");
134  if(!_recEvent) std::cout<< "SRecEvent node not found!!"<<std::endl;
135  if(verbosity) std::cout<<"mTrkEveDisplay - SRecEvent nodes found."<<std::endl;
136 }
137 
138 int mTrkEveDisplay::hit_to_wire(const int det_id, const int elm_id, double& x, double& y, double& dx, double& dy) {
139 
140  if (!_geom_svc) return -1;
141 
142  //double pos = _geom_svc->getMeasurement(det_id, elm_id) - _geom_svc->getPlane(det_id).deltaW - _geom_svc->getPlane(det_id).xoffset;
143  double pos = _geom_svc->getMeasurement(det_id, elm_id);
144 
145  double cos_theta = _geom_svc->getCostheta(det_id);
146  double sin_theta = _geom_svc->getSintheta(det_id);
147  double n[2] = {cos_theta, sin_theta};
148 
149  // Some ref point
150  double xc = 0; //_geom_svc->getPlane(det_id).x0;
151  double yc = 0; //_geom_svc->getPlane(det_id).y0;
152  double vc[2] = {xc, yc};
153 
154  double y1 = _geom_svc->getPlane(det_id).y1;
155  double y2 = _geom_svc->getPlane(det_id).y2;
156  double x1 = _geom_svc->getPlane(det_id).x1;
157  double x2 = _geom_svc->getPlane(det_id).x2;
158 
159  // (v - v_c) dot n = pos
160  // v0*n0 +v1*n1 = pos + v_c0*n0+v_c1*n1
161  if(fabs(n[0])<0.01) { // horizontal wire
162  y1 = (pos + xc*n[0] + yc*n[1] - x1*n[0])/n[1];
163  y2 = (pos + xc*n[0] + yc*n[1] - x2*n[0])/n[1];
164  } else { // non-horizontal wire
165  x1 = (pos + xc*n[0] + yc*n[1] - y1*n[1])/n[0];
166  x2 = (pos + xc*n[0] + yc*n[1] - y2*n[1])/n[0];
167  }
168 
169  x = x1;
170  y = y1;
171  dx = x2 - x1;
172  dy = y2 - y1;
173 
174  return 0;
175 }
176 
177 
178 void
180 {
181  if(!_sqhit_col) {
182  if(verbosity > 0)
183  LogInfo(!_sqhit_col);
184  return;
185  }
186 
187  for (SQHitVector::ConstIter it = _sqhit_col->begin(); it != _sqhit_col->end(); it++) {
188  try {
189  SQHit * hit = *it;
190  if(!hit) {
191  if(verbosity > 1) LogInfo("!hit");
192  continue;
193  }
194 
195  auto det_id = hit->get_detector_id();
196  auto elm_id = hit->get_element_id();
197  double x, y, dx, dy;
198 
199  if(verbosity > 1) {
200  std::cout
201  << "mTrkEveDisplay::draw_hits: {"
202  << det_id << ", " << elm_id << "} "
203  << std::endl;
204  }
205 
206  if(det_id <= 0 or det_id >= NDETPLANES) {
207  if(verbosity > 2) LogInfo(det_id);
208  continue;
209  }
210 
211  int ret_code = hit_to_wire(det_id, elm_id, x, y, dx, dy);
212  if(ret_code!=0) {
213  if(verbosity > 1) LogInfo("hit_to_wire failed");
214  continue;
215  }
216 
217  const float geom_limit = 1000;
218  if(
219  !(fabs(x) < geom_limit) or
220  !(fabs(y) < geom_limit) or
221  !(fabs(dx) < geom_limit) or
222  !(fabs(dy) < geom_limit)
223  ) {
224  if(verbosity > 2) LogInfo(" {" << x << ", " << y << ", " << dx << ", " << dy << "}");
225  continue;
226  }
227 
228  if(verbosity > 1) {
229  std::cout
230  << "mTrkEveDisplay::draw_hits: {"
231  << det_id << ", " << elm_id << "} => "
232  << " {" << x << ", " << y << ", " << dx << ", " << dy << "}"
233  << std::endl;
234  }
235 
236  _hit_wires[det_id]->AddLine(x, y, dx, dy);
237 
238  _hit_wires[det_id]->QuadId(new TNamed(Form("%d",elm_id),"element id"));
239 
240  if(det_id<31) _hit_wires[det_id]->DigitColor(kBlue);
241  else if(det_id<47) _hit_wires[det_id]->DigitColor(kGreen);
242  else if(det_id<55) _hit_wires[det_id]->DigitColor(kRed);
243  else if(det_id<63) _hit_wires[det_id]->DigitColor(kMagenta);
244  } catch (std::exception &e ) {
245  std::cout << "Exception caught mTrkEveDisplay::draw_hits " << e.what() << std::endl;
246  }
247  }
248 }
249 void
251 {
252  if(!_recEvent) {
253  if(verbosity > 0)
254  LogInfo(!_recEvent);
255  return;
256  }
257 
258  if(verbosity > 0)
259  LogInfo(_recEvent->getNTracks());
260  if(_recEvent->getNTracks()<=0) return;
261 
262  _geom_svc = GeomSvc::instance();
263  double vz_st1 = _geom_svc->getPlaneCenterZ(_geom_svc->getDetectorID("D1X"));
264 
265  std::map<int, int> hitID_ihit;
266  // If using vector, index it first
267  for(int ihit=0; ihit<_sqhit_col->size(); ++ihit) {
268  SQHit *hit = _sqhit_col->at(ihit);
269  int hitID = hit->get_hit_id();
270  hitID_ihit[hitID] = ihit;
271  }
272 
273  for(int itrack=0; itrack<_recEvent->getNTracks(); ++itrack){
274  SRecTrack srecTrack = _recEvent->getTrack(itrack);
275 
276  TEveRecTrackT<double>* eve_rec_trk = new TEveRecTrackT<double>();
277 
278  int charge = srecTrack.getCharge();
279 
280  double vx, vy, vz;
281  double px, py, pz;
282  srecTrack.getMomentumSt1(px, py, pz);
283  srecTrack.getPositionSt1(vx, vy);
284  vz = vz_st1;
285 
286  eve_rec_trk->fV.Set(vx, vy, vz);
287  eve_rec_trk->fP.Set(px, py, pz);
288 
289  if(verbosity) {
290  std::cout << "--------- itrack: " << itrack << " ---------"<< std::endl;
291  std::cout
292  << " { " << vx << "," << vy << "," << vz << " } "
293  << " { " << px << "," << py << "," << pz << " } "
294  << std::endl;
295  }
296 
297 
298  TVector3 rec_vtx = srecTrack.getTargetPos();
299  TVector3 rec_mom = srecTrack.getTargetMom();
300  eve_rec_trk->fV.Set(rec_vtx.x(), rec_vtx.y(), rec_vtx.z());
301  eve_rec_trk->fP.Set(rec_mom.x(), rec_mom.y(), rec_mom.z());
302 
303  eve_rec_trk->fSign = charge;
304  TEveTrack* trk = new TEveTrack(eve_rec_trk, _prop);
305  trk->SetSmooth(kTRUE);
306  if (charge > 0) {
307  trk->SetPdg(-13);
308  trk->SetLineColor(kYellow);
309  }
310  else {
311  trk->SetPdg(13);
312  trk->SetLineColor(kCyan);
313  }
314  trk->SetLineWidth(1);
315 
316  for (int iz = -1; iz<26; ++iz) {
317  double z = iz * 100;
318  double x, y;
319  srecTrack.getExpPositionFast(z, x, y);
320 
321  trk->AddPathMark(TEvePathMarkD(
322  TEvePathMarkD::kDaughter,
323  TEveVectorD(x, y, z)
324  ));
325  }
326 
327 // for(int ihit=0; ihit<srecTrack.getNHits();++ihit) {
328 // int hitID = srecTrack.getHitIndex(ihit);
329 //
330 // // signed hitID to hitID
331 // hitID = abs(hitID);
332 //
333 // if(hitID_ihit.find(hitID)==hitID_ihit.end()) continue;
334 //
335 // SQHit *hit = _sqhit_col->at(hitID_ihit[hitID]);
336 // if(!hit) {LogInfo("!hit");}
337 //
338 // double wx, wy, wz;
339 // double wdx, wdy;
340 //
341 // auto det_id = hit->get_detector_id();
342 // //if(det_id>30) continue;
343 // auto elm_id = hit->get_element_id();
344 // double x, y, dx, dy;
345 // hit_to_wire(det_id, elm_id, wx, wy, wdx, wdy);
346 // wz = _geom_svc->getPlaneCenterZ(det_id);
347 //
348 // trk->AddPathMark(
349 // TEvePathMarkD(TEvePathMarkD::kLineSegment,
350 // TEveVectorD(wx, wy, wz),
351 // TEveVectorD(0, 0, 1),
352 // TEveVectorD(wdx, wdy, 0)));
353 // }
354 
355  trk->SetRnrPoints(kTRUE);
356  trk->SetMarkerStyle(1);
357  trk->SetMarkerSize(1);
358  if (charge > 0)
359  trk->SetMarkerColor(kYellow);
360  else
361  trk->SetMarkerColor(kCyan);
362 
363  trk->MakeTrack();
364  if (verbosity > 2)
365  std::cout << "mTrkEveDisplay - track made. " << std::endl;
366  _reco_tracks->AddElement(trk);
367  }
368 }
369 
370 bool
372 {
373 if(fabs(pid)==211 || fabs(pid)==321 || fabs(pid)==11 || fabs(pid)==13 || fabs(pid)==15 || fabs(pid)==17) return true;
374 
375 if (pid == 0) return true;//always return true
376 return false;
377 }
378 
379 
380 void
382 {
383  _prop->IncDenyDestroy(); // Keep the track propagator from being destroyed
384 
385  for(int i=0; i<NDETPLANES; ++i) {
386  //_hit_wires[i]->DestroyElements();
387  _hit_wires[i]->Reset(TEveQuadSet::kQT_LineXYFixedZ, true, 32);
388  _hit_wires[i]->RefitPlex();
389  TEveTrans& t = _hit_wires[i]->RefMainTrans();
390  t.SetPos(0, 0, _geom_svc->getPlaneCenterZ(i));
391  }
392  _reco_tracks->DestroyElements();
393 }
Int_t getNTracks()
Get tracks.
Definition: SRecEvent.h:447
void get_nodes(PHCompositeNode *topNode)
double getSintheta(int detectorID) const
Definition: GeomSvc.h:202
std::vector< SQHit * >::const_iterator ConstIter
Definition: SQHitVector.h:37
#define NDETPLANES
TEveElementList * get_dc_list() const
Definition: PHEveDisplay.h:57
void getExpPositionFast(Double_t z, Double_t &x, Double_t &y, Int_t iNode=-1)
Definition: SRecEvent.cxx:267
bool event(PHCompositeNode *topNode)
An SQ interface class to hold one detector hit.
Definition: SQHit.h:20
double y2
Definition: GeomSvc.h:86
virtual ConstIter end() const
Definition: SQHitVector.h:59
mTrkEveDisplay(boost::shared_ptr< PHEveDisplay >)
TEveElementList * get_top_list() const
Definition: PHEveDisplay.h:56
#define NULL
Definition: Pdb.h:9
TVector3 getTargetMom()
Definition: SRecEvent.h:192
void end(PHCompositeNode *topNode)
void init(PHCompositeNode *topNode)
int get_verbosity() const
Definition: PHEveDisplay.h:69
int hit_to_wire(const int det, const int elm, double &x, double &y, double &dx, double &dy)
TVector3 getTargetPos()
Definition: SRecEvent.h:187
TEveTrackPropagator * get_cnt_prop() const
Definition: PHEveDisplay.h:55
virtual const SQHit * at(const size_t idkey) const
Definition: SQHitVector.h:53
Double_t getPositionSt1(Double_t &x, Double_t &y) const
Definition: SRecEvent.h:134
virtual short get_detector_id() const
Return the detector ID of this hit.
Definition: SQHit.h:42
TEveManager * _evemanager
double x1
Definition: GeomSvc.h:82
double x2
Definition: GeomSvc.h:85
bool pid_cut(int pid)
TEveElementList * get_hodo_list() const
Definition: PHEveDisplay.h:58
SRecTrack & getTrack(Int_t i)
Definition: SRecEvent.h:448
TEveElementList * get_prop_list() const
Definition: PHEveDisplay.h:59
double getCostheta(int detectorID) const
Definition: GeomSvc.h:201
static GeomSvc * instance()
singlton instance
Definition: GeomSvc.cxx:211
virtual size_t size() const
Definition: SQHitVector.h:50
void init_run(PHCompositeNode *topNode)
#define LogInfo(message)
Definition: DbSvc.cc:14
virtual int get_hit_id() const
Return the ID of this hit.
Definition: SQHit.h:39
int getDetectorID(const std::string &detectorName) const
Get the plane position.
Definition: GeomSvc.h:184
double getPlaneCenterZ(int detectorID)
Definition: GeomSvc.h:213
Plane getPlane(int detectorID) const
Definition: GeomSvc.h:196
void getMeasurement(int detectorID, int elementID, double &measurement, double &dmeasurement)
Convert the detectorID and elementID to the actual hit position.
Definition: GeomSvc.cxx:781
Int_t getCharge() const
Gets.
Definition: SRecEvent.h:100
virtual short get_element_id() const
Return the element ID of this hit.
Definition: SQHit.h:45
virtual ConstIter begin() const
Definition: SQHitVector.h:58
double y1
Definition: GeomSvc.h:83
TEveManager * get_eve_manager() const
Return a pointer to the underlying TEveManager.
Definition: PHEveDisplay.h:54
Double_t getMomentumSt1(Double_t &px, Double_t &py, Double_t &pz) const
Definition: SRecEvent.h:126
TEveElementList * get_dp_list() const
Definition: PHEveDisplay.h:60