FairRoot/PandaRoot
Public Member Functions | Private Attributes | Friends | List of all members
PndLmdSensorAligner Class Reference

#include <PndLmdSensorAligner.h>

Public Member Functions

 PndLmdSensorAligner ()
 
 PndLmdSensorAligner (const PndLmdSensorAligner &other)
 
virtual ~PndLmdSensorAligner ()
 
void init ()
 
bool addSimplePair (const PndLmdHitPair &pair)
 
bool writePairsToBinary (const std::string directory)
 
bool readPairsFromBinary (const std::string directory)
 
void setMaximumNumberOfHitPairs (Int_t maxPais)
 
void clearPairs ()
 
void setForceInstant (Bool_t instant)
 
void setModuleID (Int_t ID)
 
int getModuleID ()
 
int getNoOfPairs ()
 
void calculateMatrix ()
 
int getOverlapId () const
 
void setOverlapId (Int_t overlapId)
 
const MatrixgetResultMatrix () const
 
void setInCentimeters (bool inCentimeters)
 
bool successful ()
 
void setZasTimetamp (bool value)
 
void verboseLevel (int level)
 

Private Attributes

bool forceInstant
 
bool debug
 
int _maxNoOfPairs
 
int numberOfPairs
 
int lastNoOfPairs
 
std::string _inputFilename
 
int _moduleID
 
int overlapID
 
int nonSanePairs
 
int skippedPairs
 
int swappedPairs
 
int verbose
 
std::vector< double > simpleSensorOneX
 
std::vector< double > simpleSensorOneY
 
std::vector< double > simpleSensorOneZ
 
std::vector< double > simpleSensorTwoX
 
std::vector< double > simpleSensorTwoY
 
std::vector< double > simpleSensorTwoZ
 
bool _inCentimeters
 
bool _success
 
bool _zIsTimestamp
 
Matrix resultMatrix
 

Friends

class PndLmdAlignQA
 

Detailed Description

Definition at line 25 of file PndLmdSensorAligner.h.

Constructor & Destructor Documentation

PndLmdSensorAligner::PndLmdSensorAligner ( )

Definition at line 50 of file PndLmdSensorAligner.cxx.

References init().

50  {
51  init();
52 }
PndLmdSensorAligner::PndLmdSensorAligner ( const PndLmdSensorAligner other)

Definition at line 58 of file PndLmdSensorAligner.cxx.

References init().

58  { // other //FIXME [R.K.03/2017] unused variable(s)
59  std::cerr << "PndLmdSensorAligner::Warning! Unnecessary copy-construction." << "\n";
60  init();
61 }
PndLmdSensorAligner::~PndLmdSensorAligner ( )
virtual

Definition at line 54 of file PndLmdSensorAligner.cxx.

54  {
55  //destroy everything. leave nothing standing.
56 }

Member Function Documentation

bool PndLmdSensorAligner::addSimplePair ( const PndLmdHitPair pair)

Definition at line 429 of file PndLmdSensorAligner.cxx.

References _inCentimeters, _maxNoOfPairs, PndLmdHitPair::getCol1(), PndLmdHitPair::getCol2(), PndLmdHitPair::getHit1(), PndLmdHitPair::getHit2(), PndLmdHitPair::getRow1(), PndLmdHitPair::getRow2(), simpleSensorOneX, simpleSensorOneY, simpleSensorOneZ, simpleSensorTwoX, simpleSensorTwoY, and simpleSensorTwoZ.

429  {
430 
431  if ((int) simpleSensorOneX.size() >= _maxNoOfPairs) {
432  // add no more
433  return false;
434  }
435 
436  //finally, add pair
437  if (_inCentimeters) {
438  simpleSensorOneX.push_back(pair.getHit1().x());
439  simpleSensorOneY.push_back(pair.getHit1().y());
440  simpleSensorOneZ.push_back(pair.getHit1().z());
441 
442  simpleSensorTwoX.push_back(pair.getHit2().x());
443  simpleSensorTwoY.push_back(pair.getHit2().y());
444  simpleSensorTwoZ.push_back(pair.getHit2().z());
445  }
446  else {
447  simpleSensorOneX.push_back(pair.getCol1());
448  simpleSensorOneY.push_back(pair.getRow1());
449  simpleSensorOneZ.push_back(simpleSensorOneZ.size()); //vecor grows, so this is okay
450 
451  simpleSensorTwoX.push_back(pair.getCol2());
452  simpleSensorTwoY.push_back(pair.getRow2());
453  simpleSensorTwoZ.push_back(simpleSensorTwoZ.size()); //vecor grows, so this is okay
454  }
455 
456  return true;
457 }
std::vector< double > simpleSensorTwoZ
std::vector< double > simpleSensorOneX
Double_t getCol2() const
Double_t getRow2() const
const TVector3 & getHit1() const
Definition: PndLmdHitPair.h:79
std::vector< double > simpleSensorTwoY
const TVector3 & getHit2() const
Definition: PndLmdHitPair.h:89
std::vector< double > simpleSensorOneY
std::vector< double > simpleSensorOneZ
std::vector< double > simpleSensorTwoX
Double_t getRow1() const
Double_t getCol1() const
void PndLmdSensorAligner::calculateMatrix ( )

Definition at line 63 of file PndLmdSensorAligner.cxx.

