57 <<
"PndLmdSensorAligner::Warning! Unnecessary copy-construction. These aligners should never be copied.\n";
64 int modxinv = 0, modyinv = 0, modzinv = 0;
65 int temxinv = 0, temyinv = 0, temzinv = 0;
69 if (
verbose == 3) cout <<
"checking for zero values...\n";
72 std::abs(pair[0]) < 1e-15 ? modxinv++ : modxinv;
73 std::abs(pair[1]) < 1e-15 ? modyinv++ : modxinv;
74 std::abs(pair[2]) < 1e-15 ? modzinv++ : modxinv;
75 std::abs(pair[3]) < 1e-15 ? temxinv++ : modxinv;
76 std::abs(pair[4]) < 1e-15 ? temyinv++ : modxinv;
77 std::abs(pair[5]) < 1e-15 ? temzinv++ : modxinv;
80 zeroVals = modxinv + modyinv + modzinv + temxinv + temyinv + temzinv;
83 double zeroFactor = zeroVals / ((double) nPairs * 6.0);
84 if (zeroFactor > 0.15 && zeroFactor <= 0.3) {
85 cout <<
"WARNING. More than 15 % of your entries is zero. That must be a mistake. \n";
86 cout <<
"Also, the kdtree creation could crash. Keep an eye out for that...\n";
87 cout <<
"Zero factor: " << zeroFactor <<
"\n";
88 cout <<
"model x vals invalid: " << modxinv / (double) nPairs <<
"\n";
89 cout <<
"model y vals invalid: " << modyinv / (double) nPairs <<
"\n";
90 cout <<
"model z vals invalid: " << modzinv / (double) nPairs <<
"\n";
91 cout <<
"templ x vals invalid: " << temxinv / (double) nPairs <<
"\n";
92 cout <<
"templ y vals invalid: " << temyinv / (double) nPairs <<
"\n";
93 cout <<
"templ z vals invalid: " << temzinv / (double) nPairs <<
"\n";
96 if (zeroFactor > 0.3) {
97 cout <<
"ERROR. More than 30 % of your entries is zero. That must be a mistake. \n";
98 cout <<
"Also, the kdtree creation will probably crash. Exiting.\n";
99 cout <<
"Zero factor: " << zeroFactor <<
"\n";
100 cout <<
"model x vals invalid: " << modxinv / (double) nPairs <<
"\n";
101 cout <<
"model y vals invalid: " << modyinv / (double) nPairs <<
"\n";
102 cout <<
"model z vals invalid: " << modzinv / (double) nPairs <<
"\n";
103 cout <<
"templ x vals invalid: " << temxinv / (double) nPairs <<
"\n";
104 cout <<
"templ y vals invalid: " << temyinv / (double) nPairs <<
"\n";
105 cout <<
"templ z vals invalid: " << temzinv / (double) nPairs <<
"\n";
106 cout <<
"=== additional data ===\n";
107 cout <<
"overlap id: " <<
overlapID <<
"\n";
108 cout <<
"no of Pairs: " << nPairs <<
"\n";
117 cerr <<
"WARNING. Can't apply dynamic cut because distance information is not stored.\n";
118 cerr <<
"Did you read an old version of the binary pair files?\n";
128 [](
const std::vector< double >&
a,
const std::vector< double >&
b) {
return
129 (a[0]*a[0] + a[1]*a[1]) < (
b[0]*
b[0] +
b[1]*
b[1]) ;});
131 int quantileMargin =
simplePairs.size() * (percent/100);
133 vector<vector<double> >::const_iterator first =
simplePairs.begin();
134 vector<vector<double> >::const_iterator last =
simplePairs.end() - quantileMargin;
135 vector<vector<double> > newSimplePairs(first, last);
151 cout <<
"=====================================================\n";
152 cout <<
"WARNING! Invalid pairs in pair file, check your data!\n";
153 cout <<
"=====================================================\n";
157 bool eventTimeCheck =
true;
158 double minDelta = 1e-6;
171 cerr <<
"PndLmdSensrAligner::Error: Trying to use less than 50 pairs!\n";
172 cerr <<
"(And that's not going to work.) Aborting.\n";
180 double* Model =
new double[
dim * nPairs];
181 double* Template =
new double[
dim * nPairs];
184 cout <<
"arranging pairs...\n";
186 cout <<
"num pairs from vec: " <<
simplePairs.size() <<
"\n";
187 cout <<
"num pairs from dec: " << nPairs <<
"\n";
191 for (
unsigned int ipair = 0; ipair < nPairs; ipair++) {
200 for (
unsigned int ipair = 0; ipair < nPairs; ipair++) {
211 cout <<
"creating ICP...\n";
222 if (
verbose == 3) cout <<
"ICP and model created...\n";
227 translation =
Matrix(2, 1);
231 translation =
Matrix(3, 1);
235 icp.
fit(Template, nPairs, Rotation, translation, -1);
237 if (
verbose == 3) cout <<
"ICP fit step done.\n";
242 double* tempR =
new double[4];
243 double* tempT =
new double[2];
248 double* finalMatrix =
new double[16];
251 finalMatrix[0] = tempR[0];
252 finalMatrix[1] = tempR[1];
254 finalMatrix[3] = tempT[0];
255 finalMatrix[4] = tempR[2];
256 finalMatrix[5] = tempR[3];
258 finalMatrix[7] = tempT[1];
261 finalMatrix[10] = 1.0;
266 finalMatrix[15] = 1.0;
280 double* tempR =
new double[9];
281 double* tempT =
new double[3];
286 double* finalMatrix =
new double[16];
290 finalMatrix[0] = tempR[0];
291 finalMatrix[1] = tempR[1];
292 finalMatrix[2] = tempR[2];
293 finalMatrix[3] = tempT[0];
294 finalMatrix[4] = tempR[3];
295 finalMatrix[5] = tempR[4];
296 finalMatrix[6] = tempR[5];
297 finalMatrix[7] = tempT[1];
298 finalMatrix[8] = tempR[6];
299 finalMatrix[9] = tempR[7];
300 finalMatrix[10] = tempR[8];
301 finalMatrix[11] = tempT[2];
317 std::stringstream alignlog;
320 if (
verbose == 3) cout <<
"ICP convergence ok.\n";
325 alignlog <<
"====================================================\n";
329 alignlog <<
"pairs available: " << nPairs;
330 if (nPairs < 100000) {
331 alignlog <<
" (WARNING! This is not enough for accurate alignment!)\n";
336 alignlog <<
"minDelta: " << minDelta <<
"\n";
338 alignlog <<
"EventTimeCheck: ";
339 eventTimeCheck ? alignlog <<
"on (and passed)\n" : alignlog <<
"off\n";
341 alignlog <<
"Force Instant: ";
342 forceInstant ? alignlog <<
"on\n" : alignlog <<
"off\n";
344 alignlog <<
"====================================================\n";
348 alignlog <<
"====================================================\n";
349 alignlog <<
"CRITICAL ERROR:\n";
350 alignlog <<
"no convergence for overlapID " <<
overlapID <<
"." <<
"\n";
351 alignlog <<
"====================================================\n";
353 if (
verbose == 3) cout <<
"ICP did not converge!\n";
356 if (
verbose == 3) cout << alignlog.str();
384 vector<double> tempPair;
386 tempPair.push_back(pair.
getHit1().x());
387 tempPair.push_back(pair.
getHit1().y());
388 tempPair.push_back(pair.
getHit1().z());
390 tempPair.push_back(pair.
getHit2().x());
391 tempPair.push_back(pair.
getHit2().y());
392 tempPair.push_back(pair.
getHit2().z());
399 vector<double> tempPair;
401 tempPair.push_back(pair.
getCol1());
402 tempPair.push_back(pair.
getRow1());
405 tempPair.push_back(pair.
getCol2());
406 tempPair.push_back(pair.
getRow2());
423 cout <<
"warning: attempting to write empty binary pair file! (no pairs in buffer for overlapID "
430 size_t length = nPairs * doublesPerPair + 6;
436 unsigned int headersize = 6;
438 double* header =
new double[headersize];
439 header[0] = headersize;
440 header[1] = doublesPerPair;
442 header[3] =
sizeof(double);
446 pdata =
new double[length];
449 for (
unsigned int i = 0;
i < headersize;
i++) {
450 pdata[
i] = header[
i];
453 unsigned int currentIndex = headersize;
456 for (
unsigned int iPair = 0; iPair < nPairs; iPair++) {
459 for (
unsigned int jPairIndex = 0; jPairIndex <
simplePairs[0].size(); jPairIndex++) {
460 pdata[currentIndex + jPairIndex] =
simplePairs[iPair][jPairIndex];
462 currentIndex += doublesPerPair;
473 std::ofstream os(filename.c_str(), std::ios::binary |
std::ios::out);
475 cout <<
"ERROR! Could not write to " << filename <<
"!\n";
479 os.write(reinterpret_cast<const char*>(pdata), std::streamsize(length *
sizeof(
double)));
492 size_t doublesize =
sizeof(double);
494 unsigned int doublesPerPair;
495 unsigned int noOfDoubles = 0;
513 std::fstream inStream(filename.c_str(), std::ios::binary | std::ios::in | std::ios::ate);
515 std::fstream::pos_type size = inStream.tellg();
519 cout << filename.c_str() <<
" could not be read!\n";
523 double* headersizeD =
new double[1];
525 if (filesize >=
sizeof(
double)) {
527 std::ifstream
is(filename.c_str(), std::ios::binary | std::ios::in);
528 if (!
is.is_open())
return false;
529 is.read(reinterpret_cast<char*>(headersizeD), std::streamsize(
sizeof(
double)));
534 if (headersizeD[0] < 6) {
539 headersize = headersizeD[0];
543 if (filesize >= headersize *
sizeof(
double)) {
544 double* header =
new double[headersize];
547 std::ifstream
is(filename.c_str(), std::ios::binary | std::ios::in);
548 if (!
is.is_open())
return false;
549 is.read(reinterpret_cast<char*>(header), std::streamsize(headersize *
sizeof(
double)));
554 cout <<
"header size: " << header[0] <<
"\n";
555 cout <<
"doubles / pair: " << header[1] <<
"\n";
556 cout <<
"no of pairs: " << header[2] <<
"\n";
560 doublesPerPair = header[1];
561 nPairs = std::round(header[2]);
563 noOfDoubles = nPairs * doublesPerPair + headersize;
564 size_t filesizeMust =
sizeof(double) * (noOfDoubles);
566 if (doublesize != header[3]) {
568 <<
"warning! sizeof(double) on this system is different than on the system that made this binary!\n";
571 doublesize = header[3];
576 cout <<
"error! file name and overlapID do not match! did you rename the file?\n";
579 if (filesizeMust != filesize) {
580 cout <<
"file is corrupt!\n";
589 cout << filename.c_str() <<
" is too small, file corrupt!\n";
594 pdata =
new double[noOfDoubles];
597 std::ifstream
is(filename.c_str(), std::ios::binary | std::ios::in);
598 if (!
is.is_open())
return false;
599 is.read(reinterpret_cast<char*>(pdata), std::streamsize(noOfDoubles *
sizeof(
double)));
604 unsigned int currentIndex = headersize;
607 for (
unsigned int iPair = 0; iPair < nPairs; iPair++) {
609 vector<double> tempPair;
611 for (
unsigned int jPairIndex = 0; jPairIndex < doublesPerPair; jPairIndex++) {
612 double currentVal = pdata[currentIndex + jPairIndex];
614 tempPair.push_back(currentVal);
619 currentIndex += doublesPerPair;
624 cerr <<
"Warning! Error while reading binary pair file!\n";
646 cerr <<
"PndLmdSensorAligner::check: ERROR! Aligner " <<
overlapID
647 <<
"doesn't have enough pairs! Aborting.\n";
654 if (pair.size() != dimy) {
666 double xcom, ycom, zcom, xspread, yspread, zspread,
xmin,
xmax, ymin, ymax, zmin, zmax, avgDist;
667 unsigned int nPairs = 0;
670 xcom = ycom = zcom = xspread = yspread = zspread = xmin = xmax = ymin = ymax = zmin = zmax = avgDist =
674 if (what == 0 || what == 1) {
687 if (what == 0 || what == 2) {
706 if (what == 0 || what == 1) {
719 if (what == 0 || what == 2) {
736 if (what == 1 || what == 2) {
743 else if (what == 0) {
744 xcom /= 2.0 * nPairs;
745 ycom /= 2.0 * nPairs;
746 zcom /= 2.0 * nPairs;
748 avgDist /= 2.0 * nPairs;
751 xspread = std::abs(xmax - xmin);
752 yspread = std::abs(ymax - ymin);
753 zspread = std::abs(zmax - zmin);
755 std::vector<double> spread;
757 spread.push_back(xcom);
758 spread.push_back(ycom);
759 spread.push_back(zcom);
761 spread.push_back(xspread);
762 spread.push_back(yspread);
763 spread.push_back(zspread);
765 spread.push_back(avgDist);
775 cout <<
"x com: " << spread[0] <<
"\n";
776 cout <<
"y com: " << spread[1] <<
"\n";
777 cout <<
"z com: " << spread[2] <<
"\n";
779 cout <<
"x spread: " << spread[3] <<
"\n";
780 cout <<
"y spread: " << spread[4] <<
"\n";
781 cout <<
"z spread: " << spread[5] <<
"\n";
783 cout <<
"avg Dist: " << spread[6] <<
"\n";
836 if (trafoMatrix.
m != 4 || trafoMatrix.
n != 4 || pair.size() < 6) {
837 std::cerr <<
"ERROR. Matrix is not 4x4 or pair has less than 6 entries\n";
844 pairVal1.
val[0][0] = pair[0];
845 pairVal1.
val[1][0] = pair[1];
846 pairVal1.
val[2][0] = pair[2];
847 pairVal1.
val[3][0] = 1.0;
849 pairVal2.
val[0][0] = pair[3];
850 pairVal2.
val[1][0] = pair[4];
851 pairVal2.
val[2][0] = pair[5];
852 pairVal2.
val[3][0] = 1.0;
854 pairVal1 = trafoMatrix * pairVal1;
855 pairVal2 = trafoMatrix * pairVal2;
858 pair[0] = pairVal1.
val[0][0] / pairVal1.val[3][0];
859 pair[1] = pairVal1.val[1][0] / pairVal1.val[3][0];
860 pair[2] = pairVal1.val[2][0] / pairVal1.val[3][0];
862 pair[3] = pairVal2.val[0][0] / pairVal2.val[3][0];
863 pair[4] = pairVal2.val[1][0] / pairVal2.val[3][0];
864 pair[5] = pairVal2.val[2][0] / pairVal2.val[3][0];
std::mutex geometryHelperMutex
unsigned int numberOfPairs
Matrix transformToSensorOne()
bool writePairsToBinary(const std::string directory)
std::vector< std::vector< double > > simplePairs
virtual ~PndLmdSensorAligner()
static PndLmdGeometryHelper & getInstance()
void transformPair(Matrix &trafoMatrix, std::vector< double > &pair)
friend F32vec4 max(const F32vec4 &a, const F32vec4 &b)
static bool mkdir(std::string path)
bool addSimplePair(const PndLmdHitPair &pair)
unsigned int nonSanePairs
void applyDynamicCut(double percent=5.0)
const TVector3 & getHit1() const
void setVal(const int32_t m, const int32_t n, const FLOAT *val_)
static Matrix castTGeoHMatrixToMatrix(const TGeoHMatrix &matrix)
static std::string makeBinaryPairFileName(int overlapId=0, bool incentimeters=true)
PndLmdGeometryHelper * helper
std::vector< double > getPairSpread(int what=0)
unsigned int skippedPairs
Double_t getDistance() const
unsigned int swappedPairs
const TGeoHMatrix getMatrixPndGlobalToLmdLocal()
Matrix transformToLmdLocal()
double fit(double *T, const int32_t T_num, Matrix &R, Matrix &t, const double indist)
const TGeoHMatrix getMatrixPndGlobalToSensor(const int sensorId)
const TVector3 & getHit2() const
int getSensorOneFromOverlapID(int overlapID)
friend F32vec4 min(const F32vec4 &a, const F32vec4 &b)
unsigned int lastNoOfPairs
PndLmdAlignManager * superManager
static Matrix inv(const Matrix &M)
unsigned int maxNoOfPairs
void getData(FLOAT *val_, int32_t i1=0, int32_t j1=0, int32_t i2=-1, int32_t j2=-1)
void forceInstantResult(bool val)
void printPairSpread(int what=0)
bool readPairsFromBinary(const std::string directory)