FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairTutorialDet4MilleWriter.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 #include "Mille.h" // for Mille
15 
16 #include <TClonesArray.h> // for TClonesArray
17 #include <set> // for set, set<>::iterator, etc
18 
20  : FairTask("FairTutorialDet4MilleWriter")
21  , fTracks()
22  , fHits()
23  , fMille(nullptr)
24  , fWriteAscii(kFALSE)
25  , fVersion(1)
26  , fFileName("mp2tst")
27 {
28  LOG(debug) << "Default Constructor of FairTutorialDet4MilleWriter";
29 }
30 
32 {
33  LOG(debug) << "Destructor of FairTutorialDet4MilleWriter";
34 }
35 
37 {
38  LOG(debug) << "SetParContainers of FairTutorialDet4MilleWriter";
39  // Load all necessary parameter containers from the runtime data base
40  /*
41  FairRunAna* ana = FairRunAna::Instance();
42  FairRuntimeDb* rtdb=ana->GetRuntimeDb();
43 
44  <FairTutorialDet4MilleWriterDataMember> = (<ClassPointer>*)
45  (rtdb->getContainer("<ContainerName>"));
46  */
47 }
48 
50 {
51  LOG(debug) << "Initilization of FairTutorialDet4MilleWriter";
52 
53  // Get a handle from the IO manager
55 
56  // Get a pointer to the previous already existing data level
57  fTracks = static_cast<TClonesArray*>(ioman->GetObject("TutorialDetTrack"));
58  if (!fTracks) {
59  LOG(error) << "No InputDataLevelName array!\n FairTutorialDet4MilleWriter will be inactive";
60  return kERROR;
61  }
62 
63  // Get a pointer to the previous already existing data level
64  fHits = static_cast<TClonesArray*>(ioman->GetObject("TutorialDetHit"));
65  if (!fHits) {
66  LOG(error) << "No InputDataLevelName array!\n FairTutorialDet4MilleWriter will be inactive";
67  return kERROR;
68  }
69 
70  // Open MillePede Output file
71  if (fWriteAscii) {
72  fFileName += ".ascii";
73  fMille = new Mille(fFileName, false, true); // write human readable ascii file
74  } else {
75  fFileName += ".bin";
76  fMille = new Mille(fFileName, true, false); // write binary file needed by pede
77  }
78 
79  return kSUCCESS;
80 }
81 
83 {
84  LOG(debug) << "Initilization of FairTutorialDet4MilleWriter";
85  return kSUCCESS;
86 }
87 
88 void FairTutorialDet4MilleWriter::Exec(Option_t* /*option*/)
89 {
90  if (IsGoodEvent()) {
91  if (1 == fVersion) {
92  StraightLineShiftX();
93  }
94  if (2 == fVersion) {
95  StraightLineShiftXY();
96  }
97  }
98 }
99 
100 Bool_t FairTutorialDet4MilleWriter::IsGoodEvent()
101 {
102  // Check if each for the event there is maximum 1 hit per detector
103  // station. In the moment we create tracks with all hits in the
104  // event, so we have to check for this.
105  // In the end the algorithm should be able to work also with
106  // missing hits in some stations
107  FairTutorialDet4Hit* hit;
108  std::set<Int_t> detIdSet;
109  std::set<Int_t>::iterator it;
110 
111  Int_t nHits = fHits->GetEntriesFast();
112  for (Int_t iHit = 0; iHit < nHits; ++iHit) {
113  hit = static_cast<FairTutorialDet4Hit*>(fHits->At(iHit));
114  Int_t detId = hit->GetDetectorID();
115  it = detIdSet.find(detId);
116  if (it == detIdSet.end()) {
117  detIdSet.insert(detId);
118  } else {
119  // find hit in already used detector station
120  // this is not a good event
121  return kFALSE;
122  }
123  }
124  return kTRUE;
125 }
126 
127 void FairTutorialDet4MilleWriter::StraightLineShiftX()
128 {
129  const Int_t nLC = 2; // number of local parameters
130  // two for track x-coordinate
131  // x(z) = a1*z + a2
132  // dx(z)/da1 = z
133  // dx(z)/da2 = 1
134 
135  const Int_t nGL = 1; // number of global parameters per point
136  // taken from millepede 1 dim example
137 
138  Float_t sigma = 0.1;
139 
140  Float_t* derLC = new Float_t[nLC]; // array of derivatives for local parameters
141  Float_t* derGL = new Float_t[nGL]; // array of derivatives for global parameters
142 
143  Int_t* label = new Int_t[nGL]; // array of labels
144 
145  for (Int_t help = 0; help < nGL; help++) {
146  derGL[help] = 0;
147  }
148 
149  for (Int_t help = 0; help < nLC; help++) {
150  derLC[help] = 0;
151  }
152 
153  FairTrackParam* track = static_cast<FairTrackParam*>(fTracks->At(0));
154 
155  // Extract Track parameters
156  Double_t OffX = track->GetX();
157  Double_t SlopeX = track->GetTx();
158 
159  Double_t residual;
160 
161  FairTutorialDet4Hit* hit;
162 
163  Int_t nHits = fHits->GetEntriesFast();
164  for (Int_t iHit = 0; iHit < nHits; ++iHit) {
165  hit = static_cast<FairTutorialDet4Hit*>(fHits->At(iHit));
166 
167  Float_t Z = hit->GetZ();
168  Float_t hitX = hit->GetX();
169  Float_t fitX = OffX + SlopeX * Z;
170  LOG(debug) << "hitX, fitX: " << hitX << " ," << fitX;
171 
172  label[0] = iHit + 1;
173 
174  derGL[0] = -1;
175 
176  derLC[0] = 1;
177  derLC[1] = Z;
178 
179  residual = fitX - hitX;
180  LOG(debug) << "ResidualX: " << residual;
181  // call to Mille Writer
182  fMille->mille(nLC, derLC, nGL, derGL, label, residual, sigma);
183  }
184  fMille->end();
185 
186  delete[] derLC;
187  delete[] derGL;
188  delete[] label;
189 }
190 
191 void FairTutorialDet4MilleWriter::StraightLineShiftXY()
192 {
193  const Int_t nLC = 4; // number of local parameters
194  // two for track x-coordinate
195  // x(z) = a1*z + a2
196  // dx(z)/da1 = z
197  // dx(z)/da2 = 1
198 
199  const Int_t nGL = 2; // number of global parameters per point
200  // taken from millepede 1 dim example
201 
202  Float_t sigma = 0.1;
203 
204  Float_t* derLC = new Float_t[nLC]; // array of derivatives for local parameters
205  Float_t* derGL = new Float_t[nGL]; // array of derivatives for global parameters
206 
207  Int_t* label = new Int_t[nGL]; // array of labels
208 
209  for (Int_t help = 0; help < nGL; help++) {
210  derGL[help] = 0;
211  }
212 
213  for (Int_t help = 0; help < nLC; help++) {
214  derLC[help] = 0;
215  }
216 
217  FairTrackParam* track = static_cast<FairTrackParam*>(fTracks->At(0));
218 
219  // Extract Track parameters
220  Double_t OffX = track->GetX();
221  Double_t SlopeX = track->GetTx();
222  Double_t OffY = track->GetY();
223  Double_t SlopeY = track->GetTy();
224 
225  Double_t residual;
226 
227  FairTutorialDet4Hit* hit;
228 
229  Int_t nHits = fHits->GetEntriesFast();
230  for (Int_t iHit = 0; iHit < nHits; ++iHit) {
231  hit = static_cast<FairTutorialDet4Hit*>(fHits->At(iHit));
232 
233  Float_t Z = hit->GetZ();
234  Float_t hitX = hit->GetX();
235  Float_t fitX = OffX + SlopeX * Z;
236  Float_t hitY = hit->GetY();
237  Float_t fitY = OffY + SlopeY * Z;
238  LOG(debug) << "hitX, fitX: " << hitX << " ," << fitX;
239 
240  label[0] = iHit + 1;
241  label[1] = iHit + 101;
242 
243  derGL[0] = -1;
244  derGL[1] = 0;
245 
246  derLC[0] = 1;
247  derLC[1] = Z;
248  derLC[2] = 0;
249  derLC[3] = 0;
250 
251  residual = fitX - hitX;
252  LOG(debug) << "ResidualX: " << residual;
253  // call to Mille Writer
254  fMille->mille(nLC, derLC, nGL, derGL, label, residual, sigma);
255 
256  derGL[0] = 0;
257  derGL[1] = -1;
258 
259  derLC[0] = 0;
260  derLC[1] = 0;
261  derLC[2] = 1;
262  derLC[3] = Z;
263 
264  residual = fitY - hitY;
265  LOG(debug) << "ResidualX: " << residual;
266  // call to Mille Writer
267  fMille->mille(nLC, derLC, nGL, derGL, label, residual, sigma);
268  }
269  fMille->end();
270 
271  delete[] derLC;
272  delete[] derGL;
273  delete[] label;
274 }
275 
276 void FairTutorialDet4MilleWriter::Finish() { LOG(debug) << "Finish of FairTutorialDet4MilleWriter"; }
277 
Double_t GetZ() const
Definition: FairHit.h:50
InitStatus
Definition: FairTask.h:33
Double_t GetX() const
static FairRootManager * Instance()
ClassImp(FairEventBuilder)
Double_t GetX() const
Definition: FairHit.h:48
TObject * GetObject(const char *BrName)
void end()
Definition: Mille.cc:121
Int_t GetDetectorID() const
Definition: FairHit.h:47
Double_t GetY() const
void mille(int NLC, const float *derLc, int NGL, const float *derGl, const int *label, float rMeas, float sigma)
Definition: Mille.cc:38
Double_t GetY() const
Definition: FairHit.h:49
Double_t GetTy() const
Definition: Mille.h:36
Double_t GetTx() const