References _maxNoOfPairs, _success, _zIsTimestamp, exit(), Matrix::eye(), Icp::fit(), forceInstant, Icp::forceInstantResult(), Matrix::getData(), Icp::getFitnessScore(), Icp::getInterations(), Icp::hasConverged(), numberOfPairs, overlapID, resultMatrix, Matrix::setVal(), simpleSensorOneX, simpleSensorOneY, simpleSensorOneZ, simpleSensorTwoX, simpleSensorTwoY, simpleSensorTwoZ, skippedPairs, and verbose.

Referenced by PndLmdAlignManager::alignOne().

63  {
64 
65  int nPairs;
66 
67  //check if all vectors have the same size
68  int s1 = simpleSensorOneX.size();
69  int s2 = simpleSensorOneY.size();
70  int s3 = simpleSensorOneZ.size();
71 
72  int s4 = simpleSensorTwoX.size();
73  int s5 = simpleSensorTwoY.size();
74  int s6 = simpleSensorTwoZ.size();
75 
76  if (s1 == s2 && s2 == s3 && s3 == s4 && s4 == s5 && s5 == s6) {
77  nPairs = simpleSensorOneX.size();
78  }
79  else {
80  cout
81  << "PndLmdSensorAligner::calculateMatrix::FATAL. Pair sorting error, pairs vectors have different sizes.\n";
82  cout << "s1: " << s1 << "\n";
83  cout << "s2: " << s2 << "\n";
84  cout << "s3: " << s3 << "\n";
85  cout << "s4: " << s4 << "\n";
86  cout << "s5: " << s5 << "\n";
87  cout << "s6: " << s6 << "\n";
88  exit(1);
89  }
90 
91  if (skippedPairs > 0) {
92  cout << "=====================================================\n";
93  cout << "WARNING! Invalid pairs in pair file, check your data!\n";
94  cout << "=====================================================\n";
95  }
96 
97  //TODO: set from Manager or parameter file!
98  /*
99  * =============== Global Parameters, from file in the future ===================
100  */
101  int dim = 2;
102  bool eventTimeCheck = true;
103  double minDelta = 1e-6;
104  _zIsTimestamp = true;
105 
106  // only allow max Pairs!
107  if (nPairs > _maxNoOfPairs) {
108  nPairs = _maxNoOfPairs;
109  }
110 
111  //check if maxPairs > 0
112  if (nPairs < 500) {
113  cerr
114  << "PndLmdSensrAligner::Error: Trying to use less than 5 pairs! (And that's not going to work.) Aborting.\n";
115  _success = false;
116  return;
117  }
118  else {
119  // cout << "PndLmdSensrAligner::CalculateMatrix: Using " << nPairs << " pairs.\n";
120  }
121 
122  double* Model = new double[dim * nPairs];
123  double* Template = new double[dim * nPairs];
124 
125  if (verbose == 3) {
126  cout << "arranging pairs...\n";
127  cout << "num pairs from bin: " << numberOfPairs << "\n";
128  cout << "num pairs from vec: " << simpleSensorOneX.size() << "\n";
129  cout << "num pairs from dec: " << nPairs << "\n";
130  }
131 
132  if (dim == 2) {
133  for (int ipair = 0; ipair < nPairs; ipair++) {
134  Model[ipair * dim + 0] = simpleSensorOneX[ipair];
135  Model[ipair * dim + 1] = simpleSensorOneY[ipair];
136  Template[ipair * dim + 0] = simpleSensorTwoX[ipair];
137  Template[ipair * dim + 1] = simpleSensorTwoY[ipair];
138  }
139  }
140 
141  else if (dim == 3) {
142  for (int ipair = 0; ipair < nPairs; ipair++) {
143  Model[ipair * dim + 0] = simpleSensorOneX[ipair];
144  Model[ipair * dim + 1] = simpleSensorOneY[ipair];
145  Model[ipair * dim + 2] = (double) ipair;
146  Template[ipair * dim + 0] = simpleSensorTwoX[ipair];
147  Template[ipair * dim + 1] = simpleSensorTwoY[ipair];
148  Template[ipair * dim + 2] = (double) ipair;
149  }
150 
151  //artificial z component, only really relevant if using cm coordinate system
152  // UPDATE: well that's not exactly true. If using CM coordinates, the z is artificial
153  // as well. So to get comparable results of CM vs PX, we should use this in BOTH cases
154 
155  if (_zIsTimestamp) {
156  if (verbose == 3) cout << "applying artificial Z coordinate...\n";
157 
158  for (int ipair = 0; ipair < nPairs; ipair++) {
159  Model[ipair * dim + 2] = ((2.0 * ipair / (double) nPairs - 1.0) * 1e4);
160  Template[ipair * dim + 2] = ((2.0 * ipair / (double) nPairs - 1.0) * 1e4);
161  }
162  }
163 
164  int zeroVals = 0;
165  int modxinv = 0, modyinv = 0, modzinv = 0;
166  int temxinv = 0, temyinv = 0, temzinv = 0;
167 
168  /*
169  * zero factor had to be introduced because a bug in earlier versions led to many entries
170  * being filled with zeros. it shouldn't be needed anymore, but it doesn't cost much and
171  * could still be useful.
172  */
173 
174  if (verbose == 3) cout << "checking for zero values...\n";
175 
176  for (int iCheck = 0; iCheck < dim * nPairs; iCheck++) {
177  double val1 = abs(Model[iCheck]);
178  double val2 = abs(Template[iCheck]);
179  if (val1 < 1e-15) {
180  if (iCheck % 3 == 0) {
181  //cout << "model xval invalid: " << val1 << "\n";
182  modxinv++;
183  }
184  if (iCheck % 3 == 1) {
185  //cout << "model yval invalid\n";
186  modyinv++;
187  }
188  if (iCheck % 3 == 2) {
189  //cout << "model zval invalid\n";
190  modzinv++;
191  }
192  zeroVals++;
193  //cout << val1 << "\n";
194  }
195  if (val2 < 1e-15) {
196  if (iCheck % 3 == 0) {
197  //cout << "template xval invalid: " << val2 << "\n";
198  temxinv++;
199  }
200  if (iCheck % 3 == 1) {
201  //cout << "template yval invalid\n";
202  temyinv++;
203  }
204  if (iCheck % 3 == 2) {
205  //cout << "template zval invalid\n";
206  temzinv++;
207  }
208  zeroVals++;
209  //cout << val2 << "\n";
210  }
211  }
212 
213  // 3 dimension and 2 arrays = 6
214  double zeroFactor = zeroVals / ((double) nPairs * 6.0);
215  if (zeroFactor > 0.1 && zeroFactor < 0.3) {
216  cout << "WARNING. More than 10 % of your entries is zero. That must be a mistake. \n";
217  cout << "Also, the kdtree creation could crash. Keep an eye out for that...\n";
218  cout << "Zero factor: " << zeroFactor << "\n";
219  cout << "model x vals invalid: " << modxinv / (double) nPairs << "\n";
220  cout << "model y vals invalid: " << modyinv / (double) nPairs << "\n";
221  cout << "model z vals invalid: " << modzinv / (double) nPairs << "\n";
222  cout << "templ x vals invalid: " << temxinv / (double) nPairs << "\n";
223  cout << "templ y vals invalid: " << temyinv / (double) nPairs << "\n";
224  cout << "templ z vals invalid: " << temzinv / (double) nPairs << "\n";
225 
226  }
227  if (zeroFactor > 0.3) {
228  cout << "ERROR. More than 30 % of your entries is zero. That must be a mistake. \n";
229  cout << "Also, the kdtree creation will crash. Exiting.\n";
230  cout << "Zero factor: " << zeroFactor << "\n";
231  cout << "model x vals invalid: " << modxinv / (double) nPairs << "\n";
232  cout << "model y vals invalid: " << modyinv / (double) nPairs << "\n";
233  cout << "model z vals invalid: " << modzinv / (double) nPairs << "\n";
234  cout << "templ x vals invalid: " << temxinv / (double) nPairs << "\n";
235  cout << "templ y vals invalid: " << temyinv / (double) nPairs << "\n";
236  cout << "templ z vals invalid: " << temzinv / (double) nPairs << "\n";
237  cout << "=== additional data ===\n";
238  cout << "overlap id: " << overlapID << "\n";
239  cout << "no of Pairs: " << nPairs << "\n";
240  exit(1);
241  }
242  }
243 
244 // if (verbose >= 2) {
245 // if (overlapID == 0) {
246 // cout << std::setprecision(16);
247 // cout << "grep::pairs::begin\n";
248 // for (int i = 0; i < nPairs * dim; i++) {
249 // cout << "model " << i << ": " << Model[i] << "\n";
250 // cout << "templ " << i << ": " << Template[i] << "\n";
251 // cout << "-------------\n";
252 // }
253 // cout << "grep::pairs::end\n";
254 // }
255 // }
256 
257  if (verbose == 3) {
258  cout << "creating ICP...\n";
259  }
260 
261  // start with identity as initial transformation
262  // in practice you might want to use some kind of prediction here
263  Matrix Rotation;
264  Matrix translation;
265 
266  //perform ICP and store quality parameters
267  //attention! dim * nPairs must equal size of model!
268  IcpPointToPoint icp(Model, nPairs, dim);
269 
270  if (verbose == 3) cout << "ICP and model created...\n";
271 
272  //TODO: clean this up!
273  //prepare Matrices
274  if (dim == 2) {
275  Rotation = Matrix::eye(2);
276  translation = Matrix(2, 1);
277  }
278  else if (dim == 3) {
279  Rotation = Matrix::eye(3);
280  translation = Matrix(3, 1);
281  }
282 
283  icp.forceInstantResult(forceInstant);
284  icp.fit(Template, nPairs, Rotation, translation, -1);
285 
286  if (verbose == 3) cout << "ICP fit step done.\n";
287 
288  if (dim == 2) {
289 
290  //make 4x4 matrix
291  double* tempR = new double[4];
292  double* tempT = new double[2];
293 
294  Rotation.getData(tempR);
295  translation.getData(tempT);
296 
297  double* finalMatrix = new double[16];
298 
299  //okay, this is the version that FIRST rotates, THEN translates.
300  finalMatrix[0] = tempR[0];
301  finalMatrix[1] = tempR[1];
302  finalMatrix[2] = 0;
303  finalMatrix[3] = tempT[0];
304  finalMatrix[4] = tempR[2];
305  finalMatrix[5] = tempR[3];
306  finalMatrix[6] = 0;
307  finalMatrix[7] = tempT[1];
308  finalMatrix[8] = 0;
309  finalMatrix[9] = 0;
310  finalMatrix[10] = 1.0;
311  finalMatrix[11] = 0;
312  finalMatrix[12] = 0;
313  finalMatrix[13] = 0;
314  finalMatrix[14] = 0;
315  finalMatrix[15] = 1.0;
316  resultMatrix = Matrix(4, 4);
317 
318  //save matrix!
319  resultMatrix.setVal(4, 4, finalMatrix);
320 
321  delete tempR;
322  delete tempT;
323  delete finalMatrix;
324 
325  }
326  else if (dim == 3) {
327 
328  //make 4x4 matrix
329  double* tempR = new double[9];
330  double* tempT = new double[3];
331 
332  Rotation.getData(tempR);
333  translation.getData(tempT);
334 
335  double* finalMatrix = new double[16];
336 
337  //okay, this is the version that FIRST rotates, THEN translates.
338  finalMatrix[0] = tempR[0];
339  finalMatrix[1] = tempR[1];
340  finalMatrix[2] = tempR[2];
341  finalMatrix[3] = tempT[0];
342  finalMatrix[4] = tempR[3];
343  finalMatrix[5] = tempR[4];
344  finalMatrix[6] = tempR[5];
345  finalMatrix[7] = tempT[1];
346  finalMatrix[8] = tempR[6];
347  finalMatrix[9] = tempR[7];
348  finalMatrix[10] = tempR[8];
349  finalMatrix[11] = tempT[2];
350  finalMatrix[12] = 0;
351  finalMatrix[13] = 0;
352  finalMatrix[14] = 0;
353  finalMatrix[15] = 1;
354 
355  resultMatrix = Matrix(4, 4);
356 
357  //save matrix!
358  resultMatrix.setVal(4, 4, finalMatrix);
359 
360  delete tempR;
361  delete tempT;
362  delete finalMatrix;
363  }
364 
365  std::stringstream alignlog;
366 
367  if (icp.hasConverged()) {
368  if (verbose == 3) cout << "ICP convergence ok.\n";
369  _success = true;
370 
371  //and say a few words for the log
372  alignlog << "\n";
373  alignlog << "====================================================\n";
374  alignlog << "icp converged for overlapID " << overlapID << " in " << icp.getInterations()
375  << " iterations.\n";
376  alignlog << "pairs available: " << nPairs;
377  if (nPairs < 100000) {
378  alignlog << " (WARNING! This is not enough for accurate alignment!)\n";
379  }
380  else {
381  alignlog << "\n";
382  }
383  //log << "euclidean fitness score: " << icp.getFitnessScore() << " (that is " << icp.getFitnessScore()/8e-4 << " pixels)"<< "\n";
384  alignlog << "euclidean fitness score: " << icp.getFitnessScore();
385  if (icp.getFitnessScore() > 0.55) {
386  alignlog << " (WARNING! This is bad! Should be ~0.55)"; //FIXME: no it should not
387  }
388  alignlog << "\n";
389  alignlog << "minDelta: " << minDelta << "\n";
390 
391  alignlog << "EventTimeCheck: ";
392  if (eventTimeCheck) {
393  alignlog << "on (and passed)\n";
394  }
395  else {
396  alignlog << "off\n";
397  }
398  alignlog << "Force Instant: ";
399  if (forceInstant) {
400  alignlog << "on\n";
401  }
402  else {
403  alignlog << "off\n";
404  }
405  alignlog << "====================================================\n";
406  }
407  else {
408  alignlog << "\n";
409  alignlog << "====================================================\n";
410  alignlog << "CRITICAL ERROR:\n";
411  alignlog << "no convergence for overlapID " << overlapID << "." << "\n";
412  alignlog << "====================================================\n";
413  alignlog << "\n";
414  if (verbose == 3) cout << "ICP did not converge!\n";
415  _success = false;
416  }
417  if (verbose == 3) cout << alignlog.str();
418 
419  delete[] Model;
420  delete[] Template;
421 
422  // DONT DO THIS HERE. The AlignManager takes care of that!
423  // we must save the pairs before that!
424  // this shit just cost me three hours of my life!
425  // clearPairs();
426  return;
427 }
std::vector< double > simpleSensorTwoZ
std::vector< double > simpleSensorOneX
exit(0)
void setVal(const int32_t m, const int32_t n, const FLOAT *val_)
std::vector< double > simpleSensorTwoY
std::vector< double > simpleSensorOneY
std::vector< double > simpleSensorOneZ
Definition: matrix.h:50
void getData(FLOAT *val_, int32_t i1=0, int32_t j1=0, int32_t i2=-1, int32_t j2=-1)
std::vector< double > simpleSensorTwoX
void eye()
void PndLmdSensorAligner::clearPairs ( )

