Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AnaMvtxTestBeam2019.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file AnaMvtxTestBeam2019.cc
1 
8 #include "AnaMvtxTestBeam2019.h"
9 
11 #include <fun4all/PHTFileServer.h>
12 
13 #include <phool/getClass.h>
14 // #include <phool/phool.h>
15 // #include <phool/PHCompositeNode.h>
16 // #include <phool/PHIODataNode.h>
17 // #include <phool/PHNodeIterator.h>
18 
19 // #include <trackbase/TrkrDefs.h>
23 #include <trackbase/TrkrHitSetv1.h>
24 
25 // #include <TTree.h>
26 #include <TFile.h>
27 // #include <TLorentzVector.h>
28 #include <TH1D.h>
29 #include <TH2D.h>
30 // #include <TVectorD.h>
31 // #include <TMatrixD.h>
32 // #include <TDecompSVD.h>
33 
34 // #include <iostream>
35 // #include <map>
36 // #include <memory>
37 // #include <utility>
38 // #include <vector>
39 
40 
41 #define LogDebug(exp) std::cout<<"DEBUG: " <<__FILE__<<": "<<__LINE__<<": "<< exp <<"\n"
42 #define LogError(exp) std::cout<<"ERROR: " <<__FILE__<<": "<<__LINE__<<": "<< exp <<"\n"
43 #define LogWarning(exp) std::cout<<"WARNING: " <<__FILE__<<": "<<__LINE__<<": "<< exp <<"\n"
44 
45 
46 //______________________________________________________________________________
48  const std::string &ofName):
49  SubsysReco(name),
50  m_hits(nullptr),
51  m_clusters(nullptr),
52  m_lout_clusterQA(nullptr),
53  m_lout_tracking(nullptr),
54  m_foutname(ofName),
55  do_tracking(false),
56  m_ievent(0),
57  m_ref_align_stave(1)
58 {
59  h1d_nevents = NULL;
60  h1d_hit_layer = NULL;
61  h1d_clu_layer = NULL;
62  h1d_clu_map = NULL;
63 
64  for (int il = 0; il < NLAYER; il++)
65  {
66  h2f_hit_map[il] = NULL;
67  h2f_clu_xz[il] = NULL;
68  h1d_clu_mean_dz[il] = NULL;
69  h1d_clu_mean_dx[il] = NULL;
70  h1d_clu_size[il] = NULL;
71  h1d_clu_size_phi[il] = NULL;
72  h1d_clu_size_z[il] = NULL;
73 
74  htrk_dz[il] = NULL;
75  htrk_dx[il] = NULL;
76 
77  htrk_cut_dz[il] = NULL;
78  htrk_cut_dx[il] = NULL;
79  }
80 
81  htrk = NULL;
82  htrk_clus = NULL;
83  htrk_chi2xy = NULL;
84  htrk_chi2zy = NULL;
85  htrk_cut = NULL;
86  htrk_cut_clus = NULL;
87  htrk_cut_chi2xy = NULL;
88  htrk_cut_chi2zy = NULL;
89 
91 }
92 
93 
94 //______________________________________________________________________________
95 int
97 {
98  int chipColor[] = {kBlue, kRed, kGreen+2, kMagenta+2};
99 
100  //-- Initialize list containe for clusterQA
101  m_lout_clusterQA = new TList();
102  m_lout_clusterQA->SetName("ClusterQA");
103  m_lout_clusterQA->SetOwner(true);
104 
105  //-- Initialize histograms
106  h1d_nevents = new TH1D("h1d_nevents", ";;Nevts", 6, -.5, 5.5);
107  h1d_hit_layer = new TH1D("h1d_hit_layer",
108  "# hits/layer;layer;Counts", 6, -.5, 5.5);
109  h1d_clu_layer = new TH1D("h1d_clu_layer",
110  "# clusters/layer;layer;Counts", 6, -.5, 5.5);
111  h1d_clu_map = new TH1D("h1d_clu_map",
112  "cluster map;cluster_map;Counts", 16, -.5, 15.5);
117 
118  for (int il = 0; il < NLAYER; il++)
119  {
120  h2f_hit_map[il] = new TH2F(Form("h2d_hit_map_l%i", il),
121  ";col [px];row [px]",
122  9 * NCOL, -.5, (9 * NCOL) - .5,
123  NROW, -.5, NROW - .5);
124 
125  h2f_clu_xz[il] = new TH2F(Form("h2d_clu_xz_l%i", il),
126  ";Z [cm];X [cm]",
127  3E4, -15., 15.,
128  2E3, -1., 1.);
129 
130  h1d_clu_mean_dz[il] = new TH1D(Form("h1d_clu_mean_dz_l%i", il),
131  ";#DeltaZ (cm);Counts/30 #mum",
132  3E3, -1.5, 1.5);
133  h1d_clu_mean_dz[il]->SetLineWidth(2);
134  h1d_clu_mean_dz[il]->SetLineColor(chipColor[il]);
135 
136  h1d_clu_mean_dx[il] = new TH1D(Form("h1d_clu_mean_dx_l%i", il),
137  ";#DeltaX (cm);Counts/30 #mum",
138  3E3, -1.5, 1.5);
139  h1d_clu_mean_dx[il]->SetLineWidth(2);
140  h1d_clu_mean_dx[il]->SetLineColor(chipColor[il]);
141 
142  h1d_clu_size[il] = new TH1D(Form("h1d_clu_size_l%i", il),
143  ";cluster size [px];Counts", 100, -.5, 99.5);
144 
145  h1d_clu_size_phi[il] = new TH1D(Form("h1d_clu_size_phi_l%i", il),
146  ";cluster size phi [px];Counts", 100, -.5, 99.5);
147 
148  h1d_clu_size_z[il] = new TH1D(Form("h1d_clu_size_z_l%i", il),
149  ";cluster size z [px];Counts", 100, -.5, 99.5);
150 
151  m_lout_clusterQA->Add(h2f_hit_map[il]);
152  m_lout_clusterQA->Add(h2f_clu_xz[il]);
155  m_lout_clusterQA->Add(h1d_clu_size[il]);
158  } // il
159 
160  if ( do_tracking )
161  {
162  m_lout_tracking = new TList();
163  m_lout_tracking->SetName("Tracking");
164  m_lout_tracking->SetOwner(true);
165 
166  //-- results from tracking
167  htrk = new TH1D("htrk", ";trks / event", 16, -0.5, 15.5);
168  htrk_clus = new TH1D("htrk_clus", ";#cluster/track;Counts", 10, -.5, 9.5);
169  htrk_cut = new TH1D("htrk_cut", ";trks / event", 16, -0.5, 15.5);
170  htrk_cut_clus = new TH1D("htrk_cut_clus", ";#cluster/track;Counts", 5, -.5, 4.5);
171  m_lout_tracking->Add(htrk);
175 
176  for (int il = 0; il < 4; il++)
177  {
178  htrk_dx[il] = new TH1D(Form("htrk_dx_l%i", il),
179  ";track dx [cm]",
180  2000, -.5, .5);
181 
182  htrk_dz[il] = new TH1D(Form("htrk_dz_l%i", il),
183  ";track dz [cm]",
184  2000, -.5, .5);
185 
186  htrk_cut_dx[il] = new TH1D(Form("htrk_cut_dx_l%i", il),
187  ";track dx [pixels]",
188  2000, -.5, .5);
189 
190  htrk_cut_dz[il] = new TH1D(Form("htrk_cut_dz_l%i", il),
191  ";track dz [cm]",
192  2000, -.5, .5);
193 
194  m_lout_tracking->Add(htrk_dx[il]);
195  m_lout_tracking->Add(htrk_dz[il]);
196  m_lout_tracking->Add(htrk_cut_dx[il]);
197  m_lout_tracking->Add(htrk_cut_dz[il]);
198  }
199 
200  htrk_chi2xy = new TH1D("htrk_chi2xy",
201  ";track chi2/ndf in x vs y",
202  500, 0, 100);
203 
204  htrk_chi2zy = new TH1D("htrk_chi2zy",
205  ";track chi2/ndf in z vs y",
206  500, 0, 100);
207 
208  htrk_cut_chi2xy = new TH1D("htrk_cut_chi2xy",
209  ";track chi2/ndf in x vs y",
210  500, 0, 100);
211 
212  htrk_cut_chi2zy = new TH1D("htrk_cut_chi2zy",
213  ";track chi2/ndf in z vs y",
214  500, 0, 100);
215 
220  }
221 
223 }
224 
225 
226 //______________________________________________________________________________
227 int
229 {
230  //-- set counters
231  m_ievent = -1; // since we count at the beginning of the event, start at -1
232 
234 }
235 
236 
237 //______________________________________________________________________________
238 int
240 {
241  if ( Verbosity() > VERBOSITY_MORE )
242  std::cout << "AnaMvtxTestBeam2019::process_event()" << std::endl;
243 
244  // count event
245  m_ievent++;
246  h1d_nevents->Fill(0);
247 
248  //------
249  //-- get the nodes
250  //------
251  int retval = GetNodes(topNode);
252  if ( retval != Fun4AllReturnCodes::EVENT_OK )
253  return retval;
254 
255  h1d_nevents->Fill(1);
256 
257  // loop over each MvtxHitSet object (chip)
258  unsigned int n_hits = 0;
259  auto hitsetrange = m_hits->getHitSets(TrkrDefs::TrkrId::mvtxId);
260  for (auto hitsetitr = hitsetrange.first;
261  hitsetitr != hitsetrange.second;
262  ++hitsetitr)
263  {
264  auto hitset = hitsetitr->second;
265 
266  auto layer = TrkrDefs::getLayer(hitset->getHitSetKey());
267  auto stave = MvtxDefs::getStaveId(hitset->getHitSetKey());
268  auto chip = MvtxDefs::getChipId(hitset->getHitSetKey());
269  if (Verbosity() > VERBOSITY_A_LOT)
270  {
271  std::cout << "layer: " << layer << " stave: " \
272  << stave << " chip: " << chip << std::endl;
273  }
274 
275  //------
276  //-- Loop over hits
277  //------
278  auto hitrangei = hitset->getHits();
279  for (auto hitr = hitrangei.first;
280  hitr != hitrangei.second;
281  ++hitr)
282  {
283  ++n_hits;
284  auto col = MvtxDefs::getCol( hitr->first);
285  auto row = MvtxDefs::getRow( hitr->first);
286  auto stave_col = chip * SegmentationAlpide::NCols + col;
287 
288  h1d_hit_layer->Fill(layer);
289  h2f_hit_map[layer]->Fill(stave_col, row);
290  }
291  }
292 
293  if (m_clusters->size() > n_hits)
294  std::cout << __PRETTY_FUNCTION__ \
295  << " : WARNING!! Nclus " << m_clusters->size() \
296  << " > Nhits " << n_hits << std::endl;
297 
298  //------
299  //-- Loop over clusters
300  //------
301  if (Verbosity() > VERBOSITY_MORE)
302  {
303  std::cout << "-- Looping over clusters." << std::endl;
304  }
305 
306  LyrClusMap m_LyrClusMap;
307 
308  int n_clu_per_layer[NLAYER] = {0};
309  float mean_clu_x[NLAYER] = {0.};
310  float mean_clu_z[NLAYER] = {0.};
311 
312  int n_clus = 0;
313  auto clus_range = m_clusters->getClusters();
314  for (auto iter_clus = clus_range.first;
315  iter_clus != clus_range.second;
316  ++iter_clus)
317  {
318  auto clus = iter_clus->second;
319  auto ckey = clus->getClusKey();
320  auto lyr = TrkrDefs::getLayer(ckey);
321 
322  h1d_clu_layer->Fill(lyr);
323 
324  if ((lyr < 0) || (lyr > 3)) continue;
325 
326  h1d_clu_size[lyr]->Fill(clus->getAdc());
327  h1d_clu_size_phi[lyr]->Fill(clus->getPhiSize()/SegmentationAlpide::PitchRow);
328  h1d_clu_size_z[lyr]->Fill(clus->getZSize()/SegmentationAlpide::PitchCol);
329 
330  h2f_clu_xz[lyr]->Fill(clus->getZ(), clus->getX());
331 
332  n_clu_per_layer[lyr]++;
333  mean_clu_z[lyr] += clus->getZ();
334  mean_clu_x[lyr] += clus->getX();
335 
336  // insert into map
337  m_LyrClusMap.insert(std::make_pair(lyr, clus));
338  n_clus++;
339  }
340 
341  char clu_map = 0;
342  for (int ilyr = 0; ilyr < NLAYER; ++ilyr)
343  {
344  if (n_clu_per_layer[ilyr] > 0)
345  {
346  mean_clu_z[ilyr] /= (float)n_clu_per_layer[ilyr];
347  mean_clu_x[ilyr] /= (float)n_clu_per_layer[ilyr];
348 
349  h1d_clu_mean_dz[ilyr]->Fill(mean_clu_z[ilyr] - mean_clu_z[m_ref_align_stave]);
350  h1d_clu_mean_dx[ilyr]->Fill(mean_clu_x[ilyr] - mean_clu_x[m_ref_align_stave]);
351  clu_map |= ( 1 << ilyr );
352  }
353  }
354  h1d_clu_map->Fill(clu_map);
355 
356  if (Verbosity() > VERBOSITY_MORE)
357  {
358  std::cout << "-- evnt: " << m_ievent << std::endl;
359  std::cout << "-- Nhits: " << n_hits << std::endl;
360  std::cout << "-- Nclus: " << n_clus << std::endl;
361  std::cout << "-- m_LyrClusMap.size(): " << m_LyrClusMap.size() << std::endl;
362  }
363 
364  //------
365  //-- Simple tracking
366  //------
367  if (do_tracking)
368  {
369  //------
370  // Try full tracking
371  //------
373 
374  std::vector<unsigned int> use_layers;
375  use_layers.push_back(3);
376  use_layers.push_back(2);
377  use_layers.push_back(0);
378  use_layers.push_back(1);
379 
380  m_mvtxtracking->RunTracking(m_LyrClusMap, tracklist, use_layers);
381 
382  htrk->Fill(tracklist.size());
383 
384  int ncut = 0;
385  for (unsigned int i = 0; i < tracklist.size(); i++)
386  {
387  htrk_clus->Fill(tracklist.at(i).ClusterList.size());
388  htrk_chi2xy->Fill(tracklist.at(i).chi2_xy);
389  htrk_chi2zy->Fill(tracklist.at(i).chi2_zy);
390 
391  if (tracklist.at(i).chi2_xy < 5 && tracklist.at(i).chi2_zy < 5)
392  {
393  ++ncut;
394  htrk_cut_chi2xy->Fill(tracklist.at(i).chi2_xy);
395  htrk_cut_chi2zy->Fill(tracklist.at(i).chi2_zy);
396  htrk_cut_clus->Fill(tracklist.at(i).ClusterList.size());
397  }
398 
399  for (unsigned int j = 0; j < tracklist.at(i).ClusterList.size(); j++)
400  {
401  auto ckey = tracklist.at(i).ClusterList.at(j)->getClusKey();
402  auto lyr = TrkrDefs::getLayer(ckey);
403 
404  if ((lyr < 0) || (lyr >= 4))
405  {
406  std::cout << PHWHERE << " WARNING: bad layer from track cluster. lyr:" << lyr << std::endl;
407  continue;
408  }
409 
410  htrk_dx[lyr]->Fill(tracklist.at(i).dx.at(j));
411  htrk_dz[lyr]->Fill(tracklist.at(i).dz.at(j));
412 
413  if (tracklist.at(i).chi2_xy < 5 && tracklist.at(i).chi2_zy < 5)
414  {
415  htrk_cut_dx[lyr]->Fill(tracklist.at(i).dx.at(j));
416  htrk_cut_dz[lyr]->Fill(tracklist.at(i).dz.at(j));
417  }
418  } // j
419  } // i
420  htrk_cut->Fill(ncut);
421  }
422  //-- End
424  }
425 
426 
427 //______________________________________________________________________________
428 int
430 {
431  //-- Open file
432  PHTFileServer::get().open(m_foutname, "RECREATE");
433 
435  gDirectory->mkdir(m_lout_clusterQA->GetName())->cd();
436  m_lout_clusterQA->Write();
437 
438  if (m_lout_tracking)
439  {
441  gDirectory->mkdir(m_lout_tracking->GetName())->cd();
442  m_lout_tracking->Write();
443  }
446 
448 }
449 
450 
451 /*
452  * get all the necessary nodes from the node tree
453  */
454 //______________________________________________________________________________
455 int
457 {
458  // Input Hits
459  m_hits = findNode::getClass<TrkrHitSetContainerv1>(topNode, "TRKR_HITSET");
460  if (! m_hits)
461  {
462  std::cout << PHWHERE << " HITSETS node was not found on node tree" << std::endl;
464  }
465  // Input Svtx Clusters
466  m_clusters = findNode::getClass<TrkrClusterContainerv1>(topNode, "TRKR_CLUSTER");
467  if (! m_clusters)
468  {
469  std::cout << PHWHERE << " TRKR_CLUSTER node not found on node tree" << std::endl;
471  }
472 
474 }
475 
476 
477 /*
478  * Simple helper function for calculating the slope between two points
479  */
480 //______________________________________________________________________________
481 double
482 AnaMvtxTestBeam2019::CalcSlope(double x0, double y0, double x1, double y1)
483 {
484  return (y1 - y0) / (x1 - x0);
485 }
486 
487 
488 /*
489  * Simple helper function for calculating intercept
490  */
491 //______________________________________________________________________________
492 double
493 AnaMvtxTestBeam2019::CalcIntecept(double x0, double y0, double m)
494 {
495  return y0 - x0 * m;
496 }
497 
498 
499 /*
500  * Simple helper function for calculating projection
501  */
502 //______________________________________________________________________________
503 double
504 AnaMvtxTestBeam2019::CalcProjection(double x, double m, double b)
505 {
506  return m * x + b;
507 }