59 std::cerr <<
"PndLmdSensorAligner::Warning! Unnecessary copy-construction." <<
"\n";
76 if (s1 == s2 && s2 == s3 && s3 == s4 && s4 == s5 && s5 == s6) {
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";
92 cout <<
"=====================================================\n";
93 cout <<
"WARNING! Invalid pairs in pair file, check your data!\n";
94 cout <<
"=====================================================\n";
102 bool eventTimeCheck =
true;
103 double minDelta = 1e-6;
114 <<
"PndLmdSensrAligner::Error: Trying to use less than 5 pairs! (And that's not going to work.) Aborting.\n";
122 double* Model =
new double[dim * nPairs];
123 double* Template =
new double[dim * nPairs];
126 cout <<
"arranging pairs...\n";
129 cout <<
"num pairs from dec: " << nPairs <<
"\n";
133 for (
int ipair = 0; ipair < nPairs; ipair++) {
142 for (
int ipair = 0; ipair < nPairs; ipair++) {
145 Model[ipair * dim + 2] = (double) ipair;
148 Template[ipair * dim + 2] = (double) ipair;
156 if (
verbose == 3) cout <<
"applying artificial Z coordinate...\n";
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);
165 int modxinv = 0, modyinv = 0, modzinv = 0;
166 int temxinv = 0, temyinv = 0, temzinv = 0;
174 if (
verbose == 3) cout <<
"checking for zero values...\n";
176 for (
int iCheck = 0; iCheck < dim * nPairs; iCheck++) {
177 double val1 = abs(Model[iCheck]);
178 double val2 = abs(Template[iCheck]);
180 if (iCheck % 3 == 0) {
184 if (iCheck % 3 == 1) {
188 if (iCheck % 3 == 2) {
196 if (iCheck % 3 == 0) {
200 if (iCheck % 3 == 1) {
204 if (iCheck % 3 == 2) {
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";
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";
258 cout <<
"creating ICP...\n";
270 if (
verbose == 3) cout <<
"ICP and model created...\n";
276 translation =
Matrix(2, 1);
280 translation =
Matrix(3, 1);
284 icp.
fit(Template, nPairs, Rotation, translation, -1);
286 if (
verbose == 3) cout <<
"ICP fit step done.\n";
291 double* tempR =
new double[4];
292 double* tempT =
new double[2];
297 double* finalMatrix =
new double[16];
300 finalMatrix[0] = tempR[0];
301 finalMatrix[1] = tempR[1];
303 finalMatrix[3] = tempT[0];
304 finalMatrix[4] = tempR[2];
305 finalMatrix[5] = tempR[3];
307 finalMatrix[7] = tempT[1];
310 finalMatrix[10] = 1.0;
315 finalMatrix[15] = 1.0;
329 double* tempR =
new double[9];
330 double* tempT =
new double[3];
335 double* finalMatrix =
new double[16];
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];
365 std::stringstream alignlog;
368 if (
verbose == 3) cout <<
"ICP convergence ok.\n";
373 alignlog <<
"====================================================\n";
376 alignlog <<
"pairs available: " << nPairs;
377 if (nPairs < 100000) {
378 alignlog <<
" (WARNING! This is not enough for accurate alignment!)\n";
386 alignlog <<
" (WARNING! This is bad! Should be ~0.55)";
389 alignlog <<
"minDelta: " << minDelta <<
"\n";
391 alignlog <<
"EventTimeCheck: ";
392 if (eventTimeCheck) {
393 alignlog <<
"on (and passed)\n";
398 alignlog <<
"Force Instant: ";
405 alignlog <<
"====================================================\n";
409 alignlog <<
"====================================================\n";
410 alignlog <<
"CRITICAL ERROR:\n";
411 alignlog <<
"no convergence for overlapID " <<
overlapID <<
"." <<
"\n";
412 alignlog <<
"====================================================\n";
414 if (
verbose == 3) cout <<
"ICP did not converge!\n";
417 if (
verbose == 3) cout << alignlog.str();
463 int doublesPerPair = 6;
474 cout <<
"PndLmdSensorAligner::ERROR: x, y and z have different amounts of entries!\n";
486 cout <<
"warning: attempting to write empty binary pair file! (no pairs in buffer for overlapID "
491 size_t length = nPairs * doublesPerPair + 6;
497 double* header =
new double[6];
499 header[1] = doublesPerPair;
501 header[3] =
sizeof(double);
505 pdata =
new double[length];
508 for (
int i = 0;
i < 6;
i++) {
509 pdata[
i] = header[
i];
512 int currentIndex = 6;
515 for (
int i = 0;
i < nPairs;
i++) {
534 std::ofstream os(filename.c_str(), std::ios::binary |
std::ios::out);
536 cout <<
"ERROR! Could not write to " << filename <<
"!\n";
540 os.write(reinterpret_cast<const char*>(pdata), std::streamsize(length *
sizeof(
double)));
553 size_t doublesize =
sizeof(double);
574 std::fstream inStream(filename.c_str(), std::ios::binary | std::ios::in | std::ios::ate);
576 std::fstream::pos_type size = inStream.tellg();
580 cout << filename.c_str() <<
" could not be read!\n";
584 double* headersizeD =
new double[1];
586 if (filesize >=
sizeof(
double)) {
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)));
595 if (headersizeD[0] < 6) {
600 headersize = headersizeD[0];
604 if (filesize >= headersize *
sizeof(
double)) {
605 double* header =
new double[headersize];
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)));
615 cout <<
"file version: " << header[0] <<
"\n";
616 cout <<
"doubles / pair: " << header[1] <<
"\n";
617 cout <<
"no of pairs: " << header[2] <<
"\n";
621 doublesPerPair = header[1];
624 noOfDoubles = nPairs * doublesPerPair + headersize;
625 size_t filesizeMust =
sizeof(double) * (noOfDoubles);
627 if (doublesize != header[3]) {
629 <<
"warning! sizeof(double) on this system is different than on the system that made this binary!\n";
632 doublesize = header[3];
637 cout <<
"error! file name and overlapID do not match! did you rename the file?\n";
643 if (filesizeMust == filesize) {
644 if (
verbose == 3) cout <<
"file seems okay!\n";
647 cout <<
"file is corrupt!\n";
656 cout << filename.c_str() <<
" is too small, file corrupt!\n";
660 pdata =
new double[noOfDoubles];
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)));
670 int currentIndex = headersize;
673 bool compareBinaryToStored =
false;
675 if (compareBinaryToStored) {
677 cout <<
"fatal, can't compare to empty vector.\n";
684 for (
int i = 0;
i < nPairs;
i++) {
686 if (!compareBinaryToStored) {
718 currentIndex += doublesPerPair;
723 cout <<
"file check successful, everything okay!\n";
724 cout <<
"read " << nPairs <<
" from binary file.\n";
725 cout <<
"vector sizes:\n";
std::vector< double > simpleSensorTwoZ
std::vector< double > simpleSensorOneX
bool writePairsToBinary(const std::string directory)
virtual ~PndLmdSensorAligner()
static void error(int no)
static bool mkdir(std::string path)
bool addSimplePair(const PndLmdHitPair &pair)
const TVector3 & getHit1() const
void setVal(const int32_t m, const int32_t n, const FLOAT *val_)
static std::string makeBinaryPairFileName(int overlapId=0, bool incentimeters=true)
std::vector< double > simpleSensorTwoY
double fit(double *T, const int32_t T_num, Matrix &R, Matrix &t, const double indist)
const TVector3 & getHit2() const
std::vector< double > simpleSensorOneY
std::vector< double > simpleSensorOneZ
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 forceInstantResult(bool val)
bool readPairsFromBinary(const std::string directory)