Definition at line 743 of file PndLmdSensorAligner.cxx.

References lastNoOfPairs, simpleSensorOneX, simpleSensorOneY, simpleSensorOneZ, simpleSensorTwoX, simpleSensorTwoY, and simpleSensorTwoZ.

Referenced by PndLmdAlignManager::alignOne().

743  {
744 
746 
747  //call destructors of the member objects (well, they're doubles, so... yeah.)
748  simpleSensorOneX.clear();
749  simpleSensorOneY.clear();
750  simpleSensorOneZ.clear();
751  simpleSensorTwoX.clear();
752  simpleSensorTwoY.clear();
753  simpleSensorTwoZ.clear();
754 
755  //force release of allocated memory by vectors
756  vector<double>().swap(simpleSensorOneX);
757  vector<double>().swap(simpleSensorOneY);
758  vector<double>().swap(simpleSensorOneZ);
759  vector<double>().swap(simpleSensorTwoX);
760  vector<double>().swap(simpleSensorTwoY);
761  vector<double>().swap(simpleSensorTwoZ);
762 
763 }
std::vector< double > simpleSensorTwoZ
std::vector< double > simpleSensorOneX
std::vector< double > simpleSensorTwoY
std::vector< double > simpleSensorOneY
std::vector< double > simpleSensorOneZ
std::vector< double > simpleSensorTwoX
int PndLmdSensorAligner::getModuleID ( )
inline

