Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TrackProjectionTools.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file TrackProjectionTools.C
1 #include "TrackProjectionTools.h"
2 
3 /* STL includes */
4 #include <cassert>
5 
6 /* Fun4All includes */
8 
9 #include <phool/getClass.h>
10 
11 #include <calobase/RawTowerGeomContainer.h>
12 #include <calobase/RawTowerContainer.h>
13 #include <calobase/RawTowerGeom.h>
14 #include <calobase/RawTowerv1.h>
15 
16 #include <calobase/RawClusterContainer.h>
17 #include <calobase/RawCluster.h>
18 
19 #include <g4vertex/GlobalVertexMap.h>
20 #include <g4vertex/GlobalVertex.h>
21 
22 #include <g4eval/CaloEvalStack.h>
24 
25 using namespace std;
26 
28 {
29  _topNode = topNode;
30 }
31 
32 
34 {
35 
36  /* best matching track */
37  SvtxTrack* best_track = NULL;
38  best_track_dr = NAN;
39 
40  /* find name of calorimeter for this cluster */
41  string caloname = "NONE";
42 
43  /* C++11 range loop */
44  for (auto& towit : cluster->get_towermap() )
45  {
47  break;
48  }
49 
50  /* Get track collection with all tracks in this event */
51  SvtxTrackMap* trackmap =
52  findNode::getClass<SvtxTrackMap>(_topNode,"SvtxTrackMap_FastSim");
53  if (!trackmap)
54  {
55  cout << PHWHERE << "SvtxTrackMap node not found on node tree"
56  << endl;
57  }
58 
59  /* Loop over all tracks from BARREL tracking and see if one points to the same
60  * cluster as the reference clusters (i.e. matching ID in the same calorimeter) */
61  /*
62  for (SvtxTrackMap::ConstIter track_itr = trackmap->begin();
63  track_itr != trackmap->end(); track_itr++)
64  {
65  SvtxTrack* track = dynamic_cast<SvtxTrack*>(track_itr->second);
66 
67  if ( caloname == "CEMC" &&
68  track->get_cal_cluster_id(SvtxTrack::CEMC) == cluster->get_id() )
69  {
70  best_track = track;
71  }
72  }
73  */
74 
75  /* If track found with barrel tracking, return it here- if not, proceed with forward tracking below. */
76  if ( best_track )
77  return best_track;
78 
79  /* Cluster / track matching for barrel calorimeters and tracking */
80  float max_dr = 10;
81 
82  /* cluster position for easy reference */
83  float cx = cluster->get_x();
84  float cy = cluster->get_y();
85 
86  /* If track map found: Loop over all tracks to find best match for cluster (forward calorimeters)*/
87  if ( trackmap &&
88  ( caloname == "FEMC" || caloname == "EEMC" ) )
89  {
90  for (SvtxTrackMap::ConstIter track_itr = trackmap->begin();
91  track_itr != trackmap->end(); track_itr++)
92  {
93  /* get pointer to track */
94  SvtxTrack* track = dynamic_cast<SvtxTrack*>(track_itr->second);
95 
96  /* distance between track and cluster */
97  float dr = NAN;
98 
99  /* loop over track states (projections) sotred for this track */
100  for (SvtxTrack::ConstStateIter state_itr = track->begin_states();
101  state_itr != track->end_states(); state_itr++)
102  {
103  /* get pointer to current track state */
104  SvtxTrackState *temp = dynamic_cast<SvtxTrackState*>(state_itr->second);
105 
106  /* check if track state projection name matches calorimeter where cluster was found */
107  if( (temp->get_name()==caloname) )
108  {
109  dr = sqrt( pow( cx - temp->get_x(), 2 ) + pow( cy - temp->get_y(), 2 ) );
110  break;
111  }
112  }
113 
114  /* check dr and update best_track and best_track_dr if this track is closest to cluster */
115  if ( ( best_track_dr != best_track_dr ) ||
116  ( dr < max_dr &&
117  dr < best_track_dr )
118  )
119  {
120  best_track = track;
121  best_track_dr = dr;
122  }
123  }
124  }
125 
126 
127  /* If track found with barrel tracking, return it here- if not, proceed with alternative barrel cluster-track matching below. */
128  if ( best_track )
129  return best_track;
130 
131 
132  /* Cluster / track matching for barrel calorimeters and tracking */
133  float max_dr_barrel = 10;
134 
135  float ctheta = atan2( cluster->get_r() , cluster->get_z() );
136  float ceta = -log( tan( ctheta / 2.0 ) );
137  float cphi = cluster->get_phi();
138 
139  /* If track map found: Loop over all tracks to find best match for cluster (barrel calorimeters)*/
140  if ( trackmap &&
141  ( caloname == "CEMC" ) )
142  {
143  for (SvtxTrackMap::ConstIter track_itr = trackmap->begin();
144  track_itr != trackmap->end(); track_itr++)
145  {
146  /* get pointer to track */
147  SvtxTrack* track = dynamic_cast<SvtxTrack*>(track_itr->second);
148 
149  /* distance between track and cluster */
150  float dr = NAN;
151 
152  /* loop over track states (projections) sotred for this track */
153  for (SvtxTrack::ConstStateIter state_itr = track->begin_states();
154  state_itr != track->end_states(); state_itr++)
155  {
156  /* get pointer to current track state */
157  SvtxTrackState *temp = dynamic_cast<SvtxTrackState*>(state_itr->second);
158 
159  /* check if track state projection name matches calorimeter where cluster was found */
160  if( (temp->get_name()==caloname) )
161  {
162  dr = sqrt( pow( ceta - temp->get_eta(), 2 ) + pow( cphi - temp->get_phi(), 2 ) );
163  break;
164  }
165  }
166 
167  /* check dr and update best_track and best_track_dr if this track is closest to cluster */
168  if ( ( best_track_dr != best_track_dr ) ||
169  (dr < max_dr_barrel &&
170  dr < best_track_dr) )
171  {
172  best_track = track;
173  best_track_dr = dr;
174  }
175  }
176  }
177 
178  return best_track;
179 }
180 
181 RawCluster*
183 {
184  /* track direction */
185  float eta = 0;
186  float phi = 0;
187 
188  /* closest cluster */
189  RawCluster* cluster_min_dr = NULL;
190 
191  // pull the clusters
192  string clusternodename = "CLUSTER_" + detName;
193  RawClusterContainer *clusterList = findNode::getClass<RawClusterContainer>(_topNode,clusternodename.c_str());
194  if (!clusterList) {
195  cerr << PHWHERE << " ERROR: Can't find node " << clusternodename << endl;
196  return NULL;
197  }
198 
199  // loop over all clusters and find nearest
200  double min_r = NAN;
201 
202  for (unsigned int k = 0; k < clusterList->size(); ++k) {
203 
204  RawCluster *cluster = clusterList->getCluster(k);
205 
206  double dphi = atan2(sin(phi-cluster->get_phi()),cos(phi-cluster->get_phi()));
207 
208  double cluster_theta = atan2( cluster->get_r() , cluster->get_z() );
209  double cluster_eta = -log(tan(cluster_theta/2.0));
210 
211  double deta = eta-cluster_eta;
212  double r = sqrt(pow(dphi,2)+pow(deta,2));
213 
214  if (r < min_r) {
215  min_r = r;
216  cluster_min_dr = cluster;
217  }
218  }
219 
220 // if(detName == "FEMC") {
221 // if(!secondFlag){
222 // femc_cldr = min_r;
223 // femc_clE = min_e;
224 // femc_clN = min_n;
225 // }
226 // else{
227 // femc_cldr2 = min_r;
228 // femc_clE2 = min_e;
229 // femc_clN2 = min_n;
230 // }
231 // }
232 // else{
233 // if(!secondFlag){
234 // fhcal_cldr = min_r;
235 // fhcal_clE = min_e;
236 // fhcal_clN = min_n;
237 // }
238 // else{
239 // fhcal_cldr2 = min_r;
240 // fhcal_clE2 = min_e;
241 // fhcal_clN2 = min_n;
242 // }
243 // }
244 //
245  return cluster_min_dr;
246 }
247 
248 
249 
250 float
251 TrackProjectionTools::getE33Barrel( string detName, float phi, float eta ){
252 
253  string towernodename = "TOWER_CALIB_" + detName;
254  // Grab the towers
255  RawTowerContainer* towerList = findNode::getClass<RawTowerContainer>(_topNode, towernodename.c_str());
256  if (!towerList)
257  {
258  std::cout << PHWHERE << ": Could not find node " << towernodename.c_str() << std::endl;
259  return -1;
260  }
261  string towergeomnodename = "TOWERGEOM_" + detName;
262  RawTowerGeomContainer *towergeo = findNode::getClass<RawTowerGeomContainer>(_topNode, towergeomnodename.c_str());
263  if (! towergeo)
264  {
265  cout << PHWHERE << ": Could not find node " << towergeomnodename.c_str() << endl;
266  return -1;
267  }
268 
269  // calculate 3x3 and 5x5 tower energies
270  int binphi = towergeo->get_phibin(phi);
271  int bineta = towergeo->get_etabin(eta);
272 
273  float energy_3x3 = 0.0;
274  float energy_5x5 = 0.0;
275 
276  for (int iphi = binphi-2; iphi <= binphi+2; ++iphi) {
277  for (int ieta = bineta-2; ieta <= bineta+2; ++ieta) {
278 
279  // wrap around
280  int wrapphi = iphi;
281  if (wrapphi < 0) {
282  wrapphi = towergeo->get_phibins() + wrapphi;
283  }
284  if (wrapphi >= towergeo->get_phibins()) {
285  wrapphi = wrapphi - towergeo->get_phibins();
286  }
287 
288  // edges
289  if (ieta < 0) continue;
290  if (ieta >= towergeo->get_etabins()) continue;
291 
292  RawTower* tower = towerList->getTower(ieta,wrapphi);
293  if (tower) {
294  energy_5x5 += tower->get_energy();
295  if (abs(iphi - binphi)<=1 and abs(ieta - bineta)<=1 )
296  energy_3x3 += tower->get_energy();
297  }
298 
299  }
300  }
301 
302  return energy_3x3;
303 }
304 
305 
306 float
307 TrackProjectionTools::getE33Forward( string detName, float tkx, float tky )
308 {
309  float twr_sum = 0;
310 
311  string towernodename = "TOWER_CALIB_" + detName;
312  // Grab the towers
313  RawTowerContainer* towers = findNode::getClass<RawTowerContainer>(_topNode, towernodename.c_str());
314  if (!towers)
315  {
316  std::cout << PHWHERE << ": Could not find node " << towernodename.c_str() << std::endl;
317  return -1;
318  }
319  string towergeomnodename = "TOWERGEOM_" + detName;
320  RawTowerGeomContainer *towergeom = findNode::getClass<RawTowerGeomContainer>(_topNode, towergeomnodename.c_str());
321  if (! towergeom)
322  {
323  cout << PHWHERE << ": Could not find node " << towergeomnodename.c_str() << endl;
324  return -1;
325  }
326 
327  // Locate the central tower
328  float r_dist = 9999.0;
329  int twr_j = -1;
330  int twr_k = -1;
332 
333  RawTowerContainer::ConstRange begin_end = towers->getTowers();
334  RawTowerContainer::ConstIterator itr = begin_end.first;
335  for (; itr != begin_end.second; ++itr)
336  {
337  RawTowerDefs::keytype towerid = itr->first;
338  RawTowerGeom *tgeo = towergeom->get_tower_geometry(towerid);
339 
340  float x = tgeo->get_center_x();
341  float y = tgeo->get_center_y();
342 
343  float temp_rdist = sqrt(pow(tkx-x,2) + pow(tky-y,2)) ;
344  if(temp_rdist< r_dist){
345  r_dist = temp_rdist;
346  twr_j = RawTowerDefs::decode_index1(towerid);
347  twr_k = RawTowerDefs::decode_index2(towerid);
348  }
349 
350  if( (fabs(tkx-x)<(tgeo->get_size_x()/2.0)) &&
351  (fabs(tky-y)<(tgeo->get_size_y()/2.0)) ) break;
352 
353  }
354 
355  // Use the central tower to sum up the 3x3 energy
356  if(twr_j>=0 && twr_k>=0){
357  for(int ij = -1; ij <=1; ij++){
358  for(int ik = -1; ik <=1; ik++){
359  RawTowerDefs::keytype temp_towerid = RawTowerDefs::encode_towerid( calo_id_ , twr_j + ij , twr_k + ik );
360  RawTower *rawtower = towers->getTower(temp_towerid);
361  if(rawtower) twr_sum += rawtower->get_energy();
362  }
363  }
364  }
365 
366  return twr_sum;
367 }