Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
InttVertexFinder.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file InttVertexFinder.cc
1 #include "InttVertexFinder.h"
2 #include "InttVertexMapv1.h"
3 #include "InttVertexv1.h"
4 
7 #include <trackbase/TrkrDefs.h>
8 
10 
12 #include <fun4all/SubsysReco.h>
13 
14 #include <phool/PHCompositeNode.h>
15 #include <phool/PHIODataNode.h>
16 #include <phool/PHNode.h>
17 #include <phool/PHNodeIterator.h>
18 #include <phool/PHObject.h> // for PHObject
19 #include <phool/getClass.h>
20 #include <phool/phool.h>
21 
22 #include <TH1.h>
23 
24 #include <cmath>
25 #include <iostream>
26 #include <map> // for _Rb_tree_const_i...
27 #include <memory> // for make_unique, uni...
28 #include <utility> // for move, pair
29 #include <vector> // for vector
30 
32  : SubsysReco(name)
33 {
34 }
35 
37 {
38  delete h_zvtxseed_;
39 }
40 
42 {
43  delete h_zvtxseed_;
44  h_zvtxseed_ = new TH1F("h_zvtxseed", "Zvertex Seed histogram", 200, -50, 50);
45 
47 }
48 
50 {
52  {
54  }
55 
56  //----------------
57  // Report Settings
58  //----------------
59 
60  if (Verbosity() > 0)
61  {
62  std::cout << "====================== InttVertexFinder::InitRun() =====================" << std::endl;
63  std::cout << " beamcenter " << xbeam_ << " " << ybeam_ << std::endl;
64  std::cout << "===========================================================================" << std::endl;
65  }
66 
68 }
69 
71 {
72  PHNodeIterator iter(topNode);
73  PHCompositeNode* dstNode = dynamic_cast<PHCompositeNode*>(iter.findFirst("PHCompositeNode", "DST"));
74  if (!dstNode)
75  {
76  std::cout << PHWHERE << "DST Node missing doing nothing" << std::endl;
78  }
79 
80  // ---
81  PHCompositeNode* inttNode = dynamic_cast<PHCompositeNode*>(iter.findFirst("PHCompositeNode", "INTT"));
82  if (!inttNode)
83  {
84  inttNode = new PHCompositeNode("INTT");
85  dstNode->addNode(inttNode);
86  }
87 
88  m_inttvertexmap = findNode::getClass<InttVertexMap>(inttNode, "InttVertexMap");
89  if (!m_inttvertexmap)
90  {
92  PHIODataNode<PHObject>* VertexMapNode = new PHIODataNode<PHObject>(m_inttvertexmap, "InttVertexMap", "PHObject");
93  inttNode->addNode(VertexMapNode);
94  }
95 
97 }
98 
100 {
101  if (Verbosity() > 5)
102  {
103  std::cout << "Beginning process_event in InttVertexFinder" << std::endl;
104  }
105 
106  m_tGeometry = findNode::getClass<ActsGeometry>(topNode, "ActsGeometry");
107  if (!m_tGeometry)
108  {
109  std::cout << PHWHERE << "No ActsGeometry on node tree. Bailing." << std::endl;
111  }
112 
113  // get node for clusters
114  m_clusterlist = findNode::getClass<TrkrClusterContainer>(topNode, "TRKR_CLUSTER");
115  if (!m_clusterlist)
116  {
117  std::cout << PHWHERE << " ERROR: Can't find TRKR_CLUSTER." << std::endl;
119  }
120 
122 
123  double zcenter, zrms, zmean;
124  double zvertex = calculateZvertex(&zcenter, &zrms, &zmean);
125 
126  auto vertex = std::make_unique<InttVertexv1>();
127  vertex->set_z(zvertex);
128  // vertex->set_z_err(); // no value asigned yet
129 
130  if (Verbosity() > 0)
131  {
132  std::cout << "intt vertex z " << zvertex << std::endl;
133  }
134 
135  m_inttvertexmap->insert(vertex.release());
136 
139 }
140 
142  double* zcenter, double* zrms, double* zmean)
143 {
144  struct ClustInfo
145  {
146  int layer{};
147  int adc{};
149  };
150 
151  int nCluster = 0;
152  // bool exceedNwrite=false;
153  std::vector<ClustInfo> clusters[8][2]; // phi_angle:0-7, layer: inner=0; outer=1
154 
155  for (unsigned int inttlayer = 0; inttlayer < 4; inttlayer++)
156  {
157  int inout = (inttlayer < 2) ? 0 : 1;
158  for (const auto& hitsetkey : m_clusterlist->getHitSetKeys(TrkrDefs::TrkrId::inttId, inttlayer + 3))
159  {
160  auto range = m_clusterlist->getClusters(hitsetkey);
161 
162  for (auto clusIter = range.first; clusIter != range.second; ++clusIter)
163  {
164  const auto cluskey = clusIter->first;
165  const auto cluster = clusIter->second;
166  const auto globalPos = m_tGeometry->getGlobalPosition(cluskey, cluster);
167  // int ladder_z = InttNameSpace::getLadderZId(cluskey);
168  // int ladder_phi = InttNameSpace::getLadderPhiId(cluskey);
169  // int size = cluster->getSize();
170 
171  //--if(nCluster<5) {
172  //-- std::cout<<"xyz : "<<globalPos.x()<<" "<< globalPos.y()<<" "<< globalPos.z()<<" : "
173  //-- <<cluster->getAdc()<<" "<<size<<" "<<inttlayer<<" "<<ladder_z<<" "<<ladder_phi<<std::endl;
174  //--}
175  //--else {
176  //-- if(!exceedNwrite) {
177  //-- std::cout<<" exceed : ncluster limit. no more cluster xyz printed"<<std::endl;
178  //-- exceedNwrite=true;
179  //-- }
180  //--}
181 
182  ClustInfo info;
183  info.layer = inttlayer;
184  info.adc = cluster->getAdc();
185  info.pos = globalPos;
186 
187  double phi = atan2(globalPos.y(), globalPos.x());
188 
189  int iphi = (phi + M_PI) / M_PI / 4.;
190  if (iphi < 0)
191  {
192  iphi += 8;
193  }
194  iphi %= 8;
195  // std::cout<<"phi : "<<phi<<" "<<iphi<<std::endl;
196 
197  clusters[iphi][inout].push_back(info);
198 
199  nCluster++;
200  }
201  }
202  }
203 
204  Acts::Vector3 beamspot(xbeam_, ybeam_, 0);
205  std::vector<double> vz_array;
206 
207  for (auto& cluster : clusters)
208  {
209  for (auto c1 = cluster[0].begin(); c1 != cluster[0].end(); ++c1) // inner
210  {
211  for (auto & c2 : cluster[1]) // outer
212  {
213  if (c1->adc < 40 || c2.adc < 40)
214  {
215  continue;
216  }
217 
218  // TVector3 p1 = c1->pos - beamspot;
219  // TVector3 p2 = c2->pos - beamspot;
220  Acts::Vector3 p1 = c1->pos - beamspot;
221  Acts::Vector3 p2 = c2.pos - beamspot;
222  // skip bad compbination
223  double p1_ang = atan2(p1.y(), p1.x());
224  double p2_ang = atan2(p2.y(), p2.x());
225  double d_ang = p2_ang - p1_ang;
226 
227  if (fabs(d_ang) > 0.2)
228  {
229  continue;
230  }
231 
232  // TVector3 u = p2 - p1;
233  Acts::Vector3 u = p2 - p1;
234  double unorm = sqrt(u.x() * u.x() + u.y() * u.y());
235  if (unorm < 0.00001)
236  {
237  continue;
238  }
239 
240  // unit vector in 2D
241  double ux = u.x() / unorm;
242  double uy = u.y() / unorm;
243  double uz = u.z() / unorm; // same normalization factor(2D) is multiplied
244 
245  Acts::Vector3 p0 = beamspot - p1;
246  // TVector3 p0 = beamspot - p1;
247 
248  double dca_p0 = p0.x() * uy - p0.y() * ux; // cross product of p0 x u
249  double len_p0 = p0.x() * ux + p0.y() * uy; // dot product of p0 . u
250 
251  // beam center in X-Y plane
252  // double vx = len_p0*ux + p1.x();
253  // double vy = len_p0*uy + p1.y();
254 
255  double vz = len_p0 * uz + p1.z();
256 
257  if (fabs(d_ang) < 0.05 && fabs(dca_p0) < 1.0)
258  {
259  h_zvtxseed_->Fill(vz);
260  vz_array.push_back(vz);
261  }
262  }
263  }
264  }
265 
266  // calculate trancated mean of DCA~Z histogram as Z-vertex position
267  double zvtx = -9999.;
268 
269  double zcenter1 = -9999.;
270  double zmean1 = -9999.;
271  double zrms1 = -9999.;
272  if (vz_array.size() > 3)
273  {
274  double zbin = h_zvtxseed_->GetMaximumBin();
275  zcenter1 = h_zvtxseed_->GetBinCenter(zbin);
276  zmean1 = h_zvtxseed_->GetMean();
277  zrms1 = h_zvtxseed_->GetRMS();
278  if (zrms1 < 20)
279  {
280  zrms1 = 20;
281  }
282 
283  double zmax = zcenter1 + zrms1; // 1 sigma
284  double zmin = zcenter1 - zrms1; // 1 sigma
285 
286  double zsum = 0.;
287  int zcount = 0;
288  for (double vz : vz_array)
289  {
290  if (zmin < vz && vz < zmax)
291  {
292  zsum += vz;
293  zcount++;
294  }
295  }
296  if (zcount > 0)
297  {
298  zvtx = zsum / zcount;
299  }
300 
301  if (Verbosity() > 0)
302  {
303  std::cout << "ZVTX: " << zvtx << " " << zcenter1 << " " << zmean1 << " " << zrms1 << " " << zbin << std::endl; //" "<<mbdt.bz<<std::endl;
304  }
305  }
306 
307  if (zcenter != nullptr)
308  {
309  *zcenter = zcenter1;
310  }
311  if (zrms != nullptr)
312  {
313  *zrms = zrms1;
314  }
315  if (zmean != nullptr)
316  {
317  *zmean = zmean1;
318  }
319 
320  return zvtx;
321 }