Definition at line 84 of file PndLmdSensorAligner.h.

References _moduleID.

84  {
85  return _moduleID;
86  }
int PndLmdSensorAligner::getNoOfPairs ( )
inline

Definition at line 87 of file PndLmdSensorAligner.h.

References lastNoOfPairs, max(), and simpleSensorOneX.

87  {
88  return std::max(simpleSensorOneX.size(), (size_t) lastNoOfPairs);
89  }
std::vector< double > simpleSensorOneX
friend F32vec4 max(const F32vec4 &a, const F32vec4 &b)
Definition: P4_F32vec4.h:26
int PndLmdSensorAligner::getOverlapId ( ) const
inline

Definition at line 93 of file PndLmdSensorAligner.h.

References overlapID.

93  {
94  return overlapID;
95  }
const Matrix& PndLmdSensorAligner::getResultMatrix ( ) const
inline

Definition at line 101 of file PndLmdSensorAligner.h.

References resultMatrix.

101  {
102  return resultMatrix;
103  }
void PndLmdSensorAligner::init ( )

Definition at line 29 of file PndLmdSensorAligner.cxx.

References _inCentimeters, _maxNoOfPairs, _moduleID, _success, _zIsTimestamp, debug, forceInstant, nonSanePairs, overlapID, skippedPairs, swappedPairs, and verbose.

