Belle II Software  release-08-01-10
Hough3DFinder.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 //-----------------------------------------------------------------------------
10 // Description : A class to find 3D tracks using Hough algorithm
11 //-----------------------------------------------------------------------------
12 
13 #define TRG_SHORT_NAMES
14 #define TRGCDC_SHORT_NAMES
15 
16 #include <map>
17 #include "TFile.h"
18 #include "TTree.h"
19 #include <framework/dataobjects/EventMetaData.h>
20 #include "framework/datastore/StoreArray.h"
21 #include "framework/datastore/RelationArray.h"
22 #include "cdc/dataobjects/CDCHit.h"
23 #include "cdc/dataobjects/CDCSimHit.h"
24 #include "cdc/geometry/CDCGeometryPar.h"
25 #include "trg/trg/Debug.h"
26 #include "trg/trg/Utilities.h"
27 #include "trg/cdc/TRGCDC.h"
28 #include "trg/cdc/Layer.h"
29 #include "trg/cdc/Cell.h"
30 #include "trg/cdc/Wire.h"
31 #include "trg/cdc/WireHit.h"
32 #include "trg/cdc/Hough3DFinder.h"
33 #include "trg/cdc/Segment.h"
34 #include "trg/cdc/SegmentHit.h"
35 #include "trg/cdc/Circle.h"
36 #include "trg/cdc/TRGCDCTrack.h"
37 #include "trg/cdc/Link.h"
38 #include "trg/cdc/Relation.h"
39 #include "trg/cdc/Fitter3DUtility.h"
40 #include "trg/cdc/Fitter3D.h"
41 #include "trg/cdc/HandleRoot.h"
42 
43 using namespace std;
44 
45 namespace Belle2 {
51  TRGCDCHough3DFinder::TRGCDCHough3DFinder(const TRGCDC& TRGCDC, bool makeRootFile, int finderMode)
52  : _cdc(TRGCDC), m_makeRootFile(makeRootFile), m_finderMode(finderMode)
53  {
54 
55  m_fileFinder3D = 0;
58 
59  m_mBool["fMc"] = 1;
60  m_mBool["fVerbose"] = 0;
61  m_mBool["fIsPrintError"] = 0;
62  m_mBool["fIsIntegerEffect"] = 1;
63  m_mBool["fmcLR"] = 0;
64  m_mBool["fLRLUT"] = 1;
65  m_mBool["f2DFitDrift"] = 1;
66  m_mBool["f2DFit"] = 1;
67 
68  // For finder3D.
69  m_mBool["debugEfficiency"] = 1;
70  m_mBool["debugNTs"] = 1;
71 
72  m_mConstD["Trg_PI"] = M_PI;
73  // Get rr,zToStraw,angleSt,nWire
75  m_mConstV["rr"] = vector<double> (9);
76  m_mConstV["nWires"] = vector<double> (9);
77  m_mConstV["nTSs"] = vector<double> (9);
78  for (unsigned iSL = 0; iSL < 9; iSL++) {
79  unsigned t_layerId = _cdc.segment(iSL, 0).center().layerId();
80  m_mConstV["rr"][iSL] = cdcp.senseWireR(t_layerId);
81  m_mConstV["nWires"][iSL] = cdcp.nWiresInLayer(t_layerId) * 2;
82  m_mConstV["nTSs"][iSL] = cdcp.nWiresInLayer(t_layerId);
83  }
84  m_mConstV["nTSs2D"] = vector<double> (5);
85  for (unsigned iAx = 0; iAx < 5; iAx++) {
86  m_mConstV["nTSs2D"][iAx] = m_mConstV["nTSs"][2 * iAx];
87  }
88 
89  m_mConstV["zToStraw"] = vector<double> (4);
90  m_mConstV["zToOppositeStraw"] = vector<double> (4);
91  m_mConstV["angleSt"] = vector<double> (4);
92  m_mConstV["nShift"] = vector<double> (4);
93  for (int iSt = 0; iSt < 4; iSt++) {
94  unsigned t_layerId = _cdc.stereoSegment(iSt, 0).center().layerId();
95  m_mConstV["zToStraw"][iSt] = cdcp.senseWireBZ(t_layerId);
96  m_mConstV["zToOppositeStraw"][iSt] = cdcp.senseWireFZ(t_layerId);
97  m_mConstV["angleSt"][iSt] = 2 * m_mConstV["rr"][2 * iSt + 1] * sin(m_mConstD["Trg_PI"] * cdcp.nShifts(t_layerId) /
98  (2 * cdcp.nWiresInLayer(t_layerId))) / (cdcp.senseWireFZ(t_layerId) - cdcp.senseWireBZ(t_layerId));
99  m_mConstV["nShift"][iSt] = cdcp.nShifts(t_layerId);
100  }
101  m_mConstV["rr2D"] = vector<double> (5);
102  m_mConstV["rr3D"] = vector<double> (4);
103  for (int iAx = 0; iAx < 5; iAx++) m_mConstV["rr2D"][iAx] = m_mConstV["rr"][iAx * 2];
104  for (int iSt = 0; iSt < 4; iSt++) m_mConstV["rr3D"][iSt] = m_mConstV["rr"][iSt * 2 + 1];
105  m_mConstV["wirePhi2DError"] = vector<double> (5);
106  m_mConstV["driftPhi2DError"] = vector<double> (5);
107 
109  //m_mConstD["driftResolution"] = 2 * 40 * 0.0001; // (cm)
110  //m_mConstV["cellResolution"] = vector<double> (5);
111  //for(unsigned iAx=0; iAx<5; iAx++){
112  // m_mConstV["cellResolution"][iAx] = m_mConstV["rr2D"][iAx] * 2 * m_mConstD["Trg_PI"] / (m_mConstV["nWires"][iAx*2]/2); // (cm)
113  //}
114  //for(unsigned iAx=0; iAx<5; iAx++){
115  // m_mConstV["wirePhi2DError"][iAx] = m_mConstV["cellResolution"][iAx]/m_mConstV["rr2D"][iAx] / sqrt(12);
116  //}
117  //for(unsigned iAx=0; iAx<5; iAx++){
118  // m_mConstV["driftPhi2DError"][iAx] = m_mConstD["driftResolution"]/m_mConstV["rr2D"][iAx] / sqrt(12);
119  //}
120  m_mConstV["wirePhi2DError"][0] = 0.00085106;
121  m_mConstV["wirePhi2DError"][1] = 0.00039841;
122  m_mConstV["wirePhi2DError"][2] = 0.00025806;
123  m_mConstV["wirePhi2DError"][3] = 0.00019084;
124  m_mConstV["wirePhi2DError"][4] = 0.0001514;
125  m_mConstV["driftPhi2DError"][0] = 0.00085106;
126  m_mConstV["driftPhi2DError"][1] = 0.00039841;
127  m_mConstV["driftPhi2DError"][2] = 0.00025806;
128  m_mConstV["driftPhi2DError"][3] = 0.00019084;
129  m_mConstV["driftPhi2DError"][4] = 0.0001514;
134  //m_mConstV["wireZError"] = vector<double> (4);
135  //m_mConstV["wireZError"][0] = 3.19263;
136  //m_mConstV["wireZError"][1] = 2.8765;
137  //m_mConstV["wireZError"][2] = 2.90057;
138  //m_mConstV["wireZError"][3] = 3.96206;
139  //m_mConstV["driftZError"] = vector<double> (4);
140  //m_mConstV["driftZError"][0] = 3.19263;
141  //m_mConstV["driftZError"][1] = 2.8765;
142  //m_mConstV["driftZError"][2] = 2.90057;
143  //m_mConstV["driftZError"][3] = 3.96206;
144 
145  // (2016.06.07) study
146  //m_mConstV["wireZError"] = vector<double> ({4.752, 6.393, 6.578, 6.418});
147  //m_mConstV["driftZError"] = vector<double> ({0.4701, 0.7203, 0.8058, 0.9382});
148  m_mConstV["driftZError"] = vector<double> ({0.7676, 0.9753, 1.029, 1.372});
149  m_mConstV["wireZError"] = vector<double> ({0.7676, 0.9753, 1.029, 1.372});
150 
151  // Make driftLength table for each superlayer. Up to 511 clock ticks.
152  // driftLengthTableSLX[ tdcCount (~2ns unit) ] = drift length (cm)
153  for (unsigned iSl = 0; iSl < 9; iSl++) {
154  string tableName = "driftLengthTableSL" + to_string(iSl);
155  unsigned tableSize = 512;
156  m_mConstV[tableName] = vector<double> (tableSize);
157  unsigned t_layer = _cdc.segment(iSl, 0).center().layerId();
158  for (unsigned iTick = 0; iTick < tableSize; iTick++) {
159  double t_driftTime = iTick * 2 * cdcp.getTdcBinWidth();
160  double avgDriftLength = 0;
161  if (m_mBool["fXtSimple"] == 1) {
162  avgDriftLength = cdcp.getNominalDriftV() * t_driftTime;
163  } else {
164  double driftLength_0 = cdcp.getDriftLength(t_driftTime, t_layer, 0);
165  double driftLength_1 = cdcp.getDriftLength(t_driftTime, t_layer, 1);
166  avgDriftLength = (driftLength_0 + driftLength_1) / 2;
167  }
168  m_mConstV[tableName][iTick] = avgDriftLength;
169  }
170  }
171 
172  // Save geometry to root file.
173  TVectorD geometryHough3D(16);
174  for (int i = 0; i < 4; i++) {
175  geometryHough3D[i] = m_mConstV["rr"][2 * i + 1] / 100;
176  geometryHough3D[i + 4] = m_mConstV["angleSt"][i];
177  geometryHough3D[i + 8] = m_mConstV["zToStraw"][i] / 100;
178  geometryHough3D[i + 12] = m_mConstV["nWires"][2 * i + 1];
179  }
180 
182  // 1: Hough3DFinder 2: GeoFinder 3: VHDL GeoFinder
184  // Set input file name for VHDL GeoFinder.
185  m_Hough3DFinder->setInputFileName(string(std::getenv("BELLE2_LOCAL_DIR")) + "/data/trg/cdc/GeoFinder.input");
186 
187  // For VHDL GEoFinder
188  //m_Hough3DFinder->setInputFileName("GeoFinder.input");
189  // cotStart, cotEnd, z0Start, z0End, cotSteps, z0Steps
190  float tempInitVariables[] = { -3, 3, -2, 2, 1001, 1001};
191  vector<float > initVariables(tempInitVariables, tempInitVariables + sizeof(tempInitVariables) / sizeof(tempInitVariables[0]));
192  // Save the init variables
193  m_Hough3DFinder->initialize(geometryHough3D, initVariables);
194 
195  m_mConstD["modeHough3D"] = m_Hough3DFinder->getMode();
196  m_mConstV["initVariablesHough3D"] = vector<double> (6);
197  m_mConstV["initVariablesHough3D"][0] = tempInitVariables[0];
198  m_mConstV["initVariablesHough3D"][1] = tempInitVariables[1];
199  m_mConstV["initVariablesHough3D"][2] = tempInitVariables[2];
200  m_mConstV["initVariablesHough3D"][3] = tempInitVariables[3];
201  m_mConstV["initVariablesHough3D"][4] = tempInitVariables[4];
202  m_mConstV["initVariablesHough3D"][5] = tempInitVariables[5];
203 
204  }
205 
207  {
208  }
209 
211  {
212  if (m_makeRootFile) {
213  HandleRoot::writeRoot(m_fileFinder3D);
214  HandleRoot::terminateRoot(m_mRunTVectorD, m_mEventTVectorD, m_mTClonesArray);
215  delete m_fileFinder3D;
216  }
218  }
219 
220  void TRGCDCHough3DFinder::doit(vector<TRGCDCTrack*> const& trackList2D, vector<TRGCDCTrack*>& trackList3D)
221  {
222  // Loop over trackList2D and copy to make a new trackList3D. Will delete it at TRGCDC.cc.
223  for (unsigned int iTrack = 0; iTrack < trackList2D.size(); iTrack++) {
224  TCTrack& aTrack = * new TCTrack(* trackList2D[iTrack]);
225  trackList3D.push_back(& aTrack);
226  }
227  doit(trackList3D);
228  }
229 
230  void TRGCDCHough3DFinder::doit(vector<TRGCDCTrack*>& trackList)
231  {
232 
233  // Assign track ID's.
234  for (unsigned iTrack = 0; iTrack < trackList.size(); iTrack++) {
235  TCTrack& aTrack = *trackList[iTrack];
236  aTrack.setTrackID(iTrack + 1);
237  }
238 
239  if (m_finderMode == 0) doitPerfectly(trackList);
240  if (m_finderMode != 0) doitFind(trackList);
241 
242  }
243 
244  void TRGCDCHough3DFinder::doitFind(vector<TCTrack*>& trackList)
245  {
246  TRGDebug::enterStage("3D finder");
247 
248  // For saving to root file.
249  if (m_makeRootFile) {
250  m_mDouble["iSave"] = 0;
251  HandleRoot::initializeEvent(m_mEventTVectorD, m_mTClonesArray);
252  }
253 
254  // Get event number.
255  StoreObjPtr<EventMetaData> eventMetaDataPtr;
256  // Event starts from 0.
257  m_mDouble["eventNumber"] = eventMetaDataPtr->getEvent();;
258 
259  // Generate arrays for TS candidates.
260  vector<vector<double> > stTSs(4);
261  vector<vector<int> > stTSDrift(4);
262  vector<vector<const TCSHit*> > p_stTSs(4);
263  for (unsigned iSL = 0; iSL < 4; iSL++) {
264  vector<const TCSHit*> hits = _cdc.stereoSegmentHits(iSL);
265  // Initialize vectors.
266  string slName = "st" + to_string(iSL);
267  m_mEventV[slName + "_hit"] = vector<double> ();
268  m_mEventV[slName + "_driftHit"] = vector<double> ();
269  stTSs[iSL] = vector<double> ();
270  stTSDrift[iSL] = vector<int> ();
271  p_stTSs[iSL] = vector<const TCSHit*> ();
272  // Fill vectors
273  for (unsigned iHit = 0; iHit < hits.size(); iHit++) {
274  if (hits[iHit] == 0) continue;
277  //if(hits[iHit]->segment().priorityPosition() != 3) {
278  // continue;
279  //}
280  double t_wirePhi = ((double)hits[iHit]->cell().localId()) / m_mConstV["nWires"][2 * iSL + 1] * 4 * m_mConstD["Trg_PI"];
281  m_mEventV[slName + "_hit"].push_back(t_wirePhi);
282  m_mEventV[slName + "_driftHit"].push_back(TRGCDCFitter3D::calPhi(hits[iHit], _cdc.getEventTime()));
283  int t_tdc = hits[iHit]->segment().priorityTime();
284  int t_lr = hits[iHit]->segment().LUT()->getValue(hits[iHit]->segment().lutPattern());;
285  int t_priorityPosition = hits[iHit]->segment().priorityPosition();
286  int t_driftInfo = (t_tdc << 4) + (t_lr << 2) + t_priorityPosition;
287  stTSs[iSL].push_back(t_wirePhi);
288  p_stTSs[iSL].push_back(hits[iHit]);
289  stTSDrift[iSL].push_back(t_driftInfo);
290  //cout<<"iSL:"<<iSL<<" iHit:"<<iHit<<" t_tdc:"<<t_tdc<<" t_lr:"<<t_lr<<" t_priorityPosition:"<<t_priorityPosition<<" t_driftInfo:"<<t_driftInfo<<endl;
291  }
292  }
293 
294  // Get MC values related with finding
295  // numberTSsForParticle[mcId] = # superlayer hits.
296  map<unsigned, unsigned> numberTSsForParticle;
297  if (m_mBool["fMc"]) findNumberOfHitSuperlayersForMcParticles(p_stTSs, numberTSsForParticle);
298 
299  // Loop over all the tracks.
300  m_mEventD["nTracks"] = trackList.size();
301  for (unsigned iTrack = 0; iTrack < m_mEventD["nTracks"]; iTrack++) {
302 
303  TCTrack& aTrack = * trackList[iTrack];
304 
305  // Get MC values related with fitting
307 
308  // Get track ID
309  m_mDouble["trackId"] = aTrack.getTrackID();
310 
311  // 2D Fitter
312  int fit2DResult = TRGCDCFitter3D::do2DFit(aTrack, m_mBool, m_mConstD, m_mConstV, m_mDouble, m_mVector);
313  if (fit2DResult != 0) continue;
314 
315  // Set input of finder
316  //vector<double > trackVariables = { m_mDouble["charge"], m_mDouble["rho"]/100, m_mDouble["phi0"] } ;
317  vector<double > trackVariables = { m_mDouble["charge2D"], m_mDouble["rho"] / 100, m_mDouble["phi0"] } ;
318 
319  // Run finder
320  m_Hough3DFinder->runFinder(trackVariables, stTSs, stTSDrift);
321 
322  // Get results of finder
323  m_Hough3DFinder->getValues("bestTSIndex", m_mVector["bestTSIndex"]);
324  const TCSHit* p_bestTS[4] = {0, 0, 0, 0};
325  for (int iSt = 0; iSt < 4; iSt++) {
326  if (m_mVector["bestTSIndex"][iSt] == 999) p_bestTS[iSt] = 0;
327  else p_bestTS[iSt] = p_stTSs[iSt][(int)m_mVector["bestTSIndex"][iSt]];
328  }
329  m_Hough3DFinder->getValues("bestTS", m_mVector["bestTS"]);
330  // Find and append TS to track.
331  for (unsigned iSt = 0; iSt < 4; iSt++) {
332  if (m_mVector["bestTS"][iSt] != 999) aTrack.append(new TCLink(0, p_bestTS[iSt], p_bestTS[iSt]->cell().xyPosition()));
333  }
334 
335  // For saving values from finder.
336  if (m_Hough3DFinder->getMode() == 1) {
337  m_Hough3DFinder->getValues("bestZ0", m_mVector["bestZ0"]);
338  m_Hough3DFinder->getValues("bestCot", m_mVector["bestCot"]);
339  m_Hough3DFinder->getValues("houghMax", m_mVector["houghMax"]);
340  m_Hough3DFinder->getValues("minDiffHough", m_mVector["minDiffHough"]);
341  }
342  if (m_Hough3DFinder->getMode() == 2) {
343  m_Hough3DFinder->getValues("st0GeoCandidatesPhi", m_mVector["st0GeoCandidatesPhi"]);
344  m_Hough3DFinder->getValues("st1GeoCandidatesPhi", m_mVector["st1GeoCandidatesPhi"]);
345  m_Hough3DFinder->getValues("st2GeoCandidatesPhi", m_mVector["st2GeoCandidatesPhi"]);
346  m_Hough3DFinder->getValues("st3GeoCandidatesPhi", m_mVector["st3GeoCandidatesPhi"]);
347  m_Hough3DFinder->getValues("st0GeoCandidatesDiffStWires", m_mVector["st0GeoCandidatesDiffStWires"]);
348  m_Hough3DFinder->getValues("st1GeoCandidatesDiffStWires", m_mVector["st1GeoCandidatesDiffStWires"]);
349  m_Hough3DFinder->getValues("st2GeoCandidatesDiffStWires", m_mVector["st2GeoCandidatesDiffStWires"]);
350  m_Hough3DFinder->getValues("st3GeoCandidatesDiffStWires", m_mVector["st3GeoCandidatesDiffStWires"]);
351  m_Hough3DFinder->getValues("stAxPhi", m_mVector["stAxPhi"]);
352  }
353 
354  // Get MC values.
355  if (m_mBool["fMc"]) {
356  // Call storage array.
357  StoreArray<CDCSimHit> SimHits("CDCSimHits");
358  StoreArray<CDCHit> CDCHits("CDCHits");
359  RelationArray relationCDCHits(SimHits, CDCHits);
360 
361  // save performance values (purity, efficiency)
362  const TCRelation& trackRelation3D = aTrack.relation3D();
363  m_mDouble["purity"] = trackRelation3D.purity3D(aTrack.relation2D().contributor(0));
364  m_mDouble["efficiency"] = trackRelation3D.efficiency3D(aTrack.relation2D().contributor(0), numberTSsForParticle);
365  // Find one mc stereo track segment per layer.
366  m_mVector["mcTSs"] = vector<double> (4, 999);
367  vector<const TCSHit*> mcTSList;
368  perfectFinder(trackList, iTrack, mcTSList);
369  for (unsigned iTS = 0; iTS < mcTSList.size(); iTS++) {
370  int iSuperLayer = (int)(double(mcTSList[iTS]->cell().superLayerId()) - 1) / 2;
371  m_mVector["mcTSs"][iSuperLayer] = (double)mcTSList[iTS]->cell().localId() / m_mConstV["nWires"][2 * iSuperLayer + 1] * 4 *
372  m_mConstD["Trg_PI"];
373  }
374  // Save MC ture CDC's hit position
375  m_mVector["mcTSsX"] = vector<double> (4);
376  m_mVector["mcTSsY"] = vector<double> (4);
377  for (unsigned iTS = 0; iTS < mcTSList.size(); iTS++) {
378  unsigned iCDCSimHit = mcTSList[iTS]->iCDCSimHit();
379  CDCSimHit* aCDCSimHit = SimHits[iCDCSimHit];
380  B2Vector3D posWire = aCDCSimHit->getPosWire();
381  m_mVector["mcTSsX"][iTS] = posWire.X();
382  m_mVector["mcTSsY"][iTS] = posWire.Y();
383  }
384  // Calculate diff from perfect
385  m_mVector["perfectWireDiff"] = vector<double> (4);
386  m_mVector["perfectCalZ"] = vector<double> (4);
387  for (unsigned iSt = 0; iSt < 4; iSt++) {
388  if (m_mVector["mcTSs"][iSt] == 999) {
389  m_mVector["perfectWireDiff"][iSt] = 999;
390  m_mVector["perfectCalZ"][iSt] = 999;
391  } else {
392  m_mVector["perfectWireDiff"][iSt] = Fitter3DUtility::calDeltaPhi(m_mDouble["mcCharge"], m_mConstV["angleSt"][iSt],
393  m_mConstV["zToStraw"][iSt] / 100, m_mConstV["rr"][2 * iSt + 1] / 100, m_mVector["mcTSs"][iSt], m_mDouble["rho"] / 100,
394  m_mDouble["phi0"]) / 4 / m_mConstD["Trg_PI"] * m_mConstV["nWires"][2 * iSt + 1];
395  m_mVector["perfectCalZ"][iSt] = Fitter3DUtility::calZ(m_mDouble["mcCharge"], m_mConstV["angleSt"][iSt],
396  m_mConstV["zToStraw"][iSt] / 100, m_mConstV["rr"][2 * iSt + 1] / 100, m_mVector["mcTSs"][iSt], m_mDouble["rho"] / 100,
397  m_mDouble["phi0"]);
398  }
399  }
400  // Find multiple mc stereo track segments per layer.
401  unsigned int mcParticleId = aTrack.relation2D().contributor(0);
402  for (unsigned iSt = 0; iSt < 4; iSt++) {
403  string mcTsName = "mcTsSt" + to_string(iSt);
404  m_mVector[mcTsName] = vector<double> ();
405  vector<const TCSHit*> hits = _cdc.stereoSegmentHits(iSt);
406  // Find TS which match mc index
407  for (unsigned iTS = 0; iTS < hits.size(); iTS++) {
408  if (hits[iTS]->iMCParticle() == mcParticleId) m_mVector[mcTsName].push_back((double)hits[iTS]->cell().localId() /
409  m_mConstV["nWires"][2 * iSt + 1] * 4 * m_mConstD["Trg_PI"]);
410  }
411  }
412 
413  //cout<<"----newStart----"<<endl;
414  //cout<<"mcCharge:"<<m_mDouble["mcCharge"]<<endl;
415  //cout<<"rho:"<<m_mDouble["rho"]<<" phi0:"<<m_mDouble["phi0"]<<endl;
416  //cout<<"purity:"<<m_mDouble["purity"]<<" efficiency:"<<m_mDouble["efficiency"]<<endl;
417  //cout<<"mcTSs[0]:"<<m_mVector["mcTSs"][0]<<" mcTSs[1]:"<<m_mVector["mcTSs"][1]<<" mcTSs[2]:"<<m_mVector["mcTSs"][2]<<" mcTSs[3]:"<<m_mVector["mcTSs"][3]<<endl;
418  //cout<<"mcTSsX[0]:"<<m_mVector["mcTSsX"][0]<<" mcTSsX[1]:"<<m_mVector["mcTSsX"][1]<<" mcTSsX[2]:"<<m_mVector["mcTSsX"][2]<<" mcTSsX[3]:"<<m_mVector["mcTSsX"][3]<<endl;
419  //cout<<"mcTSsY[0]:"<<m_mVector["mcTSsY"][0]<<" mcTSsY[1]:"<<m_mVector["mcTSsY"][1]<<" mcTSsY[2]:"<<m_mVector["mcTSsY"][2]<<" mcTSsY[3]:"<<m_mVector["mcTSsY"][3]<<endl;
420  //cout<<"perfectWireDiff[0]:"<<m_mVector["perfectWireDiff"][0]<<" perfectWireDiff[1]:"<<m_mVector["perfectWireDiff"][1]<<" perfectWireDiff[2]:"<<m_mVector["perfectWireDiff"][2]<<" perfectWireDiff[3]:"<<m_mVector["perfectWireDiff"][3]<<endl;
421  //cout<<"perfectCalZ[0]:"<<m_mVector["perfectCalZ"][0]<<" perfectCalZ[1]:"<<m_mVector["perfectCalZ"][1]<<" perfectCalZ[2]:"<<m_mVector["perfectCalZ"][2]<<" perfectCalZ[3]:"<<m_mVector["perfectCalZ"][3]<<endl;
422  //cout<<"----newEnd----"<<endl;
423  }
424 
425  if (m_makeRootFile) {
426  if (m_fileFinder3D == 0) {
427  m_fileFinder3D = new TFile("Finder3D.root", "RECREATE");
428  HandleRoot::initializeRoot("hough3D", &m_treeConstantsFinder3D, &m_treeTrackFinder3D,
433  );
434  }
435  HandleRoot::saveTrackValues("hough3D",
437  );
438  }
439 
440  } // End of loop over all the tracks.
441 
442  // Will ignore events until event has a track.
444  HandleRoot::saveEventValues("hough3D",
446  );
447  m_treeTrackFinder3D->Fill();
448  }
449 
450 
451  // Set debug values.
452  if (m_mBool["debugEfficiency"]) {
453  // Notify when efficiency is not 1.
454  for (unsigned iTrack = 0; iTrack < trackList.size(); iTrack++) {
455  /* cppcheck-suppress variableScope */
456  TCTrack& aTrack = * trackList[iTrack];
457  // Find number of super layers that have priority layer hit.
458  int nPriorityHitSL = 0;
459  for (unsigned iSt = 0; iSt < 4; iSt++) {
460  int priorityHitSL = 0;
461  for (unsigned iTS = 0; iTS < stTSDrift[iSt].size(); iTS++) {
462  int t_priorityPosition = (stTSDrift[iSt][iTS] & 3);
463  if (t_priorityPosition == 3) priorityHitSL = 1;
464  //cout<<"iSt:"<<iSt<<" iTS:"<<iTS<<" priorityPosition:"<<t_priorityPosition<<" priorityHitSL:"<<priorityHitSL<<endl;
465  }
466  if (priorityHitSL) nPriorityHitSL++;
467  }
468  //cout<<"nPriorityHitSL:"<<nPriorityHitSL<<endl;
469  if (m_mDouble["efficiency"] != 1) {
470  // Remove case when all hits are secondary priority.
471  if (m_mDouble["efficiency"] != nPriorityHitSL * 1. / 4) {
472  aTrack.setDebugValue(TRGCDCTrack::EDebugValueType::find3D, 1);
473  }
474  }
475  }
476  }
477  if (m_mBool["debugNTs"]) {
478  // Notify when not enough TS are found
479  for (unsigned iTrack = 0; iTrack < trackList.size(); iTrack++) {
480  unsigned nHitStSl = 0;
481  for (unsigned iSt = 0; iSt < 4; iSt++) {
482  if (trackList[iTrack]->links(2 * iSt + 1).size() != 0) nHitStSl++;
483  }
484  if (nHitStSl < 2) trackList[iTrack]->setDebugValue(TRGCDCTrack::EDebugValueType::find3D, 1);
485  }
486  }
487 
488  TRGDebug::leaveStage("3D finder");
489 
490  }
491 
492  void TRGCDCHough3DFinder::perfectFinder(vector<TCTrack*>& trackList, unsigned j, vector<const TCSHit*>& mcTSList)
493  {
494 
495  //Just a test
496  StoreArray<CDCHit> CDCHits("CDCHits");
497  StoreArray<CDCSimHit> SimHits("CDCSimHits");
498  RelationArray rels(SimHits, CDCHits);
499 
500  //...G4 trackID...
501  unsigned id = trackList[j]->relation().contributor(0);
502  vector<const TCSHit*> tsList[9];
503  //cout<<"[JB] id: "<<id<<endl;
504 
505  //...Segment loop...
506  const vector<const TCSHit*> hits = _cdc.segmentHits();
507  for (unsigned i = 0; i < hits.size(); i++) {
508  const TCSHit& ts = * hits[i];
509  if (ts.segment().axial()) continue;
510  if (! ts.signal().active()) continue;
511  const TCWHit* wh = ts.segment().center().hit();
512  if (! wh) continue;
513  const unsigned trackId = wh->iMCParticle();
514  // Try to track down the mcParticle another way.
515  //cout<<"[CDCTRG] trackId:"<<trackId<<" "<<ts.cell().name()<<endl;
516 //iw commented out because simind is not used
517 // int ind=wh->iCDCHit();
518 // int simind=rels[ind].getFromIndex();
519  //CDCSimHit &h=*SimHits[simind];
520  //cout<<"[CDCTRG] simTrackId: "<<wh->simHit()->getTrackId()<<endl;;
521  //cout<<"[CDC] simTrackId: "<<h.getTrackId()<<" from CDCHit: "<<CDCHits[ind]->getIWire()<<endl;;
522 
523  if (id == trackId)
524  tsList[wh->wire().superLayerId()].push_back(& ts);
525  }
526 
527  if (TRGDebug::level()) {
528  for (unsigned k = 0; k < 9; k++) {
529  if (k % 2) {
530  cout << TRGDebug::tab(4) << "superlayer " << k << ":";
531  for (unsigned l = 0; l < tsList[k].size(); l++) {
532  if (l)
533  cout << ",";
534  cout << tsList[k][l]->cell().name();
535  }
536  cout << endl;
537  }
538  }
539  }
540 
541  //...Select best one in each super layer...
542  for (unsigned i = 0; i < 9; i++) {
543  const TCSHit* best = 0;
544  if (tsList[i].size() == 0) {
545  continue;
546  } else if (tsList[i].size() == 1) {
547  best = tsList[i][0];
548  } else {
549  int timeMin = 99999;
550  for (unsigned k = 0; k < tsList[i].size(); k++) {
551  const TRGSignal& timing = tsList[i][k]->signal();
552  const TRGTime& t = * timing[0];
553  if (t.time() < timeMin) {
554  timeMin = t.time();
555  best = tsList[i][k];
556  }
557  }
558  }
559  mcTSList.push_back(best);
560  }
561 
562  }
563 
564 
565  void TRGCDCHough3DFinder::doitPerfectly(vector<TCTrack*>& trackList)
566  {
567 
568  TRGDebug::enterStage("Perfect 3D Finder");
569  if (TRGDebug::level())
570  cout << TRGDebug::tab() << "givenTrk#=" << trackList.size() << endl;
571 
572 
573  //...Track loop....
574  for (unsigned j = 0; j < trackList.size(); j++) {
575  //...G4 trackID...
576  TCTrack* trk = trackList[j];
577 
578  vector<const TCSHit*> mcTSList;
579  perfectFinder(trackList, j, mcTSList);
580  for (unsigned iTS = 0; iTS < mcTSList.size(); iTS++) {
581  trk->append(new TCLink(0, mcTSList[iTS], mcTSList[iTS]->cell().xyPosition()));
582  }
583 
584  if (TRGDebug::level())
585  trk->dump("", "> ");
586 
587  }
588 
589  TRGDebug::leaveStage("Perfect 3D Finder");
590 
591  }
592 
593  void TRGCDCHough3DFinder::findNumberOfHitSuperlayersForMcParticles(vector<vector<const TCSHit*> >& p_stTSs,
594  map<unsigned, unsigned>& numberTSsForParticle)
595  {
596  vector<unsigned> mcParticleList;
597  for (unsigned iLayer = 0; iLayer < 4; iLayer++) {
598  // Find what mc particles there are in a layer
599  mcParticleList.clear();
600  for (unsigned iTS = 0; iTS < p_stTSs[iLayer].size(); iTS++) {
601  unsigned iMCParticle = p_stTSs[iLayer][iTS]->iMCParticle();
602  if (find(mcParticleList.begin(), mcParticleList.end(), iMCParticle) == mcParticleList.end()) {
603  mcParticleList.push_back(iMCParticle);
604  }
605  }
606  // Loop over mcParticleList and add to numberTSsForParticle
607  for (unsigned iMCPart = 0; iMCPart < mcParticleList.size(); iMCPart++) {
608  map<unsigned, unsigned>::iterator it = numberTSsForParticle.find(mcParticleList[iMCPart]);
609  if (it != numberTSsForParticle.end()) ++it->second;
610  else numberTSsForParticle[mcParticleList[iMCPart]] = 1;
611  }
612  }
613  }
614 
615 
617 }
618 
DataType X() const
access variable X (= .at(0) without boundary check)
Definition: B2Vector3.h:431
DataType Y() const
access variable Y (= .at(1) without boundary check)
Definition: B2Vector3.h:433
Example Detector.
Definition: CDCSimHit.h:21
B2Vector3D getPosWire() const
The method to get position on wire.
Definition: CDCSimHit.h:199
The Class for CDC Geometry Parameters.
int nShifts(int layerId) const
Returns number shift.
double getTdcBinWidth() const
Return TDC bin width (nsec).
double senseWireBZ(int layerId) const
Returns backward z position of sense wire in each layer.
unsigned nWiresInLayer(int layerId) const
Returns wire numbers in a layer.
double senseWireFZ(int layerId) const
Returns forward z position of sense wire in each layer.
double getNominalDriftV() const
Return the nominal drift velocity of He-ethane gas (default: 4.0x10^-3 cm/nsec).
double getDriftLength(double dt, unsigned short layer, unsigned short lr, double alpha=0., double theta=0.5 *M_PI, bool calculateMinTime=true, double minTime=0.) const
Return the drift dength to the sense wire.
static CDCGeometryPar & Instance(const CDCGeometry *=nullptr)
Static method to get a reference to the CDCGeometryPar instance.
double senseWireR(int layerId) const
Returns radius of sense wire in each layer.
Low-level class to create/modify relations between StoreArrays.
Definition: RelationArray.h:62
Accessor to arrays stored in the data store.
Definition: StoreArray.h:113
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:96
std::map< std::string, std::vector< double > > m_mVector
Map to hold track vector values for Fitter3D.
Definition: Hough3DFinder.h:86
std::map< std::string, TVectorD * > m_mEventTVectorD
TVectorD map for saving event values to root file.
std::map< std::string, bool > m_mBool
Map to hold input options.
Definition: Hough3DFinder.h:96
std::map< std::string, double > m_mConstD
Map to hold run values for Fitter3D.
Definition: Hough3DFinder.h:88
bool m_makeRootFile
Choose whether to save root file.
Definition: Hough3DFinder.h:68
const TRGCDC & _cdc
Members.
Definition: Hough3DFinder.h:66
std::map< std::string, std::vector< double > > m_mConstV
Map to hold run vectcors for Fitter3D.
Definition: Hough3DFinder.h:90
std::map< std::string, double > m_mDouble
Map to hold track double values for Fitter3D.
Definition: Hough3DFinder.h:84
std::map< std::string, double > m_mEventD
Map to hold event values for Fitter3D.
Definition: Hough3DFinder.h:92
std::map< std::string, TClonesArray * > m_mTClonesArray
TClonesArray map for saving track values to root file.
TTree * m_treeConstantsFinder3D
TTree for constants of Hough3D.
Definition: Hough3DFinder.h:82
int m_finderMode
0: perfect finder, 1: Hough3DFinder, 2: GeoFinder, 3: VHDL GeoFinder Choose what finder to use.
Definition: Hough3DFinder.h:71
Hough3DFinder * m_Hough3DFinder
Hough Variables.
Definition: Hough3DFinder.h:74
TFile * m_fileFinder3D
Tfile for Hough3D root file.
Definition: Hough3DFinder.h:78
TTree * m_treeTrackFinder3D
TTree for tracks of Hough3D.
Definition: Hough3DFinder.h:80
std::map< std::string, std::vector< double > > m_mEventV
Map to hold event vectcors for Fitter3D.
Definition: Hough3DFinder.h:94
std::map< std::string, TVectorD * > m_mRunTVectorD
TVectorD map for saving run values to root file.
Definition: Hough3DFinder.h:98
The instance of TRGCDC is a singleton.
Definition: TRGCDC.h:69
A class to represent a digitized signal. Unit is nano second.
Definition: Signal.h:23
A class to represent a signal timing in the trigger system.
Definition: Time.h:25
static double calZ(int charge, double anglest, double ztostraw, double rr, double phi, double rho, double phi0)
Calculates z.
static double calDeltaPhi(int charge, double anglest, double ztostraw, double rr, double phi, double rho, double phi0)
Calculates the phi difference between fitted axial phi and stereo phi.
A class to finded stereo TS hits related to 2D tracks.
void initialize(const TVectorD &geometryVariables, std::vector< float > &initVariables)
Geometry variables.
void setInputFileName(const std::string &inputFileName)
Sets the config file for the GeoFinder.
int getMode(void)
Gets which 3D finder is used.
void setMode(int mode)
Sets which 3D finder to use.
void runFinder(const std::vector< double > &trackVariables, std::vector< std::vector< double > > &stTSs, const std::vector< std::vector< int > > &stTSDrift)
Track variables.
void getValues(const std::string &input, std::vector< double > &result)
Gets results from the 3D finder.
void destruct(void)
Destructs the 3D finder.
std::vector< const TRGCDCSegmentHit * > segmentHits(void) const
returns a list of TRGCDCSegmentHit.
Definition: TRGCDC.h:996
static std::string tab(void)
returns tab spaces.
Definition: Debug.cc:47
const TRGCDCWire & center(void) const
returns a center wire.
Definition: Segment.h:250
static double calPhi(TRGCDCSegmentHit const *segmentHit, double eventTime)
Utility functions.
Definition: Fitter3D.cc:895
std::vector< const TRGCDCSegmentHit * > stereoSegmentHits(unsigned) const
returns a list of TRGCDCSegmentHit in a stereo super layer N.
Definition: TRGCDC.h:1023
unsigned layerId(void) const
returns layer id.
Definition: Cell.h:211
const TRGCDCSegment & stereoSegment(unsigned lyrId, unsigned id) const
returns a track segment in stereo layers. (lyrId is stereo #)
Definition: TRGCDC.h:947
void terminate(void)
Termination method.
static void enterStage(const std::string &stageName)
Declare that you enter new stage.
Definition: Debug.cc:24
void findNumberOfHitSuperlayersForMcParticles(std::vector< std::vector< const TRGCDCSegmentHit * > > &p_stTSs, std::map< unsigned, unsigned > &numberTSsForParticle)
Finds number of hit superlayers for each mc particle.
void perfectFinder(std::vector< TRGCDCTrack * > &trackList, unsigned j, std::vector< const TRGCDCSegmentHit * > &mcTSList)
Perfect 3D finder for a track.
const TRGCDCSegment & segment(unsigned id) const
returns a track segment.
Definition: TRGCDC.h:933
int getEventTime(void) const
returns bad hits(finding invalid hits).
Definition: TRGCDC.cc:2743
void doitPerfectly(std::vector< TRGCDCTrack * > &trackList)
Perfect 3D finder for a tracklist.
static int do2DFit(TRGCDCTrack &aTrack, const std::map< std::string, bool > &m_mBool_in, const std::map< std::string, double > &m_mConstD_in, std::map< std::string, std::vector< double > > &m_mConstV_in, std::map< std::string, double > &m_mDouble_in, std::map< std::string, std::vector< double > > &m_mVector_in)
Does 2D fit. Returns 0 if fit is done successfully. m_mBool should have fIsPrintError,...
Definition: Fitter3D.cc:1259
static int level(void)
returns the debug level.
Definition: Debug.cc:67
static void leaveStage(const std::string &stageName)
Declare that you leave a stage.
Definition: Debug.cc:34
void doitFind(std::vector< TRGCDCTrack * > &trackList)
Finds tracks using tracklist.
static void getMCValues(const TRGCDC &m_cdc_in, TRGCDCTrack *aTrack, const std::map< std::string, double > &m_mConstD_in, std::map< std::string, double > &m_mDouble_in, std::map< std::string, std::vector< double > > &m_mVector_in)
Function for mc debugging.
Definition: Fitter3D.cc:963
void doit(std::vector< TRGCDCTrack * > const &trackList2D, std::vector< TRGCDCTrack * > &trackList3D)
Member functions.
Abstract base class for different kinds of events.