3 #include <TGeoManager.h>
4 #include <TGeoMatrix.h>
5 #include <TGeoPhysicalNode.h>
10 #include <TClonesArray.h>
12 #include <FairRunSim.h>
32 cout <<
"creating dummy simulation to populate gGeoManager...\n";
33 TString geometryFile =
"Luminosity-Detector.root";
35 FairRunSim *
fRun =
new FairRunSim();
36 fRun->SetName(
"TGeant4");
37 fRun->SetOutputFile(simOutput);
38 fRun->SetMaterials(
"media_pnd.geo");
40 Cave->SetGeometryFileName(
"pndcave.geo");
41 fRun->AddModule(Cave);
43 Pipe->SetGeometryFileName(
"beampipe_201309.root");
44 fRun->AddModule(Pipe);
46 Magnet->SetGeometryFileName(
"FullSolenoid_V842.root");
47 fRun->AddModule(Magnet);
49 Dipole->SetGeometryFileName(
"dipole.geo");
50 fRun->AddModule(Dipole);
53 Lum->SetGeometryFileName(geometryFile);
59 bool misaligned =
true;
62 TFile *misalignmentMatrixRootfile =
new TFile(filename,
"READ");
64 if (misalignmentMatrixRootfile->IsOpen()) {
65 std::map<std::string, TGeoHMatrix> *matrices;
67 gDirectory->GetObject(
"PndLmdMisalignMatrices", matrices);
68 misalignmentMatrixRootfile->Close();
70 std::cout << matrices->size()
71 <<
" matrices successfully read from file.\napplying misalignment.\n";
74 for (
auto const& entry : *matrices) {
79 TGeoNode* currentNode =
gGeoManager->GetCurrentNode();
80 TGeoMatrix* matrixToNode = currentNode->GetMatrix();
82 TGeoHMatrix misalignedMatrixToNode = *matrixToNode * entry.second;
85 TGeoHMatrix* newMatrixToNode =
new TGeoHMatrix(misalignedMatrixToNode);
87 TGeoPhysicalNode* physicalNode =
gGeoManager->MakePhysicalNode(volPath);
89 physicalNode->Align(newMatrixToNode);
91 std::cout <<
"all misalignments applied.\n";
94 std::cout <<
"file could not be read\n";
100 bool misaligned =
true;
103 TFile *misalignmentMatrixRootfile =
new TFile(filename,
"READ");
105 if (misalignmentMatrixRootfile->IsOpen()) {
106 std::map<std::string, TGeoHMatrix> *matrices;
108 gDirectory->GetObject(
"PndLmdMisalignMatrices", matrices);
109 misalignmentMatrixRootfile->Close();
111 std::cout <<
"resetting geometry!.\n";
114 for (
auto const& entry : *matrices) {
119 TGeoNode* currentNode =
gGeoManager->GetCurrentNode();
120 TGeoMatrix* matrixToNode = currentNode->GetMatrix();
123 TGeoHMatrix inverseMisalign = entry.second.Inverse();
125 TGeoHMatrix misalignedMatrixToNode = *matrixToNode * inverseMisalign;
128 TGeoHMatrix* newMatrixToNode =
new TGeoHMatrix(misalignedMatrixToNode);
130 TGeoPhysicalNode* physicalNode =
gGeoManager->MakePhysicalNode(volPath);
132 physicalNode->Align(newMatrixToNode);
136 std::cout <<
"file could not be read\n";
146 stringstream *valueStream =
new stringstream();
149 ifs.open(filename.c_str());
150 if (!ifs.is_open()) {
151 cout <<
"could not read ";
153 cout <<
"! aborting." <<
"\n";
157 while (std::getline(ifs, linebuffer)) {
158 *valueStream << linebuffer <<
"\n";
163 std::stringstream *ss;
164 std::stringstream iss;
168 std::string line, token;
169 std::vector<std::vector<double> > data;
170 while (getline(*ss, line)) {
172 std::vector<double> tempvec;
173 while (getline(iss, token,
',')) {
174 tempvec.push_back(std::stod(token));
176 data.push_back(tempvec);
186 if (data.size() < 1) {
187 cout <<
"warning! can't read matrix from file " << filename <<
"\n";
190 if (data[0].size() < 1) {
191 cout <<
"warning! can't read matrix from file " << filename <<
"\n";
200 columns = data[0].size();
202 if (rows != 4 || columns != 4) {
203 cerr <<
"wrong matrix dimensions.\n";
217 trans[0] = data[0][3];
218 trans[1] = data[1][3];
219 trans[2] = data[2][3];
223 result.SetRotation(rot);
224 result.SetDx(trans[0]);
225 result.SetDy(trans[1]);
226 result.SetDz(trans[2]);
232 cout <<
"reading files from file: " << filename <<
"\n";
234 TFile *misalignmentMatrixRootfile =
new TFile(filename,
"READ");
236 if (misalignmentMatrixRootfile->IsOpen()) {
237 std::map<std::string, TGeoHMatrix> *matrices;
239 gDirectory->GetObject(
"PndLmdMisalignMatrices", matrices);
240 misalignmentMatrixRootfile->Close();
241 cout << matrices->size() <<
" matrices successfully read from file.\n";
246 cout <<
"file could not be read\n";
254 const double *translationDesign = mat1.GetTranslation();
255 const double *translationICP = mat2.GetTranslation();
256 const double *rotationDesign = mat1.GetRotationMatrix();
257 const double *rotationICP = mat2.GetRotationMatrix();
259 double dx = translationDesign[0] - translationICP[0];
260 double dy = translationDesign[1] - translationICP[1];
261 double da = rotationDesign[1] - rotationICP[1];
263 cout <<
"\n=-=-=-=-=-=-=-=-=-=\n";
264 cout <<
"differences are:\ndx: " << dx * 1e4 <<
"\ndy: " << dy * 1e4 <<
"\n";
265 cout <<
"=-=-=-=-=-=-=-=-=-=\n";
275 return toSen0.Inverse() * toSen1;
280 return TGeoHMatrix(toBaseMatrix * input * toBaseMatrix.Inverse());
286 TGeoHMatrix misalignmentToSensor0 = (*matricesMisaligned)[info.
path1];
287 TGeoHMatrix misalignmentToSensor1 = (*matricesMisaligned)[info.
path2];
292 return misalignmentToSensor0.Inverse() * misSen1inSen0;
301 return sen0to1ICPcorr * sen0to1;
310 return sen0to1ICPcorr * sen0to1;
315 for (
auto &info : infos) {
316 int smallOverlapHere = info.
overlapID % 10;
317 if (smallOverlap == smallOverlapHere) {
325 const double *translationDesign = mat1.GetTranslation();
326 const double *translationICP = mat2.GetTranslation();
327 const double *rotationDesign = mat1.GetRotationMatrix();
328 const double *rotationICP = mat2.GetRotationMatrix();
330 double dx = translationDesign[0] - translationICP[0];
331 double dy = translationDesign[1] - translationICP[1];
332 double da = rotationDesign[1] - rotationICP[1];
334 std::vector<double> result;
335 result.push_back(dx);
336 result.push_back(dy);
337 result.push_back(da);
343 TString idealMatrices =
"idealMatrices.root";
345 TString misalignedMatrices =
"";
346 std::string pathPrefix =
"/home/arbeit/RedPro3TB/simulationData/";
348 std::string pathPost =
"LMDmatrices-python-cut-" + cut +
"-2D/";
352 switch (alignParam) {
354 path =
"2018-08-himster2-aligned/";
355 pdfPath =
"aligned/cut-" + cut +
"/";
358 misalignedMatrices =
"misalignMatrices-SensorsOnly-10.root";
359 path =
"2018-08-himster2-misalign-10u/";
360 pdfPath =
"misalign-10u/cut-" + cut +
"/";
363 misalignedMatrices =
"misalignMatrices-SensorsOnly-50.root";
364 path =
"2018-08-himster2-misalign-50u/";
365 pdfPath =
"misalign-50u/cut-" + cut +
"/";
368 misalignedMatrices =
"misalignMatrices-SensorsOnly-100.root";
369 path =
"2018-08-himster2-misalign-100u/";
370 pdfPath =
"misalign-100u/cut-" + cut +
"/";
373 misalignedMatrices =
"misalignMatrices-SensorsOnly-150.root";
374 path =
"2018-08-himster2-misalign-150u/";
375 pdfPath =
"misalign-150u/cut-" + cut +
"/";
378 misalignedMatrices =
"misalignMatrices-SensorsOnly-200.root";
379 path =
"2018-08-himster2-misalign-200u/";
380 pdfPath =
"misalign-200u/cut-" + cut +
"/";
383 misalignedMatrices =
"misalignMatrices-SensorsOnly-250.root";
384 path =
"2018-08-himster2-misalign-250u/";
385 pdfPath =
"misalign-250u/cut-" + cut +
"/";
392 std::map<int, TGeoHMatrix> matricesByHand;
393 std::map<int, TGeoHMatrix> matricesByGeoManager;
395 bool compareWithIdeal =
false;
396 string ext =
"cm.mat";
398 if (compareWithIdeal) {
399 cout <<
"getting matrices by hand | ICP-like matrices from gGeoManager\n";
402 cout <<
"getting matrices by hand | real ICP matrices\n";
405 path = pathPrefix + path + pathPost;
407 gSystem->Exec((
"mkdir -p " + pdfPath).c_str());
409 for (
int iHalf = 0; iHalf < 2; iHalf++) {
410 for (
int iPlane = 0; iPlane < 4; iPlane++) {
411 for (
int iModule = 0; iModule < 5; iModule++) {
414 std::vector<PndLmdOverlapInfo> overlaps = helper->
getOverlapInfos(iHalf, iPlane, iModule);
427 if (compareWithIdeal) {
463 TGeoHMatrix sen1to2 = mat4 * mat5.Inverse();
464 TGeoHMatrix sen1to3 = mat4 * mat1.Inverse();
465 TGeoHMatrix sen1to4a = mat4 * mat5.Inverse() * mat6 * mat2.Inverse();
466 TGeoHMatrix sen1to4b = mat4 * mat1.Inverse() * mat7 * mat8.Inverse();
468 TGeoHMatrix sen1to5 = mat0;
469 TGeoHMatrix sen1to6 = mat4 * mat1.Inverse() * mat3;
470 TGeoHMatrix sen1to7 = mat4 * mat1.Inverse() * mat7;
471 TGeoHMatrix sen1to8 = mat4;
472 TGeoHMatrix sen1to9a = mat4 * mat5.Inverse() * mat6;
473 TGeoHMatrix sen1to9b = mat4 * mat1.Inverse() * mat7 * mat8.Inverse() * mat2;
475 int matrixId = 1000 * iHalf + 100 * iPlane + 10 * iModule;
477 matricesByHand[matrixId + 2] = sen1to2;
478 matricesByHand[matrixId + 3] = sen1to3;
479 matricesByHand[matrixId + 4] = sen1to4a;
480 matricesByHand[matrixId + 5] = sen1to5;
481 matricesByHand[matrixId + 6] = sen1to6;
482 matricesByHand[matrixId + 7] = sen1to7;
483 matricesByHand[matrixId + 8] = sen1to8;
484 matricesByHand[matrixId + 9] = sen1to9a;
489 if (alignParam > 0) {
493 cout <<
"getting matrices by geoManager after misalignment\n";
494 for (
int iHalf = 0; iHalf < 2; iHalf++) {
495 for (
int iPlane = 0; iPlane < 4; iPlane++) {
496 for (
int iModule = 0; iModule < 5; iModule++) {
499 std::vector<PndLmdOverlapInfo> overlaps = helper->
getOverlapInfos(iHalf, iPlane, iModule);
501 int matrixId = 1000 * iHalf + 100 * iPlane + 10 * iModule;
514 int sensor0 = info0.
id1;
515 int sensor1 = info4.
id1;
517 int sensor3 = info1.
id1;
518 int sensor4 = info2.
id1;
519 int sensor5 = info0.
id2;
520 int sensor6 = info3.
id2;
521 int sensor7 = info7.
id2;
522 int sensor8 = info4.
id2;
523 int sensor9 = info6.
id2;
534 matricesByGeoManager[matrixId + 2] = sen1to2;
535 matricesByGeoManager[matrixId + 3] = sen1to3;
536 matricesByGeoManager[matrixId + 4] = sen1to4a;
537 matricesByGeoManager[matrixId + 5] = sen1to5;
538 matricesByGeoManager[matrixId + 6] = sen1to6;
539 matricesByGeoManager[matrixId + 7] = sen1to7;
540 matricesByGeoManager[matrixId + 8] = sen1to8;
541 matricesByGeoManager[matrixId + 9] = sen1to9a;
547 if (alignParam > 0) {
551 cout <<
"making histograms\n";
552 double dalpha,
dx,
dy, dxTarget, dyTarget, daTarget, dxICP, dyICP, daICP;
553 TH1D histA(
"h1",
"h1", 25, -1, -1);
554 TH1D histX(
"h2",
"h2", 25, -1, -1);
555 TH1D histY(
"h3",
"h3", 25, -1, -1);
557 for (
auto &
i : matricesByHand) {
559 auto thisDiff =
getMatrixDiff(matricesByHand[key], matricesByGeoManager[key]);
562 histA.Fill(thisDiff[2] * 1e3);
563 histX.Fill(thisDiff[0] * 1e4);
564 histY.Fill(thisDiff[1] * 1e4);
568 histA.GetXaxis()->SetTitle(
"d#alpha [#mrad]");
569 histX.GetXaxis()->SetTitle(
"dx [#mum]");
570 histY.GetXaxis()->SetTitle(
"dy [#mum]");
571 histA.GetYaxis()->SetTitle(
"Entries");
572 histX.GetYaxis()->SetTitle(
"Entries");
573 histY.GetYaxis()->SetTitle(
"Entries");
574 histA.SetTitle(
"d#alpha-combined");
575 histX.SetTitle(
"dx-combined");
576 histY.SetTitle(
"dy-combined");
578 TCanvas canvas(
"c1",
"c1", 800, 600);
581 canvas.Print((pdfPath +
"dAlpha-combined.pdf").c_str());
584 canvas.Print((pdfPath +
"dx-combined.pdf").c_str());
587 canvas.Print((pdfPath +
"dy-combined.pdf").c_str());
594 cout <<
"got " << overlaps.size() <<
" overlaps\n";
598 for (
auto &overlap : overlaps) {
601 double *mat1 =
new double[16];
602 overlap.mat1.GetHomogenousMatrix(mat1);
603 double *mat2 =
new double[16];
604 overlap.mat2.GetHomogenousMatrix(mat2);
606 j[std::to_string(overlap.overlapID)][
"overlapID"] = overlap.overlapID;
607 j[std::to_string(overlap.overlapID)][
"id1"] = overlap.id1;
608 j[std::to_string(overlap.overlapID)][
"id2"] = overlap.id2;
609 j[std::to_string(overlap.overlapID)][
"path1"] = overlap.path1;
610 j[std::to_string(overlap.overlapID)][
"path2"] = overlap.path2;
611 j[std::to_string(overlap.overlapID)][
"matrix1"] = {
612 mat1[0], mat1[1], mat1[2], mat1[12],
613 mat1[4], mat1[5], mat1[6], mat1[13],
614 mat1[8], mat1[9], mat1[10], mat1[14],
615 mat1[3], mat1[7], mat1[11], mat1[15]
617 j[std::to_string(overlap.overlapID)][
"matrix2"] = {
618 mat2[0], mat2[1], mat2[2], mat2[12],
619 mat2[4], mat2[5], mat2[6], mat2[13],
620 mat2[8], mat2[9], mat2[10], mat2[14],
621 mat2[3], mat2[7], mat2[11], mat2[15]
625 cout <<
"got " << j.size() <<
" json objects\n";
627 std::ofstream o(
"detectorMatricesIdeal.json");
628 o << std::setw(2) << j << std::endl;
643 cout << setprecision(16);
655 std::string shiftDataPath =
"/home/arbeit/RedPro3TB/simulationData/2018-10-himster2-misalignData-200u/";
656 std::string shiftGeoPath =
"/home/arbeit/RedPro3TB/simulationData/2018-08-himster2-misalign-200u/";
657 std::string pathPre =
"LMDmatrices-python-cut-";
658 std::string pathPost =
"-2D";
659 std::string
ext =
"cm.mat";
660 std::string cutPath =
"";
662 std::string pdfPathPre =
"ShiftGeovsShiftData/";
669 std::vector<string>
cuts {
"0"};
673 TH1D histA(
"h1",
"h1", 25, -1, -1);
674 TH1D histX(
"h2",
"h2", 25, -1, -1);
675 TH1D histY(
"h3",
"h3", 25, -1, -1);
678 cutPath = pathPre +
cut + pathPost;
679 pdfPath = pdfPathPre +
"cut-" +
cut +
"/";
680 gSystem->Exec((
"mkdir -p " + pdfPath).c_str());
684 for (
auto &overlap : overlaps) {
687 filename = shiftGeoPath + cutPath +
"/m" + std::to_string(overlap.overlapID) +
ext;
691 filename = shiftDataPath + cutPath +
"/m" + std::to_string(overlap.overlapID) +
ext;
700 histA.Fill(thisDiff[2] * 1e3);
701 histX.Fill(thisDiff[0] * 1e4);
702 histY.Fill(thisDiff[1] * 1e4);
707 histA.GetXaxis()->SetTitle(
"d#alpha [#mrad]");
708 histX.GetXaxis()->SetTitle(
"dx [#mum]");
709 histY.GetXaxis()->SetTitle(
"dy [#mum]");
710 histA.GetYaxis()->SetTitle(
"Entries");
711 histX.GetYaxis()->SetTitle(
"Entries");
712 histY.GetYaxis()->SetTitle(
"Entries");
713 histA.SetTitle(
"ICP | shift Geo vs Shift Data, d#alpha");
714 histX.SetTitle(
"ICP | shift Geo vs Shift Data, px");
715 histY.SetTitle(
"ICP | shift Geo vs Shift Data, py");
717 TCanvas canvas(
"c1",
"c1", 800, 600);
720 canvas.Print((pdfPath +
"dAlpha.pdf").c_str());
723 canvas.Print((pdfPath +
"dx.pdf").c_str());
726 canvas.Print((pdfPath +
"dy.pdf").c_str());
733 TString idealMatrices =
"idealMatrices.root";
735 TString misalignedMatrices =
"";
736 std::string pathMisalign;
737 std::string pathPrefix =
"/home/arbeit/RedPro3TB/simulationData/";
739 std::string pathPost =
"LMDmatrices-python-cut-" + cut +
"-2D/";
741 std::string
ext =
"cm.mat";
743 std::string fullPath =
"/home/roman/temp/himster2misaligned-move-data/Pairs/LMDmatrices/";
745 switch (alignParam) {
747 pathMisalign =
"2018-08-himster2-aligned/";
748 pdfPath =
"aligned/cut-" + cut +
"/singleMatrices/";
751 misalignedMatrices =
"misalignMatrices-SensorsOnly-10.root";
752 pathMisalign =
"2018-08-himster2-misalign-10u/";
753 pdfPath =
"misalign-10u/cut-" + cut +
"/singleMatrices/";
756 misalignedMatrices =
"misalignMatrices-SensorsOnly-50.root";
757 pathMisalign =
"2018-08-himster2-misalign-50u/";
758 pdfPath =
"misalign-50u/cut-" + cut +
"/singleMatrices/";
761 misalignedMatrices =
"misalignMatrices-SensorsOnly-100.root";
762 pathMisalign =
"2018-08-himster2-misalign-100u/";
763 pdfPath =
"misalign-100u/cut-" + cut +
"/singleMatrices/";
766 misalignedMatrices =
"misalignMatrices-SensorsOnly-150.root";
767 pathMisalign =
"2018-08-himster2-misalign-150u/";
768 pdfPath =
"misalign-150u/cut-" + cut +
"/singleMatrices/";
771 misalignedMatrices =
"misalignMatrices-SensorsOnly-200.root";
772 pathMisalign =
"2018-08-himster2-misalign-200u/";
773 pdfPath =
"misalign-200u/cut-" + cut +
"/singleMatrices/";
776 misalignedMatrices =
"misalignMatrices-SensorsOnly-250.root";
777 pathMisalign =
"2018-08-himster2-misalign-250u/";
778 pdfPath =
"misalign-250u/cut-" + cut +
"/singleMatrices/";
783 if (alignParam != 0) {
789 std::string path = pathPrefix + pathMisalign + pathPost;
790 gSystem->Exec((
"mkdir -p " + pdfPath).c_str());
792 cout <<
"making histograms\n";
793 double dalpha,
dx,
dy, dxTarget, dyTarget, daTarget, dxICP, dyICP, daICP;
794 TH1D histA(
"h1",
"h1", 25, -1, -1);
795 TH1D histX(
"h2",
"h2", 25, -1, -1);
796 TH1D histY(
"h3",
"h3", 25, -1, -1);
799 for (
auto &overlap : overlaps) {
803 filename = path +
"/m" + std::to_string(overlap.overlapID) +
ext;
805 TGeoHMatrix matIdeal;
807 if (alignParam != 0) {
817 if (thisDiff[1] * 1e4 > 40.0) {
818 cout <<
"Alarm! " << overlap.overlapID <<
"\n";
822 histA.Fill(thisDiff[2] * 1e3);
823 histX.Fill(thisDiff[0] * 1e4);
824 histY.Fill(thisDiff[1] * 1e4);
828 histA.GetXaxis()->SetTitle(
"d#alpha [#mrad]");
829 histX.GetXaxis()->SetTitle(
"dx [#mum]");
830 histY.GetXaxis()->SetTitle(
"dy [#mum]");
831 histA.GetYaxis()->SetTitle(
"Entries");
832 histX.GetYaxis()->SetTitle(
"Entries");
833 histY.GetYaxis()->SetTitle(
"Entries");
834 histA.SetTitle(
"ICP vs ideal, d#alpha");
835 histX.SetTitle(
"ICP vs ideal, px");
836 histY.SetTitle(
"ICP vs ideal, py");
838 TCanvas canvas(
"c1",
"c1", 800, 600);
841 canvas.Print((pdfPath +
"dAlpha.pdf").c_str());
844 canvas.Print((pdfPath +
"dx.pdf").c_str());
847 canvas.Print((pdfPath +
"dy.pdf").c_str());
871 cout <<
"starting.\n";
void initDummySimulation()
void compareShiftDataShiftGeo()
TGeoHMatrix getOverlapMatrixLikeICP(PndLmdOverlapInfo info)
void checkPairOrientation()
std::vector< double > getMatrixDiff(TGeoHMatrix &mat1, TGeoHMatrix &mat2)
void SetExclusiveSensorType(const TString sens)
TGeoHMatrix baseTransformation(TGeoHMatrix &input, TGeoHMatrix &toBaseMatrix)
static PndLmdGeometryHelper & getInstance()
TGeoHMatrix readMatrixFromDisk(std::string filename)
TGeoManager * gGeoManager
std::map< std::string, TGeoHMatrix > * matricesMisaligned
void applyMisalignmentToGGeoManager(TString filename)
void printMatrixDiff(TGeoHMatrix mat1, TGeoHMatrix mat2)
void buildCyclic(int alignParam, string cut)
TGeoHMatrix getMisalignedOverlapFromGeoManager(PndLmdOverlapInfo &info)
std::vector< PndLmdOverlapInfo > getOverlapInfos(int iHalf=-1, int iPlane=-1, int iModule=-1)
PndLmdGeometryHelper * helper
void saveDetMatricesToJson()
std::map< std::string, TGeoHMatrix > * readRootMatrices(TString filename)
const TGeoHMatrix getMatrixPndGlobalToSensor(const int sensorId)
std::map< std::string, TGeoHMatrix > * matricesIdeal
void compareICPmatrices(int alignParam, string cut)
void saveMatricesToJson()
PndLmdOverlapInfo & getSmallOverlapInfo(std::vector< PndLmdOverlapInfo > &infos, int smallOverlap)
void unApplyMisalignmentToGGeoManager(TString filename)
TGeoHMatrix getMatrixSensorToSensor(int sensorOne, int sensorTwo)
TGeoHMatrix getMisalignedOverlapFromICP(PndLmdOverlapInfo &info, std::string ICPmatrix)