Referenced by PndLmdSensorAligner().

29  {
30  _maxNoOfPairs = 3e5;
31 
32  forceInstant = true;
33  _moduleID = -1;
34  nonSanePairs = 0;
35  skippedPairs = 0;
36  swappedPairs = 0;
37 // ID1 = -1;
38 // ID2 = -1;
39  verbose = 0;
40  overlapID = -1;
41  _inCentimeters = true;
42  _success = false;
43  _zIsTimestamp = true;
44 
45  debug = false;
46 
47  //cout << "PndLmdSensorAligner::Init(): Initialization successful.\n";
48 }
bool PndLmdSensorAligner::readPairsFromBinary ( const std::string  directory)

Definition at line 547 of file PndLmdSensorAligner.cxx.

References _inCentimeters, directory, error(), exit(), filename, i, is, PndLmdAlignManager::makeBinaryPairFileName(), numberOfPairs, overlapID, simpleSensorOneX, simpleSensorOneY, simpleSensorOneZ, simpleSensorTwoX, simpleSensorTwoY, simpleSensorTwoZ, and verbose.

547  {
548 
549  string filename; //binary pair file
550  //size_t length; //number of raw doubles, remember pairs have 6 doubles
551  double* pdata; //array with pairs
552  size_t filesize;
553  size_t doublesize = sizeof(double);
554  int nPairs;
555  int doublesPerPair;
556  int noOfDoubles = 0;
557 
558  /*
559  * read goes as follows:
560  *
561  * first, check file size.
562  * file size is larger than 6 doubles (header)? -> read header only! else return false;
563  * read header and interpret data from header.
564  * is nPairs*6*sizeof(double) + 6*sizeof(double) (header) == filezise?
565  * is nPairs in header == 6 * sizeof(double) (filesize - header)
566  * if so, file seems okay, read header + file. else return false;
567  * when entire file is read, copy data without header to arrays for ICP *
568  */
569 
570  filename = directory;
572 
573  //check if file exists and file size
574  std::fstream inStream(filename.c_str(), std::ios::binary | std::ios::in | std::ios::ate);
575  if (inStream) {
576  std::fstream::pos_type size = inStream.tellg();
577  filesize = size;
578  }
579  else {
580  cout << filename.c_str() << " could not be read!\n";
581  return false;
582  }
583 
584  double* headersizeD = new double[1];
585  //read header size
586  if (filesize >= sizeof(double)) {
587  //read header
588  std::ifstream is(filename.c_str(), std::ios::binary | std::ios::in);
589  if (!is.is_open()) return false;
590  is.read(reinterpret_cast<char*>(headersizeD), std::streamsize(sizeof(double)));
591  is.close();
592  }
593 
594  int headersize;
595  if (headersizeD[0] < 6) {
596  //cout << "headersize is " << headersizeD[0] << ", seems to be old format. using 6 for now.";
597  headersize = 6;
598  }
599  else {
600  headersize = headersizeD[0];
601  }
602 
603  //read header
604  if (filesize >= headersize * sizeof(double)) {
605  double* header = new double[headersize];
606 
607  //read header
608  std::ifstream is(filename.c_str(), std::ios::binary | std::ios::in);
609  if (!is.is_open()) return false;
610  is.read(reinterpret_cast<char*>(header), std::streamsize(headersize * sizeof(double)));
611  is.close();
612 
613  //check header
614  if (verbose == 3) {
615  cout << "file version: " << header[0] << "\n";
616  cout << "doubles / pair: " << header[1] << "\n";
617  cout << "no of pairs: " << header[2] << "\n";
618  }
619 
620  //check header
621  doublesPerPair = header[1];
622  nPairs = header[2];
623  numberOfPairs = nPairs;
624  noOfDoubles = nPairs * doublesPerPair + headersize;
625  size_t filesizeMust = sizeof(double) * (noOfDoubles);
626 
627  if (doublesize != header[3]) {
628  cout
629  << "warning! sizeof(double) on this system is different than on the system that made this binary!\n";
630  //TODO: decide what to do in this case
631  exit(1);
632  doublesize = header[3];
633  return false;
634  }
635 
636  if (overlapID != header[4]) {
637  cout << "error! file name and overlapID do not match! did you rename the file?\n";
638 
639  //FIXME: allow this, for now...
640  //return false;
641  }
642 
643  if (filesizeMust == filesize) {
644  if (verbose == 3) cout << "file seems okay!\n";
645  }
646  else {
647  cout << "file is corrupt!\n";
648  return false;
649  }
650 
651  //free allocated space!
652  delete[] header;
653 
654  }
655  else {
656  cout << filename.c_str() << " is too small, file corrupt!\n";
657  return false;
658  }
659 
660  pdata = new double[noOfDoubles];
661 
662  //actually read file
663  std::ifstream is(filename.c_str(), std::ios::binary | std::ios::in);
664  if (!is.is_open()) return false;
665  is.read(reinterpret_cast<char*>(pdata), std::streamsize(noOfDoubles * sizeof(double)));
666  is.close();
667 
668  //store data correctly or whatever
669  //save data
670  int currentIndex = headersize; //data starts here
671  bool error = false;
672 
673  bool compareBinaryToStored = false;
674 
675  if (compareBinaryToStored) {
676  if ((int) simpleSensorOneX.size() != nPairs) {
677  cout << "fatal, can't compare to empty vector.\n";
678  return false;
679  }
680  }
681 
682  //from here on, only simpleStorage is supported
683  //TODO: 4 doubles per pair (eq in px) is not supported yet, or other storage options like distance
684  for (int i = 0; i < nPairs; i++) {
685  //assign pair data
686  if (!compareBinaryToStored) {
687  simpleSensorOneX.push_back(pdata[currentIndex + 0]);
688  simpleSensorOneY.push_back(pdata[currentIndex + 1]);
689  simpleSensorOneZ.push_back(pdata[currentIndex + 2]);
690 
691  simpleSensorTwoX.push_back(pdata[currentIndex + 3]);
692  simpleSensorTwoY.push_back(pdata[currentIndex + 4]);
693  simpleSensorTwoZ.push_back(pdata[currentIndex + 5]);
694  }
695  //check read data
696  else {
697  if (pdata[currentIndex + 0] != simpleSensorOneX[i]) error = true;
698  if (pdata[currentIndex + 1] != simpleSensorOneY[i]) error = true;
699  if (pdata[currentIndex + 2] != simpleSensorOneZ[i]) error = true;
700  if (pdata[currentIndex + 3] != simpleSensorTwoX[i]) error = true;
701  if (pdata[currentIndex + 4] != simpleSensorTwoY[i]) error = true;
702  if (pdata[currentIndex + 5] != simpleSensorTwoZ[i]) error = true;
703 
704  if (error) {
705  cout << pdata[currentIndex + 0] << "|" << simpleSensorOneX[i] << "\n";
706  cout << pdata[currentIndex + 1] << "|" << simpleSensorOneY[i] << "\n";
707  cout << pdata[currentIndex + 2] << "|" << simpleSensorOneZ[i] << "\n";
708  cout << pdata[currentIndex + 3] << "|" << simpleSensorTwoX[i] << "\n";
709  cout << pdata[currentIndex + 4] << "|" << simpleSensorTwoY[i] << "\n";
710  cout << pdata[currentIndex + 5] << "|" << simpleSensorTwoZ[i] << "\n";
711 
712  cout << "error!\n";
713  return false;
714  }
715  }
716 
717  //get next index, every pair has doublesPerPair doubles
718  currentIndex += doublesPerPair;
719  }
720 
721  if (!error) {
722  if (verbose == 3) {
723  cout << "file check successful, everything okay!\n";
724  cout << "read " << nPairs << " from binary file.\n";
725  cout << "vector sizes:\n";
726  cout << "1x: " << simpleSensorOneX.size() << "\n";
727  cout << "1y: " << simpleSensorOneY.size() << "\n";
728  cout << "1z: " << simpleSensorOneZ.size() << "\n";
729  cout << "2x: " << simpleSensorTwoX.size() << "\n";
730  cout << "2y: " << simpleSensorTwoY.size() << "\n";
731  cout << "2z: " << simpleSensorTwoZ.size() << "\n";
732  }
733  }
734 
735  //delete array!
736  delete[] pdata;
737 
738  // now, check if ID1 and ID2 can be generated from overlapID
739  // well, they can, but only from dimension->, so this is done in manager, not here
740  return true;
741 }
std::vector< double > simpleSensorTwoZ
std::vector< double > simpleSensorOneX
Int_t i
Definition: run_full.C:25
exit(0)
static void error(int no)
Definition: ranlxd.cxx:419
static std::string makeBinaryPairFileName(int overlapId=0, bool incentimeters=true)
std::vector< double > simpleSensorTwoY
std::vector< double > simpleSensorOneY
std::vector< double > simpleSensorOneZ
static int is
Definition: ranlxd.cxx:374
std::vector< double > simpleSensorTwoX
TString directory
const string filename
void PndLmdSensorAligner::setForceInstant ( Bool_t  instant)
inline

