FairRoot/PandaRoot
PndFtsHoughSpace.h
Go to the documentation of this file.
1 
26 #ifndef PndFtsHoughSpace_H
27 #define PndFtsHoughSpace_H
28 
29 #include "PndFtsHoughTrackerTask.h"
30 #include "PndFtsHoughSpacePeak.h"
32 
33 #include "TH2.h"
34 #include <cmath>
35 #include <vector>
36 #include "Rtypes.h" // for Double_t, Int_t, etc
37 #include "FairLogger.h" // for FairLogger, MESSAGE_ORIGIN
38 
39 #include "PndTrackCandHit.h"
40 #include "TClonesArray.h"
41 
42 // magnetic field
43 #include "FairField.h"
44 #include "TVector3.h"
45 
46 #include "PndFtsHoughTracklet.h"
47 #include "PndFtsHoughTrackCand.h"
48 
49 // For error throwing
50 #include "TString.h"
51 #include <stdexcept>
52 
53 
54 
55 // for saving the path through the Hough space
56 typedef std::vector< Int_t > IdxPath; // helper -- path for one hit
57 typedef std::map<Int_t, IdxPath > HitIdxPathMap; // that is the one I need -- map of hit indices to path for the corresponding hits
58 typedef std::pair<Int_t, IdxPath > HitIdxPathPair; // helper for inserting one pair of hit index and path in map
59 
60 
61 // cout for the above types
62 std::ostream& operator <<(std::ostream& os, const IdxPath& outVector);
64 
65 
66 
67 class PndFtsHoughSpace : public TH2S {
68 public:
69 
70  // Constructors/Destructors ---------
72  const char *name=0,
73  const Int_t refIndex = -1,
74 
76 
77  Double_t zRefPos=0.,
78  Double_t interceptZx=0.,
79 
80  PndFtsHoughTrackCand *associatedTrackCand=0,
81 
82  PndFtsHoughTrackerTask *trackerTask=0
83  );
85 
86  // TH2S ExportTH2S();
87 
88  // General info for peak finders
89  // The peaks contain the following information
90  // peakTheta = theta for peak
91  // peakSecond = Q/pzx for peak (parabola HT)
92  // peakSecond = intercept for peak (line HT) (in z-x- or z-y-plane)
93  //
94  // peakThetaHw = half width of peak in theta
95  // peakSecondHw = half width of peak in second value (see above for what it stands for)
96  //
97  // actualHeight height of the peak in the histogram (in counts)
98 
99 
100 
106  std::vector<PndFtsHoughTracklet> FindAllPeaksScanPathsMergeBins(const UInt_t minHeight);
107 
108 
114  std::vector<PndFtsHoughTracklet> FindAllPeaksScanPathsMergeBinsCalculatingPaths(const UInt_t minHeight);
115 
116 
122  std::vector<PndFtsHoughTracklet> FindAllPeaksWithTSpectrum2(const UInt_t minHeight);
123 
124 
130  std::vector<PndFtsHoughTracklet> FindAllPeaksBinsWoMergingWithSearchWindow(const UInt_t minHeight, const Int_t vicinityLength = 0);
131 
137  std::vector<PndFtsHoughTracklet> FindAllPeaksBlanko(const UInt_t minHeight);
138 
139 
140 
141 
142 
143 
160  void FillHoughSpace();
161 
162  // operators
163  // PndFtsHoughSpace are the same if they contain the same hits, that means if the PndTrackCand are the same, therefore no need to implement that operator here
164 
165  // Accessors -----------------------
166  inline void Print() const;
167 
168  void setVerbose(Int_t verbose) { fVerbose = verbose; };
169 
170  Double_t getInterceptZx() const { return fInterceptZx; };
171 
172  Double_t getZRefPos() const { return fZRefPos; };
173 
174  inline UInt_t GetNHits() const { return fHitId.size(); };
175 
176 
177 private:
178  // for PandaRoot input/output
180 
182  void throwError(const TString s) const{ throw std::runtime_error(s.Data()); };
183 
184  Bool_t setParametersForHsOption(); // set parameters according to the kind of Hough transform I want to do
185  void filterInputHits(); // copies input hits (based on z coordinate and skewed/non-skewed) from fFtsHitArray (all FTS hits) to fHitId (only the hits that qualify for the specific Hough transform)
186  inline void AddHitToHS(UInt_t hitId, Double_t rho);
187  inline void AddHitToHS(FairLink link, Double_t rho);
188  inline Bool_t IsHitFromTubeIdAlreadyAdded(const Int_t tubeIdToAdd);
189  inline const PndFtsHit* getHitFromHS(UInt_t index) const; // gets the FTS hit corresponding to index
190  inline Int_t getHitIdFromHS(UInt_t index) const { return fHitId.at(index).GetHitId(); }; // gets the FTS hit Id corresponding to index
191  inline const TVector3 CalculateHitPosFromIntersectionsWithZxTrackModel(const PndFtsHit *const myHit) const;
192  inline const TVector3 GetRawOrCalculatedHitPos(const PndFtsHit* const myHit) const;
193 
194 
195  void AddHitsToTrackletByCalculating(PndFtsHoughTracklet *currentTracklet, Int_t locmax);
196 
197 
198 
199 
206 
207  // Private Data Members ------------
208  Int_t fVerbose;
209  const Int_t fRefIndex;
210 
213  std::vector<PndTrackCandHit> fHitId;
214 
217 
218 
219  // only hits with a z value (in the laboratory system, zreal) between onlyusehitsfromz and onlyusehitsuptoz will be used for building the houghspace
222  Bool_t fUseNonSkewedStraws; // if kTRUE, then hits from non-skewed straws are used for Hough transform
223  Bool_t fUseSkewedStraws; // if kTRUE, then hits from skewed straws are used for Hough transform
224 
225  Bool_t fKeepBConstant; // set only to kFALSE for testing
226  // If kTRUE the y-component of the B-field is not used in the parabola hough transform
227  // if kFALSE the parabola's shape will be adjusted based on the magnetic field maps
228 
229  // at which z value the values are calculated
230  Double_t fZRefPos; // is used to redefine an origin for the coordinate system (so that the angle definition gives meaningful theta values)
231  // Double_t interceptZx; // cannot be constant, because might need to be reset if set incorrectly (has to be 0 for line HT)
232  // is used to shift the true x values of hits so that they hit the point (zOffset|0) in z-x-plane (value is determined by line fit on chambers1+2)
233  // (zreal=zOffset, xreal=interceptZx) = (zshifted = 0, xshifted = 0)
234  // zshifted = zreal - zRefPos
235  // xshifted = xreal - interceptZx
236  // zreal = zshifted + zRefPos
237  // xreal = xshifted + interceptZx
238  // For the z-x-plane parabola, a shift in x (hitshiftinx) needs to be set (which should be the result of the straight line hough transform before the dipole field)
239  // For the straight line (stations before dipole field) hitshiftinx HAS TO BE ZERO
240 
241  // is used only for parabola in zx plane to shift the true x values of hits so that they hit the point (zOffset|0) in z-x-plane (value is determined by line fit on chambers1+2)
243 
244 
245 
246 
247  // for B field access
248  FairField* fField;
249 
250 
251  // for HoughTransform
253  // if kTRUE will correct the pzx prediction according to values which should be obtained from a line fit mc truth momentum VS. reco momentum with high statistics
254  static const Bool_t fCorrectPzx = kFALSE;
255 
256 
257 
258 
259 
273  inline Bool_t FillHoles(
274  const Int_t lastBinX,
275  const Int_t lastBinY,
276  const Int_t currentBinY,
277  IdxPath * ptrThetaYIdxPathVec
278  );
279 
280 
281 
282  inline Double_t equationParabola(Double_t thetaRad, Double_t hitZShifted, Double_t hitXShifted, Double_t By)
283  {
284  // for parabola equation
285  const Double_t n = 1.;
286  const Double_t e = 1.;
287  const Double_t c = n * e / 2.;
288 
289  Double_t yVal = 1. / c / By * (-hitZShifted * sin(thetaRad) + hitXShifted * cos(thetaRad))/ pow((hitZShifted * cos(thetaRad) + hitXShifted * sin(thetaRad)), 2);
290 
291  // next line is with rotation as in paper (I believe it is incorrect)
292  // value = 1. / c / By * (hitZshifted * sin(realtheta) - hitXshifted * cos(realtheta))/ pow((hitZshifted * cos(realtheta) + hitXshifted * sin(realtheta)), 2);
293  return yVal;
294  };
295 
296  inline Double_t equationParabolaPz(Double_t thetaRad, Double_t hitZShifted, Double_t hitXShifted, Double_t By)
297  {
298  // for parabola equation
299  const Double_t n = 1.;
300  const Double_t e = 1.;
301  const Double_t c = n * e / 2.;
302 
303  // Use shifted x and shifted z for parabola
304  Double_t yVal = c*By*pow((hitZShifted * cos(thetaRad) + hitXShifted * sin(thetaRad)), 2)/(-hitZShifted * sin(thetaRad) + hitXShifted * cos(thetaRad));
305 
306  // next line is with rotation as in paper (I believe it is incorrect)
307  // value = c*By*pow((hitZshifted * cos(realtheta) + hitXshifted * sin(realtheta)), 2)/(hitZshifted * sin(realtheta) - hitXshifted * cos(realtheta));
308 
309  return yVal;
310  };
311 
312  inline Double_t equationLineZxOrZy(Double_t thetaRad, Double_t hitZShifted, Double_t hitXLabSys)
313  { // TODO: Check if that also works in zy plane
314  // calculate b which is the distance of point on line at z = zOffset from z axis
315  Double_t yVal = -tan(thetaRad)*hitZShifted+hitXLabSys;
316  return yVal;
317  }
318 
319 
320  //------
321  //DEBUG
322  //-----
325  inline TString GetDebugOutName(TString title="", Int_t param=-1) const;
328  void WriteHistoOfHoughSpace() const;
331  TH2S MakeEmptyHistoOfSameDimensions(TString specifier="", Int_t index=-1) const;
334  void WriteHistoOfAllPeaks(const std::vector< PndFtsHoughSpacePeak >& peaksToPlot) const;
337  void WriteHistoOfAllPaths() const;
341  inline void PrintFoundTracklets(const std::vector<PndFtsHoughTracklet>& tracklets) const;
342  inline Double_t getByFromBField(Double_t hitXLabSys,
343  Double_t hitYLabSys, Double_t hitZLabSys);
344 
345 public:
347 
348 };
349 
350 
351 
352 
353 
354 
355 // inline functions
357  std::cout << "=========== PndFtsHoughSpace::Print() ==========" << '\n';
358  std::cout << "fZRefPos = " << fZRefPos << '\n';
359  std::cout << "fInterceptZx = " << fInterceptZx << 'n';
360  TH2S::Print();
361 }
362 
363 void PndFtsHoughSpace::PrintFoundTracklets(const std::vector<PndFtsHoughTracklet>& tracklets) const{
364  std::cout << tracklets.size() << " peaks found for " << GetName() << '\n';
365  if (10<fVerbose){
366  for (UInt_t i=0; i< tracklets.size(); ++i)
367  {
368  tracklets[i].Print();
369  }
370  }
371 }
372 
373 
374 const PndFtsHit* PndFtsHoughSpace::getHitFromHS(UInt_t index) const {
375  if (GetNHits() < index) throwError("index too high");
376 
377  Int_t hitIndex = fHitId.at(index).GetHitId();
378  const PndFtsHit *const myHit = (PndFtsHit*) fTrackerTask->GetFtsHit(hitIndex);
379  return myHit;
380 }
381 
382 
384  const PndFtsHit* const myHit
385 ) const {
386  // get hit position
387  TVector3 hitPos;
388  if (kFALSE == myHit->GetSkewed()) {
389  myHit->Position(hitPos);
390  } else {
392  }
393  return hitPos;
394 }
395 
397  if (0==fAssociatedTrackCand) throwError("No track cand associated with Hough space. Cannot calculate possible hit pos. from track model.");
398 
399  const PndFtsTube *ftsTube = fTrackerTask->GetFtsTube(myHit);
400  const TVector3 wireDirection = ftsTube->GetWireDirection();
401  const TVector3 wireCenter = ftsTube->GetPosition();
402 
403  const Double_t hitZLabSys = wireCenter.Z();
404 
405  // calculate xTM according to track model at hitZLabSys
406  const Double_t xTM = fAssociatedTrackCand->getXLabSys(hitZLabSys);
407  // calculate param which is needed for xStraw = xTM
408  // xTM = wireCenter.X() + param*wireDirection.X()
409  const Double_t param = ( xTM - wireCenter.X() ) / wireDirection.X();
410  // calculate corresponding yStraw
411  const Double_t yStraw = wireCenter.Y() + param*wireDirection.Y();
412 
413  TVector3 crossingPosition(xTM,yStraw,hitZLabSys);
414  return crossingPosition;
415 }
416 
417 
419  TString debugOut = "/home/plots/";
420  debugOut+=fTrackerTask->GetEventNr();
421  debugOut += title;
422  if (-1!=fRefIndex){
423  debugOut+="-rId";
424  debugOut+=fRefIndex;
425  }
426  if (-1!=param){
427  debugOut+=param;
428  }
429  debugOut += ".rtg"; // root textual graphics ;) -- actually just a macro // png does not work in this way
430  return debugOut;
431 }
432 
433 
435  Double_t hitYLabSys, Double_t hitZLabSys) {
436  Double_t By = 0.;
437  // set y component of B field constant
438  if (kTRUE == fKeepBConstant) {
439  // do not take B field into account
440  By = 1.;
441  } else {
442  // Use B field information
443  Double_t po[3], BB[3];
444  po[0] = hitXLabSys; // Use magnetic field at real (not shifted) x position
445  po[1] = hitYLabSys;
446  po[2] = hitZLabSys;
447  fField->GetFieldValue(po, BB); //return value in KG (G3)
448  By = BB[1] / 10.; // By is y-component of magnetic field in Tesla
449  }
450  return By;
451 }
452 
453 
454 #endif
MechFsc Print()
void AddHitsToTrackletByCalculating(PndFtsHoughTracklet *currentTracklet, Int_t locmax)
Bool_t IsHitFromTubeIdAlreadyAdded(const Int_t tubeIdToAdd)
void setVerbose(Int_t verbose)
const TVector3 CalculateHitPosFromIntersectionsWithZxTrackModel(const PndFtsHit *const myHit) const
PndFtsHoughTrackCand * fAssociatedTrackCand
hits relevant for this Hough space
friend F32vec4 cos(const F32vec4 &a)
Definition: P4_F32vec4.h:112
std::vector< PndFtsHoughTracklet > FindAllPeaksBinsWoMergingWithSearchWindow(const UInt_t minHeight, const Int_t vicinityLength=0)
Finds all peaks that satisfy the minimum height requirement minHeight.
const Int_t fRefIndex
for debugging output (to which index in some loop does this Hough space and its output belong to) ...
std::vector< Int_t > IdxPath
Int_t i
Definition: run_full.C:25
TH2S MakeEmptyHistoOfSameDimensions(TString specifier="", Int_t index=-1) const
Makes a new empty histogram with the same binning and limits as this.
Double_t equationParabola(Double_t thetaRad, Double_t hitZShifted, Double_t hitXShifted, Double_t By)
Interface between PandaRoot (data input and output) and PndFtsHoughTrackFinder (implementation of PR ...
Double_t fOnlyUseHitsFromZ
Double_t equationParabolaPz(Double_t thetaRad, Double_t hitZShifted, Double_t hitXShifted, Double_t By)
#define verbose
friend F32vec4 sin(const F32vec4 &a)
Definition: P4_F32vec4.h:111
TLorentzVector s
Definition: Pnd2DStar.C:50
int n
const TVector3 GetRawOrCalculatedHitPos(const PndFtsHit *const myHit) const
void WriteHistoOfAllPeaks(const std::vector< PndFtsHoughSpacePeak > &peaksToPlot) const
For writing out peaks in Hough spaces as histograms (for debugging purposes).
TVector3 GetWireDirection() const
Definition: PndFtsTube.cxx:83
std::vector< PndTrackCandHit > fHitId
std::vector< PndFtsHoughTracklet > FindAllPeaksScanPathsMergeBins(const UInt_t minHeight)
Finds all peaks that satisfy the minimum height requirement minHeight.
Bool_t FillHoles(const Int_t lastBinX, const Int_t lastBinY, const Int_t currentBinY, IdxPath *ptrThetaYIdxPathVec)
Makes sure there are no holes in the Hough space by filling them with a line.
std::vector< PndFtsHoughTracklet > FindAllPeaksBlanko(const UInt_t minHeight)
Finds all peaks that satisfy the minimum height requirement minHeight.
Double_t getByFromBField(Double_t hitXLabSys, Double_t hitYLabSys, Double_t hitZLabSys)
void throwError(const TString s) const
For error reporting.
Class for saving the result of one Hough transform for FTS PR.
PndFtsHoughSpace(const char *name=0, const Int_t refIndex=-1, PndFtsHoughSpaceBinning binning=PndFtsHoughSpaceBinning(), Double_t zRefPos=0., Double_t interceptZx=0., PndFtsHoughTrackCand *associatedTrackCand=0, PndFtsHoughTrackerTask *trackerTask=0)
std::ostream & operator<<(std::ostream &os, const IdxPath &outVector)
Double_t
Bool_t setParametersForHsOption()
void WriteHistoOfAllPathsForEachMcTruthTrack() const
For writing out paths from hits which stem from the same MC truth tracks (how the Hough space would b...
void WriteHistoOfAllPaths() const
For writing out paths (how the Hough space was filled seen from one hit) as histograms (for debugging...
std::vector< PndFtsHoughTracklet > FindAllPeaksWithTSpectrum2(const UInt_t minHeight)
Finds all peaks that satisfy the minimum height requirement minHeight.
Class for Hough space based on TH2S (for the moment). Saves the hits which enter this Hough space and...
TVector3 GetPosition() const
Definition: PndFtsTube.cxx:68
UInt_t GetNHits() const
TString name
Double_t getInterceptZx() const
Int_t fFtsBranchId
@ brief FTS Hits
void Print() const
void WriteHistoOfHoughSpace() const
For writing out Hough spaces as histograms (for debugging purposes).
const PndFtsHit * GetFtsHit(UInt_t hitId) const
Returns pointer to the hit with index hitId in the FTS hit array.
Double_t equationLineZxOrZy(Double_t thetaRad, Double_t hitZShifted, Double_t hitXLabSys)
Int_t getHitIdFromHS(UInt_t index) const
const PndFtsTube * GetFtsTube(const PndFtsHit *const myHit) const
Returns pointer to the FTS tube corresponding to input FTS hit.
Double_t fOnlyUseHitsUpToZ
std::vector< PndFtsHoughTracklet > FindAllPeaksScanPathsMergeBinsCalculatingPaths(const UInt_t minHeight)
Finds all peaks that satisfy the minimum height requirement minHeight.
Helper class for Hough space containing binning. Created: 09.02.2015.
HitIdxPathMap fHitThetaYIdxPath
Int_t GetSkewed() const
Definition: PndFtsHit.h:75
Class for saving a FTS track cand. for Hough transform based FTS PR.
Double_t getXLabSys(const Double_t zLabSys) const
UInt_t GetEventNr() const
Returns the save debug flag.
void AddHitToHS(UInt_t hitId, Double_t rho)
void PrintFoundTracklets(const std::vector< PndFtsHoughTracklet > &tracklets) const
const PndFtsHit * getHitFromHS(UInt_t index) const
std::map< Int_t, IdxPath > HitIdxPathMap
PndFtsHoughTrackerTask * fTrackerTask
ClassDef(PndFtsHoughSpace, 1)
std::pair< Int_t, IdxPath > HitIdxPathPair
Double_t getZRefPos() const
static const Bool_t fCorrectPzx
TString GetDebugOutName(TString title="", Int_t param=-1) const
For writing out debugging histograms.
void FillHoughSpace()
Fills the Hough space using the equation which corresponds to the name of the Hough space...