FairRoot/PandaRoot
PndFtsHoughTrackerTask.cxx
Go to the documentation of this file.
2 
3 
4 #include <iostream>
5 #include <math.h>
6 
7 // FTS
8 #include "PndGeoFtsPar.h"
9 #include "PndFtsTube.h"
10 #include "PndFtsMapCreator.h"
11 #include "PndFtsHit.h"
12 #include "FairHit.h"
13 
14 
15 // magnetic field
16 #include "FairField.h"
17 
18 
19 // (Hough) tracking
20 #include "PndFtsHoughTrackFinder.h"
22 #include "PndFtsHoughSpace.h"
23 #include "PndTrackCand.h"
24 #include "PndTrack.h"
25 #include "FairTrackParP.h"
26 #include "PndFtsHoughTrackCand.h"
27 
28 // histogramming / plotting
29 #include "TH1.h"
30 #include "TH2.h"
31 #include "TGraph.h"
32 
33 // peak finder
34 #include "TSpectrum2.h"
35 
36 // root IO
37 
38 #include "FairRunAna.h"
39 #include "FairRootManager.h"
40 #include "FairRuntimeDb.h"
41 #include "PndPersistencyTask.h"
42 
43 
44 
45 #include "TString.h"
46 
47 
48 // TODO this list can probably be shorter
49 //#include "PndDetectorList.h"
50 
51 // Root includes
52 #include "TROOT.h"
53 #include "TString.h"
54 #include "TClonesArray.h"
55 #include "TParticlePDG.h"
56 
57 // framework includes
58 #include "FairRootManager.h"
59 #include "FairRun.h"
60 #include "FairRuntimeDb.h"
61 #include "FairRunAna.h"
62 
63 
64 #include "TObjectTable.h"
65 
66 #include "PndFtsMapCreator.h"
67 
68 
69 using std::cout;
70 using std::endl;
71 
72 
73 
74 
75 
76 
77 // ---- Default constructor -------------------------------------------
79 : PndPersistencyTask("PndFtsHoughTrackerTask", verbose),
80  fLogger(FairLogger::GetLogger()),
81  fFtsBranchId(0),
82  fFtsHitArray(0),
83  fFtsMcPoints(0),
84  fFtsParameters(0),
85  fFtsTubeArray(0),
86  fField(0),
87  fTracksArrayName("FTSTrkHough"),
88  fTrackCands(0),
89  fTracks(0),
90  fSaveDebugInfo(kFALSE),
91  fEventNr(0)
92  // fOutFile(0),
93  // fHoughTrackCands(0),
94 {
95  if(3<fVerbose) std::cout << "PndFtsHoughTrackerTask is the tracker ptr " << this << '\n';
96  SetPersistency(persistence);
97 }
98 
99 // ---- Destructor ----------------------------------------------------
101 {
102  if(fVerbose>3) fLogger->Info(MESSAGE_ORIGIN,"Destructor of PndFtsHoughTrackerTask");
103  // TODO Is that correct?!?
104 // delete fHoughTrackCands;
105  delete fTrackCands;
106  delete fTracks;
107  // fOutFile->Close();
108 }
109 
110 
111 
112 
113 // ---- Initialisation ----------------------------------------------
115 {
116  if(fVerbose>3) fLogger->Info(MESSAGE_ORIGIN,"SetParContainers of PndFtsHoughTrackerTask");
117 
118  // FTS parameters
119  FairRuntimeDb *rtdb= FairRun::Instance()->GetRuntimeDb();
120  fFtsParameters=(PndGeoFtsPar*)(rtdb->getContainer("PndGeoFtsPar"));
121 }
122 
123 // ---- Init ----------------------------------------------------------
125 {
126  if(fVerbose>3) Info("Init","Initilization of PndFtsHoughTrackerTask");
127 
128  // Get a handle from the IO manager
129  FairRootManager* ioman = FairRootManager::Instance();
130  if ( ! ioman ) {
131  fLogger->Fatal(MESSAGE_ORIGIN,"RootManager not instantiated, return!");
132  return kFATAL;
133  }
134 
135  // Get a pointer to the previous already existing data level
136  /*
137  <InputDataLevel> = (TClonesArray*) ioman->GetObject("InputDataLevelName");
138  if ( ! <InputLevel> ) {
139  fLogger->Error(MESSAGE_ORIGIN,"No InputDataLevelName array!\n PndFtsHoughTrackerTask will be inactive");
140  return kERROR;
141  }
142  */
143 
144  // Create the TClonesArray for the output data and register
145  // it in the IO manager
146  /*
147  <OutputDataLevel> = new TClonesArray("OutputDataLevelName", 100);
148  ioman->Register("OutputDataLevelName","OutputDataLevelName",<OutputDataLevel>,kTRUE);
149  */
150 
151  // Do whatever else is needed at the initilization stage
152  // Create histograms to be filled
153  // initialize variables
154 
155 
156 
157 
158  // FTS Hits
159  fFtsHitArray= (TClonesArray *)ioman->GetObject("FTSHit");
160  if ( ! fFtsHitArray ) {
161  fLogger->Info(MESSAGE_ORIGIN,"No FTSHit array!");
162  return kERROR;
163  }
164  fFtsMcPoints = dynamic_cast<TClonesArray *> (ioman->GetObject("FTSPoint"));
165  if ( ! fFtsMcPoints ) {
166  fLogger->Info(MESSAGE_ORIGIN,"No McPoints array!");
167  return kERROR;
168  }
169 
170  // FTS Branch
171  fFtsBranchId = ioman->GetBranchId("FTSHit");
172 
173  // FTS Tube Array
174  PndFtsMapCreator mapperFts(fFtsParameters);
175  fFtsTubeArray = mapperFts.FillTubeArray();
176 
177 
178 
179  // B field
180  if(fVerbose>3) Info("Init","Try to get B field.");
181  fField = FairRunAna::Instance()->GetField();
182  if ( ! fField ) {
183  if(fVerbose>3) fLogger->Info(MESSAGE_ORIGIN,"No fField array!");
184  return kERROR;
185  }
186 
187  // Debugging
188  // if (fSaveDebugInfo){
189  // InitOutFileForDebugging();
190  // }
191  // FIXME this caused trouble, but I don't need it
192 // fHoughTrackCands = new TClonesArray("PndFtsHoughTrackCand");
193 // ioman->Register("FTSTrkDebugCand", "HoughTrackCand", fHoughTrackCands, fSaveDebugInfo);
194 
195  //fHoughSpaces = new TClonesArray("PndFtsHoughSpace");
196  //ioman->Register("FTSTrkDebugHS", "HoughSpaces", fHoughSpaces, fSaveDebugInfo);
197 
198 
199  // Output
200  fTrackCands = new TClonesArray("PndTrackCand");
201  fTracks = new TClonesArray("PndTrack");
202  ioman->Register(fTracksArrayName,"FTSTrk", fTracks, GetPersistency()); // for PndTrack
203  ioman->Register(fTracksArrayName+"Cand","FTSTrk", fTrackCands, GetPersistency()); // for PndTrackCand // TODO Is that correct, should it not be FTSTrkCand or something?
204 
205  if(3<fVerbose) Info("Register","Done.");
206 
207  return kSUCCESS;
208 
209 }
210 
211 
212 //void PndFtsHoughTrackerTask::InitOutFileForDebugging(){
213 // fOutFile = FairRootManager::Instance()->GetOutFile();
214 // if (0==fOutFile)
215 // {
216 // std::cout << "InitOutFileForDebugging: Cannot get outfile.\n";
217 // }
218 // else
219 // {
220 // fOutFile->cd();
221 // fOutFile->mkdir("PndFtsHoughTrackerTask");
222 // std::cout << "InitOutFileForDebugging: Outfile initialised for debugging output.\n";
223 // }
224 //}
225 
226 //void PndFtsHoughTrackerTask::AddNewEventToOutFileForDebugging(UInt_t eventNr){
227 // fOutFile = FairRootManager::Instance()->GetOutFile();
228 // if (0==fOutFile)
229 // {
230 // std::cout << "AddNewEventToOutFileForDebugging: Cannot get outfile.\n";
231 // }
232 // else
233 // {
234 // fOutFile->cd();
235 // fOutFile->cd("PndFtsHoughTrackerTask");
236 // fOutFile->mkdir(""+eventNr);
237 // }
238 //}
239 
240 
241 
242 
243 
244 // ---- ReInit -------------------------------------------------------
246 {
247  InitStatus stat=kSUCCESS;
248  if(3<fVerbose) fLogger->Info(MESSAGE_ORIGIN,"Re- Initilization of PndFtsHoughTrackerTask");
249  return stat;
250 }
251 
252 
253 const TVector3 PndFtsHoughTrackerTask::GetFtsHitPosErrors(const PndFtsHit *const ftsHit) const
254 {
255  const PndFtsTube *const tube = GetFtsTube(ftsHit);
256 
257  const Double_t sizeSigmaCoeff = 1.5; // TODO Check value
258  const Double_t rhoError = tube->GetRadIn()/sizeSigmaCoeff;
259  const Double_t zError = tube->GetHalfLength()/sizeSigmaCoeff; // TODO might need additional factor
260 
261  TVector3 hitPosErrors(rhoError,rhoError,zError);
262 
263  return hitPosErrors;
264 }
265 
266 const TMatrixT<Double_t> PndFtsHoughTrackerTask::GetFtsHitCovMatrix(const PndFtsHit *const ftsHit) const
267 {
268  const PndFtsTube *const tube = GetFtsTube(ftsHit);
269 
270  const Double_t sizeSigmaCoeff = 1.5; // TODO Check value
271  const Double_t rhoError = tube->GetRadIn()/sizeSigmaCoeff;
272  const Double_t zError = tube->GetHalfLength()/sizeSigmaCoeff; // TODO might need additional factor
273  TMatrixT<Double_t> rotationMatrix = tube->GetRotationMatrix();
274 
275  TMatrixT<Double_t> unrotatedCovMatrix(3,3);
276  // initialize with 0
277  for (Int_t firstIdx=0; firstIdx < 3; ++firstIdx){
278  for (Int_t secondIdx=0; secondIdx < 3; ++secondIdx){
279  unrotatedCovMatrix[firstIdx][secondIdx] = 0;
280  }
281  }
282  unrotatedCovMatrix[0][0] = pow(rhoError, 2);
283  unrotatedCovMatrix[1][1] = pow(rhoError, 2);
284  unrotatedCovMatrix[2][2] = pow(zError, 2);
285 
286  TMatrixT<Double_t> rotatedCovMatrix = rotationMatrix*unrotatedCovMatrix;
287  rotatedCovMatrix *= rotationMatrix.Transpose(rotationMatrix);
288 
289  return rotatedCovMatrix;
290 }
291 
292 
293 // ---- Exec ----------------------------------------------------------
295 {
296  if(1<fVerbose) Info("Exec","Exec of PndFtsHoughTrackerTask on event %i", fEventNr);
297 
298  // Reset output array
299  if ( ! fTrackCands ) Fatal("Exec", "No track cand array");
300 
301  fTrackCands->Delete();
302  fTracks->Delete();
303 // fHoughTrackCands->Delete();
304  //fHoughSpaces->Delete();
305 
307 
308  if(3<fVerbose) std::cout << "PndFtsHoughTrackFinder::Exec tracker ptr " << this << '\n';
310  // trackFinder.SetMinPeakHeightZxLineParabola(4);
311  // trackFinder.SetMinPeakHeightZxParabola(6);
312  // trackFinder.SetMinPeakHeightZxParabolaLine(4);
313  // trackFinder.SetMinPeakHeightZyLine(4);
314  trackFinder.FindTracks();
315 
316 
317  // have a look at PndMvdRiemannTrackFinderTask line 161 for reference
318  // store the found tracks as PndTrack and PndTrackCand
319  for (Int_t iFoundTrack = 0; iFoundTrack < trackFinder.NTracks(); ++iFoundTrack){
320 
321 // // for debug output get PndFtsHoughTrackCand
322 // // FIXME this caused trouble, but I don't need it
323 // if (0<fSaveDebugInfo) {
324 // PndFtsHoughTrackCand* myHoughCand = new ((*fHoughTrackCands)[iFoundTrack])PndFtsHoughTrackCand(trackFinder.GetHoughTrack(iFoundTrack));
325 // }
326 
327  // get output as PndTrackCand and store into TCA
328  PndTrackCand* myCand = new ((*fTrackCands)[iFoundTrack])PndTrackCand(trackFinder.GetPndTrackCand(iFoundTrack));
329  if (1<fVerbose)
330  {
331  std::cout << "Track " << iFoundTrack << '\n';
332  std::cout << "Links: ";
333  ((FairMultiLinkedData*) myCand)->Print();
334  std::cout << '\n';
335  }
336 
337  myCand->CalcTimeStamp(); // TODO Why is this needed?
338  if (1<fVerbose) trackFinder.GetHoughTrack(iFoundTrack).Print();
339 
340 
341  PndTrack* myTrack = new ((*fTracks)[iFoundTrack])PndTrack(trackFinder.GetPndTrack(iFoundTrack)); // TODO Some parameters are missing
342  if (myCand->GetTimeStamp() == 0){
343  myTrack->SetTimeStamp(0.0001 * (iFoundTrack+1));
344  myCand->SetTimeStamp(0.0001 * (iFoundTrack+1));
345 
346  } else {
347  myTrack->SetTimeStamp(myCand->GetTimeStamp());
348  myTrack->SetTimeStampError(myCand->GetTimeStampError());
349  }
350  myTrack->SetLink(FairLink("FTSHoughTrackCand", iFoundTrack));
351  myTrack->SetTrackCandRef(myCand);
352 
353 
354  if (1<fVerbose) {
355  std::cout << iFoundTrack << ": ";
356  myTrack->Print();
357  }
358  }
359  fTrackCands->Sort();
360  fTracks->Sort();
361 
362 
363 
364  if(3<fVerbose) Info("Exec","End eventloop.");
365  ++fEventNr;
366 }
367 
368 
369 
371 {
372  fTrackCands->Delete();
373  fTracks->Delete();
374 // fHoughTrackCands->Delete();
375 }
376 
377 
378 // ---- Finish --------------------------------------------------------
380 {
381  if(3<fVerbose) Info("Finish","Finish of PndFtsHoughTrackerTask");
382  // Get a handle from the IO manager
383  FairRootManager* ioman = FairRootManager::Instance();
384  if ( ! ioman ) {
385  fLogger->Fatal(MESSAGE_ORIGIN,"RootManager not instantiated, return!");
386  }
387  ioman->Write();
388  if(3<fVerbose) Info("Finish","Found %i tracks.",fTrackCands->GetEntriesFast());
389 }
390 
391 
392 
393 
394 
PndMultiField * fField
Definition: sim_emc_apd.C:97
virtual InitStatus ReInit()
ReInitiliazation of task when the runID changes.
int fVerbose
Definition: poormantracks.C:24
UInt_t fEventNr
Event number for debugging purposes.
virtual void Finish()
Writes output to root file, I guess. Called at the end of the run.
void Print() const
FairField * fField
For B field access.
Interface between PandaRoot (data input and output) and PndFtsHoughTrackFinder (implementation of PR ...
#define verbose
const TMatrixT< Double_t > GetFtsHitCovMatrix(const PndFtsHit *const ftsHit) const
Returns the position covariance matrix (based on FTS straw geometry) for the hit with index hitId in ...
void SetPersistency(Bool_t val=kTRUE)
TClonesArray * fFtsHitArray
Input array of PndFtsHit.
void SetTrackCandRef(PndTrackCand *candPointer)
Definition: PndTrack.h:44
TClonesArray * fTracks
Array of found tracks in PndTrack (for output)
PndTrack GetPndTrack(int i)
Returns the number of found tracks.
virtual void FinishEvent()
When is this executed? After each event?
PndTrackCand GetPndTrackCand(int i)
Returns the track cand. with index i.
Int_t fFtsBranchId
Detector Id of FTS.
TClonesArray * fFtsMcPoints
Input array of McPoints.
Double_t
TString fTracksArrayName
Branch name where to store the Track candidates.
PndFtsHoughTrackerTask(Int_t verbose=0, Bool_t persistence=kTRUE)
Constructor with flags. Can also be used as standard constructor.
FairRuntimeDb * rtdb
Definition: hit_dirc.C:66
TMatrixT< Double_t > GetRotationMatrix() const
Definition: PndFtsTube.cxx:71
virtual void SetParContainers()
Loads the parameter container from the runtime database.
FairLogger * fLogger
Returns pointer to the B field.
virtual void FindTracks()
Performs the track finding.
PndFtsHoughTrackCand GetHoughTrack(int i) const
Returns the track cand. with index i.
Double_t GetHalfLength() const
Definition: PndFtsTube.cxx:80
void CalcTimeStamp()
PndGeoFtsPar * fFtsParameters
Needed for FTS map creator.
virtual InitStatus Init()
Initialization of task at the beginning of a run.
PndLheTrackFinder * trackFinder
Double_t GetRadIn() const
Definition: PndFtsTube.cxx:74
const PndFtsTube * GetFtsTube(const PndFtsHit *const myHit) const
Returns pointer to the FTS tube corresponding to input FTS hit.
ClassImp(PndAnaContFact)
TClonesArray * FillTubeArray()
this function will be used in PndFtsHitProducesRealFast
void Print()
Definition: PndTrack.cxx:39
Implementation of the QA for the Hough transform based FTS PR. Use this for parameter optimization...
virtual void Exec(Option_t *opt)
Executed for each event.
const TVector3 GetFtsHitPosErrors(const PndFtsHit *const ftsHit) const
Returns the position error (based on FTS straw geometry) for the hit with index hitId in the FTS hit ...
TClonesArray * fFtsTubeArray
Input array of PndFtsTube (map of FTS tubes).
TClonesArray * fTrackCands
Array of found track candidates in PndTrackCand (for output)