Definition at line 77 of file PndLmdSensorAligner.h.

References forceInstant.

77  {
78  forceInstant = instant;
79  }
void PndLmdSensorAligner::setInCentimeters ( bool  inCentimeters)
inline

Definition at line 106 of file PndLmdSensorAligner.h.

References _inCentimeters.

Referenced by PndLmdAlignManager::init().

106  {
107  _inCentimeters = inCentimeters;
108  }
void PndLmdSensorAligner::setMaximumNumberOfHitPairs ( Int_t  maxPais)
inline

Definition at line 60 of file PndLmdSensorAligner.h.

References _maxNoOfPairs, simpleSensorOneX, simpleSensorOneY, simpleSensorOneZ, simpleSensorTwoX, simpleSensorTwoY, and simpleSensorTwoZ.

60  {
61  if (maxPais > 0) {
62  _maxNoOfPairs = maxPais;
63 
64  //check vector capacity to avoid constant re-allocation, use max pairs
65  simpleSensorOneX.reserve(maxPais);
66  simpleSensorOneY.reserve(maxPais);
67  simpleSensorOneZ.reserve(maxPais);
68  simpleSensorTwoX.reserve(maxPais);
69  simpleSensorTwoY.reserve(maxPais);
70  simpleSensorTwoZ.reserve(maxPais);
71  }
72  }
std::vector< double > simpleSensorTwoZ
std::vector< double > simpleSensorOneX
std::vector< double > simpleSensorTwoY
std::vector< double > simpleSensorOneY
std::vector< double > simpleSensorOneZ
std::vector< double > simpleSensorTwoX
void PndLmdSensorAligner::setModuleID ( Int_t  ID)
inline

