Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TrackProjectorPlaneECAL.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file TrackProjectorPlaneECAL.cc
1 
3 
4 /* Fun4All includes */
8 
10 #include <phool/PHIODataNode.h>
11 
13 
14 #include <phgeom/PHGeomUtility.h>
15 
18 //#include <g4hough/PHG4HoughTransform.h>
20 
21 #include <calobase/RawCluster.h>
22 #include <calobase/RawTowerDefs.h>
23 
24 #include <phfield/PHFieldUtility.h>
25 
26 #include <phgenfit/Fitter.h>
27 #include <phgenfit/PlanarMeasurement.h>
28 #include <phgenfit/Track.h>
29 #include <phgenfit/SpacepointMeasurement.h>
30 
31 #include <GenFit/RKTrackRep.h>
32 #include <GenFit/FieldManager.h>
33 
34 
35 
36 using namespace std;
37 
39  _fitter(nullptr)
40 {
41 
42  TGeoManager* tgeo_manager = PHGeomUtility::GetTGeoManager(topNode);
43  PHField * field = PHFieldUtility::GetFieldMapNode(nullptr, topNode);
44 
45  _fitter = PHGenFit::Fitter::getInstance(tgeo_manager,field,
46  "DafRef",
47  "RKTrackRep", false);
48 
50 
51  if (!_fitter) {
52  cerr << "No fitter found at: " << endl;
53  cerr << PHWHERE << endl;
54  }
55 }
56 
57 bool
58 TrackProjectorPlaneECAL::get_projected_position( SvtxTrack_FastSim * track, RawCluster* cluster, double arr_pos[3], const PROJECTION_SURFACE surf, const float surface_par )
59 {
60  /* set position components to 0 */
61  arr_pos[0] = 0;
62  arr_pos[1] = 0;
63  arr_pos[2] = 0;
64 
65  /* project track */
66  auto state = project_track( track, cluster, surf, surface_par );
67 
68  /* Set position at extrapolate position */
69  if ( state )
70  {
71  arr_pos[0] = state->get_x();
72  arr_pos[1] = state->get_y();
73  arr_pos[2] = state->get_z();
74  return true;
75  }
76 
77  return false;
78 }
79 
80 bool
81 TrackProjectorPlaneECAL::get_projected_momentum( SvtxTrack_FastSim * track, RawCluster *cluster, double arr_mom[3], const PROJECTION_SURFACE surf, const float surface_par )
82 {
83  /* set momentum components to 0 */
84  arr_mom[0] = 0;
85  arr_mom[1] = 0;
86  arr_mom[2] = 0;
87 
88  /* project track */
89  auto state = project_track( track, cluster, surf, surface_par );
90 
91  /* Set momentum at extrapolate position */
92  if ( state )
93  {
94  arr_mom[0] = state->get_px();
95  arr_mom[1] = state->get_py();
96  arr_mom[2] = state->get_pz();
97  return true;
98  }
99 
100  return false;
101 }
102 // Iterate through all tracks in the SvtxTrackMap. For every track, we iterate among all of its TrackStates. Each
103 // track state corresponds to a point in space where the track object has been extrapolated to. Currently, we
104 // extrapolate via coresoftware/simulation/g4simulation/g4trackfastsim/PHG4TrackFastSim.cc. There are four
105 // detectors in which we perform extrapolation to, the CEMC, FEMC, EEMC, and FHCAL. For every track, we find
106 // the best extrapolation to the inputted 'RawCluster' object. We then compare the best states and return the
107 // best extrapolated track.
110 {
111  std::vector< float > distance_from_track_to_cluster;
112  bool there_is_a_track=false;
113 
114  float deltaR = -1;
115 
116  // Iterate all tracks in the trackmap
117  for(SvtxTrackMap::ConstIter track_itr = trackmap->begin(); track_itr != trackmap->end(); track_itr++)
118  {
119  SvtxTrack_FastSim* the_track = dynamic_cast<SvtxTrack_FastSim*>(track_itr->second);
120  /* Check if the_track is null ptr */
121  if(the_track == NULL)
122  {
123  distance_from_track_to_cluster.push_back(NAN);
124  continue;
125  }
126  else
127  {
128  there_is_a_track = true;
129  }
130  float distance_from_state_to_cluster = 9990;
131  // Iterate all track states in the track object
132  for(SvtxTrack::ConstStateIter state_itr = the_track->begin_states(); state_itr != the_track->end_states(); state_itr++)
133  {
134  float distance_from_state_to_cluster_temp = 9999;
135 
136  SvtxTrackState* the_state = dynamic_cast<SvtxTrackState*>(state_itr->second);
137  /* Check if the_state is a NULL pointer */
138  if(the_state==NULL)
139  {
140  cout << "State is NULL, skipping..." << endl;
141  continue;
142  }
143  //cout<<the_state->get_x()<< " : " <<the_state->get_y() << " : " <<the_state->get_z() << endl;
144  distance_from_state_to_cluster_temp = ( sqrt( (cluster->get_x()-the_state->get_x())*(cluster->get_x()-the_state->get_x()) + (cluster->get_y()-the_state->get_y())*(cluster->get_y()-the_state->get_y()) + (cluster->get_z()-the_state->get_z())*(cluster->get_z()-the_state->get_z())));
145 
146  /*cout << "Cluster : " << cluster->get_x() << " " << cluster->get_y() << " " << cluster->get_z() << endl;
147  cout << "State : " << the_state->get_x() << " " << the_state->get_y() << " " << the_state->get_z() << endl;
148  cout << "Name : " << the_state->get_name() << endl;
149  cout << " " << endl;*/
150  if(distance_from_state_to_cluster_temp < distance_from_state_to_cluster)
151  {
152  distance_from_state_to_cluster = distance_from_state_to_cluster_temp;
153  }
154  }
155  if(distance_from_state_to_cluster!=9990)
156  {
157  distance_from_track_to_cluster.push_back(distance_from_state_to_cluster);
158 
159  }
160 
161  /* Position vector of extrapolated track */
162  //double posv[3] = {0.,0.,0.};
163  //if(!get_projected_position( the_track,cluster, posv, TrackProjectorPlaneECAL::PLANE_CYLINDER, -1))
164  // {
165  //std::cout << "Track Projection Position NOT FOUND; next iteration" << std::endl;
166  // distance_from_track_to_cluster.push_back(NAN);
167  // continue;
168  // }
169  // else
170  //{
171  // distance_from_track_to_cluster.push_back( sqrt( (cluster->get_x()-posv[0])*(cluster->get_x()-posv[0]) +
172  // (cluster->get_y()-posv[1])*(cluster->get_y()-posv[1]) +
173  // (cluster->get_z()-posv[2])*(cluster->get_z()-posv[2]) ) );
174  //
175  //}
176  }
177 
178  /* If no track was found, return NULL */
179  if(distance_from_track_to_cluster.size()==0)
180  return NULL;
181  else if(there_is_a_track==false)
182  return NULL;
183  /* Find the track with minimum distance */
184  /* TODO: What if two tracks are within deltaR */
185  float min_distance = 9999;
186  int min_distance_index = 0;
187  for(unsigned i = 0; i<distance_from_track_to_cluster.size(); i++)
188  {
189  if(distance_from_track_to_cluster.at(i)<min_distance)
190  {
191  min_distance = distance_from_track_to_cluster.at(i);
192  min_distance_index = i;
193  }
194  }
195  /* Test min_distance with deltaR */
196  if(deltaR<0)
197  return dynamic_cast<SvtxTrack_FastSim*>(trackmap->get(min_distance_index));
198  else if(min_distance<deltaR)
199  return dynamic_cast<SvtxTrack_FastSim*>(trackmap->get(min_distance_index));
200  else
201  return NULL;
202 }
203 
204 
207 {
208  /* Check if the_track is null ptr */
209  float distance_from_state_to_cluster = 9990;
210  int best_state_index = -1;
211  int count = 0;
212  // Iterate all track states in the track object
213  for(SvtxTrack::ConstStateIter state_itr = the_track->begin_states(); state_itr != the_track->end_states(); state_itr++)
214  {
215  float distance_from_state_to_cluster_temp = 9999;
216 
217  SvtxTrackState* the_state = dynamic_cast<SvtxTrackState*>(state_itr->second);
218  /* Check if the_state is a NULL pointer */
219  if(the_state==NULL)
220  {
221  cout << "State is NULL, skipping..." << endl;
222  continue;
223  }
224  //cout<<the_state->get_x()<< " : " <<the_state->get_y() << " : " <<the_state->get_z() << endl;
225  distance_from_state_to_cluster_temp = ( sqrt( (cluster->get_x()-the_state->get_x())*(cluster->get_x()-the_state->get_x()) + (cluster->get_y()-the_state->get_y())*(cluster->get_y()-the_state->get_y()) + (cluster->get_z()-the_state->get_z())*(cluster->get_z()-the_state->get_z())));
226  if(distance_from_state_to_cluster_temp < distance_from_state_to_cluster)
227  {
228  best_state_index = count;
229  distance_from_state_to_cluster = distance_from_state_to_cluster_temp;
230  }
231  count++;
232  }
233  if(distance_from_state_to_cluster==-1||best_state_index==-1)
234  {
235  cout << "State Map issue, returning NULL" << endl;
236  }
237  count = 0;
238  for(SvtxTrack::ConstStateIter state_itr = the_track->begin_states(); state_itr != the_track->end_states(); state_itr++)
239  {
240  if(count!=best_state_index)
241  {
242  count++;
243  continue;
244  }
245  else
246  {
247  SvtxTrackState* the_state = dynamic_cast<SvtxTrackState*>(state_itr->second);
248  return the_state;
249  }
250  }
251 
252  return NULL;
253 }
254 
255 
256 
257 
258 
259 
260 char
262 {
263  switch(detector){
264  case CEMC:
265  return 'C';
266  break;
267  case EEMC:
268  return 'E';
269  break;
270  case FEMC:
271  return 'F';
272  break;
273  default:
274  cout << "WARNING: get_detector was unable to find a defined detector" << endl;
275  return ' ';
276  }
277 }
278 
281 {
282  auto last_state_iter = --track->end_states();
283  SvtxTrackState * trackstate = last_state_iter->second;
284 
285  if(!trackstate) {
286  cout << "No state found here!" << endl;
287  return NULL;
288  }
289 
290  int _pid_guess = 11;
291  auto rep = unique_ptr<genfit::AbsTrackRep> (new genfit::RKTrackRep(_pid_guess));
292 
293  unique_ptr<genfit::MeasuredStateOnPlane> msop80 = nullptr;
294 
295  {
296  cout << track->size_states() << endl;
297  TVector3 pos(trackstate->get_x(), trackstate->get_y(), trackstate->get_z());
298  cout << pos(0) << " : " << pos(1) << " : " << pos(2) << endl;
299  //pos(0) = 0.0;
300  //pos(1) = 0.0;
301  //pos(2) = 0.0;
302  TVector3 mom(trackstate->get_px(), trackstate->get_py(), trackstate->get_pz());
303  TMatrixDSym cov(6);
304  for (int i = 0; i < 6; ++i) {
305  for (int j = 0; j < 6; ++j) {
306  cov[i][j] = trackstate->get_error(i, j);
307  }
308  }
309  msop80 = unique_ptr<genfit::MeasuredStateOnPlane> (new genfit::MeasuredStateOnPlane(rep.get()));
310  msop80->setPosMomCov(pos, mom, cov);
311  }
312 
313  /* This is where the actual extrapolation of the track to a surface (cylinder, plane, cone, sphere) happens. */
314  try {
315 
316  /* 'rep 'is of type AbsTrackRep (see documentation or header for extrapolation function options ) */
317  if ( surf == SPHERE )
318  rep->extrapolateToSphere(*msop80, surface_par, TVector3(0,0,0), false, false);
319 
320  else if ( surf == CYLINDER )
321  {
322  rep->extrapolateToCylinder(*msop80, surface_par, TVector3(0,0,0), TVector3(0,0,1));
323  }
324  else if ( surf == PLANE_CYLINDER )
325  {
326  // Get position of cluster
327  TVector3 cluster_pos(cluster->get_x(),cluster->get_y(),cluster->get_z());
328  // Get detector normal vector
329  double xvect=0; double yvect = 0; double zvect = 0;
330  switch(detector){
331  case CEMC:
332  xvect = cos(cluster->get_phi());
333  yvect = sin(cluster->get_phi());
334  break;
335  case EEMC:
336  zvect = 1;
337  break;
338  case FEMC:
339  cluster_pos(0)=0;
340  cluster_pos(1)=0;
341  cluster_pos(2)=295;
342  zvect = -1;
343  break;
344  default:
345  cout << "WARNING: detector variable native to TrackProjectorPlaneECAL not defined" << endl;
346  break;
347  }
348  // Get normal vector to detector
349  TVector3 cluster_norm(xvect,yvect,zvect);
350  // Case in which norm vector remained undefined
351  if(xvect==0&&yvect==0&&zvect==0)
352  {
353  cout << "WARNING: Cluster normal vector uninitialized. Aborting track" << endl;
354  return NULL;
355  }
356 
357 
358  // Extrapolate to detector
359  rep->extrapolateToCylinder(*msop80, 95, TVector3(0,0,0), TVector3(0,0,1));
360  //rep->extrapolateToPlane(*msop80, genfit::SharedPlanePtr( new genfit::DetPlane( cluster_pos, cluster_norm ) ), false, false);
361 
362  }
363  else if ( surf == PLANEXY )
364  {
365  rep->extrapolateToPlane(*msop80, genfit::SharedPlanePtr( new genfit::DetPlane( TVector3(0, 0, surface_par), TVector3(0, 0, 1) ) ), false, false);
366 
367  }
368  } catch (...) {
369  cout << "track extrapolateToXX failed" << endl;
370  return NULL;
371  }
372 
373  /* Create SvtxTrackState object as storage version of the track state
374  * to pass projected track state information to other member functions. */
375  SvtxTrackState_v1 *svtx_state = new SvtxTrackState_v1();
376 
377  svtx_state->set_x( msop80->getPos().X() );
378  svtx_state->set_y( msop80->getPos().Y() );
379  svtx_state->set_z( msop80->getPos().Z() );
380 
381  svtx_state->set_px( msop80->getMom().x() );
382  svtx_state->set_py( msop80->getMom().y() );
383  svtx_state->set_pz( msop80->getMom().z() );
384 
385  for (int i = 0; i < 6; i++) {
386  for (int j = i; j < 6; j++) {
387  svtx_state->set_error(i, j, msop80->get6DCov()[i][j]);
388  }
389  }
390 
391  return svtx_state;
392 }
393 
394 void
396 {
397  switch(c){
398  case 'C':
399  case 'c':
400  detector = CEMC;
401  break;
402  case 'E':
403  case 'e':
404  detector = EEMC;
405  break;
406  case 'F':
407  case 'f':
408  detector = FEMC;
409  break;
410  default:
411  cout << "WARNING: set_detector call received unrecognized char " << c << endl;
412  break;
413  }
414 }
415 
417 {
418  RawCluster::TowerConstIterator rtiter = cluster->get_towers().first;
419  //caloid = RawTowerDefs::decode_caloid( rtiter->first );
420  const char *caloid_to_name = RawTowerDefs::convert_caloid_to_name( RawTowerDefs::decode_caloid( rtiter->first ) ).c_str();
421  if(strcmp(caloid_to_name,"EEMC")==0) return 'E';
422  else if(strcmp(caloid_to_name,"CEMC")==0) return 'C';
423  else if(strcmp(caloid_to_name,"FEMC")==0) return 'F';
424  else
425  {
426  cout << "Unrecognized calorimeter. Default to CEMC" << endl;
427  return 'C';
428  }
429 }