FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairTutorialDet4StraightLineFitter.cxx
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
9 
10 #include "FairLogger.h" // for FairLogger, etc
11 #include "FairRootManager.h" // for FairRootManager
12 #include "FairTrackParam.h" // for FairTrackParam
13 #include "FairTutorialDet4Hit.h" // for FairTutorialDet4Hit
14 
15 #include <TClonesArray.h> // for TClonesArray
16 #include <TF1.h> // for TF1
17 #include <TGraphErrors.h> // for TGraphErrors
18 #include <TVector3.h> // for TVector3
19 #include <set> // for set, set<>::iterator, etc
20 
22  : FairTask("FairTutorialDet4StraightLineFitter")
23  , fHits(nullptr)
24  , fTracks(nullptr)
25  , fVersion(2)
26 {
27  LOG(debug) << "Default Constructor of FairTutorialDet4StraightLineFitter";
28 }
29 
31 {
32  LOG(debug) << "Destructor of FairTutorialDet4StraightLineFitter";
33  if (fTracks) {
34  fTracks->Delete();
35  delete fTracks;
36  }
37 }
38 
40 {
41  LOG(debug) << "SetParContainers of FairTutorialDet4StraightLineFitter";
42  // Load all necessary parameter containers from the runtime data base
43  /*
44  FairRunAna* ana = FairRunAna::Instance();
45  FairRuntimeDb* rtdb=ana->GetRuntimeDb();
46 
47  <FairTutorialDet4StraightLineFitterDataMember> = (<ClassPointer>*)
48  (rtdb->getContainer("<ContainerName>"));
49  */
50 }
51 
53 {
54  LOG(debug) << "Initilization of FairTutorialDet4StraightLineFitter";
55 
56  // Get a handle from the IO manager
58 
59  // Get a pointer to the previous already existing data level
60 
61  fHits = static_cast<TClonesArray*>(ioman->GetObject("TutorialDetHit"));
62  if (!fHits) {
63  LOG(error) << "No InputDataLevelName array!\n"
64  << "FairTutorialDet4StraightLineFitter will be inactive";
65  return kERROR;
66  }
67 
68  // Create the TClonesArray for the output data and register
69  // it in the IO manager
70  fTracks = new TClonesArray("FairTrackParam", 100);
71  ioman->Register("TutorialDetTrack", "TutorialDet", fTracks, kTRUE);
72 
73  // Do whatever else is needed at the initilization stage
74  // Create histograms to be filled
75  // initialize variables
76 
77  return kSUCCESS;
78 }
79 
81 {
82  LOG(debug) << "Initilization of FairTutorialDet4StraightLineFitter";
83  return kSUCCESS;
84 }
85 
86 void FairTutorialDet4StraightLineFitter::Exec(Option_t* /*option*/)
87 {
88  LOG(debug) << "Exec of FairTutorialDet4StraightLineFitter";
89 
90  if (!IsGoodEvent()) {
91  return;
92  }
93 
94  // Declare some variables
95  FairTutorialDet4Hit* hit = nullptr;
96  /*
97  Int_t detID = 0; // Detector ID
98  Int_t trackID = 0; // Track index
99  Double_t x, y, z; // Position
100  Double_t dx = 0.1; // Position error
101  Double_t tof = 0.; // Time of flight
102 */
103  TVector3 pos, dpos; // Position and error vectors
104 
105  // Loop over TofPoints
106  Int_t nHits = fHits->GetEntriesFast();
107  Float_t* ZPos = new Float_t[nHits];
108  Float_t* XPos = new Float_t[nHits];
109  Float_t* XPosErr = new Float_t[nHits];
110  Float_t* YPos = new Float_t[nHits];
111  Float_t* YPosErr = new Float_t[nHits];
112 
113  for (Int_t iHit = 0; iHit < nHits; iHit++) {
114  hit = static_cast<FairTutorialDet4Hit*>(fHits->At(iHit));
115  if (!hit) {
116  continue;
117  }
118 
119  XPos[iHit] = hit->GetX();
120  YPos[iHit] = hit->GetY();
121  ZPos[iHit] = hit->GetZ();
122 
123  XPosErr[iHit] = hit->GetDx();
124  YPosErr[iHit] = hit->GetDy();
125  }
126 
127  TF1* f1 = new TF1("f1", "[0]*x + [1]");
128  TGraphErrors* LineGraph;
129 
130  LineGraph = new TGraphErrors(nHits, ZPos, XPos, 0, XPosErr);
131  LineGraph->Fit("f1", "Q");
132  Double_t SlopeX = f1->GetParameter(0);
133  Double_t OffX = f1->GetParameter(1);
134  Double_t Chi2X = f1->GetChisquare();
135  Double_t SlopeY = 0.;
136  Double_t OffY = 0.;
137  Double_t Chi2Y;
138 
139  if (2 == fVersion) {
140  LineGraph = new TGraphErrors(nHits, ZPos, YPos, 0, YPosErr);
141  LineGraph->Fit("f1", "Q");
142  SlopeY = f1->GetParameter(0);
143  OffY = f1->GetParameter(1);
144  Chi2Y = f1->GetChisquare();
145 
146  LOG(debug) << XPos[0] << "," << XPos[nHits - 1] << "," << YPos[0] << "," << YPos[nHits - 1] << "," << ZPos[0]
147  << "," << ZPos[nHits - 1];
148  Double_t XSlope = (XPos[nHits - 1] - XPos[0]) / (ZPos[nHits - 1] - ZPos[0]);
149  Double_t YSlope = (YPos[nHits - 1] - YPos[0]) / (ZPos[nHits - 1] - ZPos[0]);
150 
151  LOG(debug) << "Slope(x,y): " << SlopeX << " ," << SlopeY;
152  LOG(debug) << "Slope1(x,y): " << XSlope << " ," << YSlope;
153  LOG(debug) << "Offset(x,y): " << OffX << " ," << OffY;
154  LOG(debug) << "Chi2(x,y): " << Chi2X << " ," << Chi2Y;
155  }
156 
157  FairTrackParam* track = new FairTrackParam();
158  track->SetX(OffX);
159  track->SetTx(SlopeX);
160  track->SetZ(0.);
161  if (2 == fVersion) {
162  track->SetY(OffY);
163  track->SetTy(SlopeY);
164  }
165  new ((*fTracks)[0]) FairTrackParam(*track);
166  // const TMatrixFSym matrix;
167  // Double_t Z = 0.;
168  // new ((*fTracks)[0]) FairTrackParam(OffX, OffY, Z, SlopeX, SlopeY, matrix);
169 
170  delete[] ZPos;
171  delete[] XPos;
172  delete[] XPosErr;
173  delete[] YPos;
174  delete[] YPosErr;
175 }
176 
177 Bool_t FairTutorialDet4StraightLineFitter::IsGoodEvent()
178 {
179  // Check if each for the event there is maximum 1 hit per detector
180  // station. In the moment we create tracks with all hits in the
181  // event, so we have to check for this.
182  // In the end the algorithm should be able to work also with
183  // missing hits in some stations
184  FairTutorialDet4Hit* hit;
185  std::set<Int_t> detIdSet;
186  std::set<Int_t>::iterator it;
187 
188  Int_t nHits = fHits->GetEntriesFast();
189  for (Int_t iHit = 0; iHit < nHits; ++iHit) {
190  hit = static_cast<FairTutorialDet4Hit*>(fHits->At(iHit));
191  Int_t detId = hit->GetDetectorID();
192  it = detIdSet.find(detId);
193  if (it == detIdSet.end()) {
194  detIdSet.insert(detId);
195  } else {
196  // find hit in already used detector station
197  // this is not a good event
198  return kFALSE;
199  }
200  }
201  return kTRUE;
202 }
203 
204 void FairTutorialDet4StraightLineFitter::Finish() { LOG(debug) << "Finish of FairTutorialDet4StraightLineFitter"; }
205 
Double_t GetZ() const
Definition: FairHit.h:50
InitStatus
Definition: FairTask.h:33
void SetX(Double_t x)
static FairRootManager * Instance()
ClassImp(FairEventBuilder)
Double_t GetX() const
Definition: FairHit.h:48
TObject * GetObject(const char *BrName)
void SetZ(Double_t z)
Int_t GetDetectorID() const
Definition: FairHit.h:47
Double_t GetDy() const
Definition: FairHit.h:43
void SetTx(Double_t tx)
Double_t GetY() const
Definition: FairHit.h:49
void Register(const char *name, const char *Foldername, TNamed *obj, Bool_t toFile)
void SetTy(Double_t ty)
Double_t GetDx() const
Definition: FairHit.h:42
void SetY(Double_t y)