Definition at line 81 of file PndLmdSensorAligner.h.

References _moduleID.

81  {
82  _moduleID = ID;
83  }
void PndLmdSensorAligner::setOverlapId ( Int_t  overlapId)
inline

Definition at line 97 of file PndLmdSensorAligner.h.

References overlapID.

Referenced by PndLmdAlignManager::init().

97  {
98  overlapID = overlapId;
99  }
void PndLmdSensorAligner::setZasTimetamp ( bool  value)
inline

Definition at line 114 of file PndLmdSensorAligner.h.

References _zIsTimestamp.

Referenced by PndLmdAlignManager::init().

114  {
115  _zIsTimestamp = value;
116  }
bool PndLmdSensorAligner::successful ( )
inline

Definition at line 110 of file PndLmdSensorAligner.h.

References _success.

110  {
111  return _success;
112  }
void PndLmdSensorAligner::verboseLevel ( int  level)
inline

Definition at line 118 of file PndLmdSensorAligner.h.

References verbose.

118  {
119  verbose = level;
120  }
bool PndLmdSensorAligner::writePairsToBinary ( const std::string  directory)

Definition at line 459 of file PndLmdSensorAligner.cxx.

References _inCentimeters, directory, filename, i, PndLmdAlignManager::makeBinaryPairFileName(), PndLmdAlignManager::mkdir(), out, overlapID, simpleSensorOneX, simpleSensorOneY, simpleSensorOneZ, simpleSensorTwoX, simpleSensorTwoY, and simpleSensorTwoZ.

Referenced by PndLmdAlignManager::alignOne().

459  {
460 
461  string filename; //target directory
462  double* pdata; //array with pairs
463  int doublesPerPair = 6; //well, doubles per Pair
464  int nPairs = 0;
465 
466  if (simpleSensorOneX.size() == simpleSensorOneY.size()
467  && simpleSensorOneX.size() == simpleSensorOneZ.size()
468  && simpleSensorOneX.size() == simpleSensorTwoX.size()
469  && simpleSensorOneX.size() == simpleSensorTwoY.size()
470  && simpleSensorOneX.size() == simpleSensorTwoZ.size()) {
471  nPairs = simpleSensorOneX.size();
472  }
473  else {
474  cout << "PndLmdSensorAligner::ERROR: x, y and z have different amounts of entries!\n";
475  cout << "oneX: " << simpleSensorOneX.size() << "\n";
476  cout << "oneY: " << simpleSensorOneY.size() << "\n";
477  cout << "oneZ: " << simpleSensorOneZ.size() << "\n";
478  cout << "twoX: " << simpleSensorTwoX.size() << "\n";
479  cout << "twoY: " << simpleSensorTwoY.size() << "\n";
480  cout << "twoZ: " << simpleSensorTwoZ.size() << "\n";
481 
482  nPairs = 0;
483  }
484 
485  if (nPairs == 0) {
486  cout << "warning: attempting to write empty binary pair file! (no pairs in buffer for overlapID "
487  << overlapID << ")\n";
488  return false;
489  }
490 
491  size_t length = nPairs * doublesPerPair + 6; //number of raw doubles (including header), remember pairs have 6 doubles
492 
493  filename = directory;
495 
496  //construct header
497  double* header = new double[6];
498  header[0] = 6; //header size in double fields
499  header[1] = doublesPerPair; //doubles per pair
500  header[2] = nPairs; //nPairs, read the correct vector!
501  header[3] = sizeof(double); //sizeof(double), check when reading files!
502  header[4] = overlapID; //overlapID
503  header[5] = -1; //value not assigned yet
504 
505  pdata = new double[length];
506 
507  //save header
508  for (int i = 0; i < 6; i++) {
509  pdata[i] = header[i];
510  }
511  //save data
512  int currentIndex = 6; //data starts here
513 
514  // if(_simpleStorage){
515  for (int i = 0; i < nPairs; i++) {
516  //first, only assume simple storage
517  pdata[currentIndex + 0] = simpleSensorOneX[i];
518  pdata[currentIndex + 1] = simpleSensorOneY[i];
519  pdata[currentIndex + 2] = simpleSensorOneZ[i];
520  pdata[currentIndex + 3] = simpleSensorTwoX[i];
521  pdata[currentIndex + 4] = simpleSensorTwoY[i];
522  pdata[currentIndex + 5] = simpleSensorTwoZ[i];
523  currentIndex += 6;
524  }
525 
526  /*
527  * the write part is easy, just dump everything. read part is more difficult,
528  * need to check file first
529  */
530 
531  //TODO: abstract this to Manager!
532  //create directory if not already present
534  std::ofstream os(filename.c_str(), std::ios::binary | std::ios::out);
535  if (!os.is_open()) {
536  cout << "ERROR! Could not write to " << filename << "!\n";
537  return false;
538  }
539 
540  os.write(reinterpret_cast<const char*>(pdata), std::streamsize(length * sizeof(double)));
541  os.close();
542  delete[] pdata;
543  delete[] header;
544  return true;
545 }
std::vector< double > simpleSensorTwoZ
std::vector< double > simpleSensorOneX
Int_t i
Definition: run_full.C:25
static bool mkdir(std::string path)
static std::string makeBinaryPairFileName(int overlapId=0, bool incentimeters=true)
std::vector< double > simpleSensorTwoY
std::vector< double > simpleSensorOneY
std::vector< double > simpleSensorOneZ
TFile * out
Definition: reco_muo.C:20
std::vector< double > simpleSensorTwoX
TString directory
const string filename

Friends And Related Function Documentation

friend class PndLmdAlignQA
friend

Definition at line 29 of file PndLmdSensorAligner.h.

Member Data Documentation

bool PndLmdSensorAligner::_inCentimeters
private
std::string PndLmdSensorAligner::_inputFilename
private

Definition at line 33 of file PndLmdSensorAligner.h.

int PndLmdSensorAligner::_maxNoOfPairs
private
int PndLmdSensorAligner::_moduleID
private

Definition at line 34 of file PndLmdSensorAligner.h.

Referenced by getModuleID(), init(), and setModuleID().

bool PndLmdSensorAligner::_success
private

Definition at line 40 of file PndLmdSensorAligner.h.

Referenced by calculateMatrix(), init(), and successful().

bool PndLmdSensorAligner::_zIsTimestamp
private

Definition at line 40 of file PndLmdSensorAligner.h.

Referenced by calculateMatrix(), init(), and setZasTimetamp().

bool PndLmdSensorAligner::debug
private

Definition at line 31 of file PndLmdSensorAligner.h.

Referenced by init().

bool PndLmdSensorAligner::forceInstant
private

Definition at line 31 of file PndLmdSensorAligner.h.

Referenced by calculateMatrix(), init(), and setForceInstant().

int PndLmdSensorAligner::lastNoOfPairs
private

Definition at line 32 of file PndLmdSensorAligner.h.

Referenced by clearPairs(), and getNoOfPairs().

int PndLmdSensorAligner::nonSanePairs
private

Definition at line 35 of file PndLmdSensorAligner.h.

Referenced by init().

int PndLmdSensorAligner::numberOfPairs
private
int PndLmdSensorAligner::overlapID
private
Matrix PndLmdSensorAligner::resultMatrix
private

Definition at line 42 of file PndLmdSensorAligner.h.

Referenced by calculateMatrix(), and getResultMatrix().

std::vector<double> PndLmdSensorAligner::simpleSensorOneX
private
std::vector<double> PndLmdSensorAligner::simpleSensorOneY
private
std::vector<double> PndLmdSensorAligner::simpleSensorOneZ
private
std::vector<double> PndLmdSensorAligner::simpleSensorTwoX
private
std::vector<double> PndLmdSensorAligner::simpleSensorTwoY
private
std::vector<double> PndLmdSensorAligner::simpleSensorTwoZ
private
int PndLmdSensorAligner::skippedPairs
private

Definition at line 35 of file PndLmdSensorAligner.h.

Referenced by calculateMatrix(), and init().

int PndLmdSensorAligner::swappedPairs
private

Definition at line 35 of file PndLmdSensorAligner.h.

Referenced by init().

int PndLmdSensorAligner::verbose
private

Definition at line 35 of file PndLmdSensorAligner.h.

Referenced by calculateMatrix(), init(), readPairsFromBinary(), and verboseLevel().


The documentation for this class was generated from the following files: