10 #include <boost/bind.hpp>
11 #include <boost/lexical_cast.hpp>
12 #include <boost/filesystem.hpp>
13 #include <boost/ref.hpp>
14 #include <boost/regex.hpp>
28 #include <TClonesArray.h>
30 #include <TGeoMatrix.h>
38 using std::stringstream;
47 _info <<
"info for aligned areas\n";
81 std::cout <<
"PndLmdAlignManager::Init(): Initialization successful." <<
"\n";
88 std::cout <<
"using " <<
aligners.size() <<
" aligners, which have:\n";
90 cout <<
"id: " << it->second.getModuleID() <<
" has " << it->second.getNoOfPairs() <<
" pairs."
112 std::vector<string> list;
114 for (
size_t i = 0;
i < list.size();
i++) {
116 if ((
int) i == maxFiles - 1) {
121 cout <<
"looking for files in " << directory <<
". choose " <<
fileNames.size()
122 <<
" files of maximum of " << maxFiles <<
".\n";
136 cout <<
"found " << noOfFiles <<
" file(s). reading...\n";
139 cout <<
"no files found. exiting.\n";
143 TChain* chainPairs =
new TChain(
"pndsim");
146 if (
fileNames[
i].find(
"Lumi_Pairs") != std::string::npos) {
152 TClonesArray* hitPairs =
new TClonesArray(
"PndLmdHitPair");
153 chainPairs->SetBranchAddress(
"PndLmdHitPair", &hitPairs);
154 int nEntries = chainPairs->GetEntries();
155 cout <<
"HitPairs no of entries: " << nEntries <<
"\n";
157 cout <<
"Sorting Pairs to Manager...\n";
159 for (
int i_event = 0; i_event < nEntries; i_event++) {
161 loadBar(i_event, nEntries, 1000, 60);
162 chainPairs->GetEntry(i_event);
163 int nPairs = hitPairs->GetEntries();
166 for (
int i_Pair = 0; i_Pair < nPairs; i_Pair++) {
181 cout <<
"================================ \n";
182 cout <<
"total Pairs: " << totalPairs <<
"\n";
183 cout <<
"All done. Running Align Manager. \n";
184 cout <<
"================================ \n";
194 if (!aligner.
check()) {
219 std::lock_guard<std::mutex> lock(
MTLBmutex);
230 if (i % (n / r) != 0) {
237 float ratio = i / (float) n;
241 printf(
"%3d%% [", (
int) (ratio * 100));
244 for (
int x = 0;
x <
c;
x++)
247 for (
int x = c;
x < w;
x++)
252 printf(
"] %s %d of %d \n\033[F\033[J", message.c_str(),
i,
n);
261 for (
size_t iFile = 0; iFile <
fileNames.size(); iFile++) {
262 if (!boost::filesystem::exists(
fileNames[iFile])) {
263 cout <<
"error opening file:";
271 if (!boost::filesystem::exists(outPath.parent_path())) {
272 boost::filesystem::create_directories(outPath.parent_path());
292 if (temp.size() < 1) {
293 cout <<
"warning! can't read matrix from file " << filename <<
"\n";
294 return TGeoHMatrix();
296 if (temp[0].size() < 1) {
297 cout <<
"warning! can't read matrix from file " << filename <<
"\n";
298 return TGeoHMatrix();
303 columns = temp[0].size();
304 Matrix resultMat(rows, columns);
307 double translation[3];
310 for (
int i = 0;
i < rows;
i++) {
311 for (
int j = 0; j < columns; j++) {
312 resultMat.
val[
i][j] = temp[
i][j];
317 if (resultMat.
m != 4 || resultMat.
n != 4) {
318 cerr <<
"Error! Invalid matrix!\n";
319 return TGeoHMatrix();
323 rotation[0] = resultMat.
val[0][0];
324 rotation[1] = resultMat.
val[0][1];
325 rotation[2] = resultMat.
val[0][2];
326 rotation[3] = resultMat.
val[1][0];
327 rotation[4] = resultMat.
val[1][1];
328 rotation[5] = resultMat.
val[1][2];
329 rotation[6] = resultMat.
val[2][0];
330 rotation[7] = resultMat.
val[2][1];
331 rotation[8] = resultMat.
val[2][2];
333 translation[0] = resultMat.
val[0][3];
334 translation[1] = resultMat.
val[1][3];
335 translation[2] = resultMat.
val[2][3];
337 result.SetRotation(rotation);
338 result.SetTranslation(translation);
345 if (!(filename ==
"")) {
346 boost::filesystem::path outPath(filename);
347 if (!boost::filesystem::exists(outPath.parent_path())) {
348 boost::filesystem::create_directories(outPath.parent_path());
353 std::ofstream outFileStream;
354 outFileStream.open(filename.c_str());
356 if (outFileStream.fail()) {
360 outFileStream << std::setprecision(16);
361 outFileStream << mat;
362 outFileStream.close();
368 std::vector<std::vector<double> > data;
370 ifs.open(filename.c_str());
375 while (getline(ifs, line)) {
377 std::vector<double> tempvec;
378 while (getline(iss, token,
',')) {
379 tempvec.push_back(boost::lexical_cast<double>(token));
381 data.push_back(tempvec);
387 throw std::runtime_error(
"Unable to open file " + filename);
394 bool includeSubDirs) {
396 if (!boost::filesystem::exists(path)) {
399 boost::filesystem::directory_iterator iterator(path);
400 for (; iterator != boost::filesystem::directory_iterator(); ++iterator) {
401 boost::filesystem::path thisPath = iterator->path();
402 if (boost::filesystem::is_directory(thisPath) && includeSubDirs) {
403 list.push_back(thisPath.string());
404 searchFiles(thisPath.string(), list, detail, includeSubDirs);
406 else if (boost::filesystem::is_regular_file(thisPath)) {
407 if (thisPath.string().find(detail) != std::string::npos) {
408 list.push_back(thisPath.string());
412 sort(list.begin(), list.end());
417 boost::filesystem::path bpath(path);
418 if (boost::filesystem::exists(bpath)) {
421 return boost::filesystem::create_directories(bpath);
425 return boost::filesystem::exists(boost::filesystem::path(path));
430 boost::regex expression(regex);
431 std::string::const_iterator start, end;
432 start = source.begin();
434 boost::match_results<std::string::const_iterator> result;
435 boost::match_flag_type flags = boost::match_default;
437 vector<string> resultStrings;
439 while (regex_search(start, end, result, expression, flags)) {
441 start = result[0].second;
443 for (
size_t j = 0; j < result.size(); j++) {
444 resultStrings.push_back(boost::lexical_cast<string>(result[j]));
448 flags |= boost::match_prev_avail;
449 flags |= boost::match_not_bob;
452 return resultStrings;
456 bool includeSubDirs) {
458 if (!boost::filesystem::exists(curr_directory)) {
461 boost::filesystem::directory_iterator iterator(curr_directory);
463 for (; iterator != boost::filesystem::directory_iterator(); ++iterator) {
464 boost::filesystem::path thisPath = iterator->path();
465 if (boost::filesystem::is_directory(thisPath)) {
466 list.push_back(thisPath.string());
469 if (includeSubDirs) {
474 sort(list.begin(), list.end());
496 double* homogenousMatrix =
new double[16];
497 double* finalMatrix =
new double[16];
498 matrix.GetHomogenousMatrix(homogenousMatrix);
537 finalMatrix[0] = homogenousMatrix[0];
538 finalMatrix[1] = homogenousMatrix[1];
539 finalMatrix[2] = homogenousMatrix[2];
540 finalMatrix[3] = homogenousMatrix[12];
541 finalMatrix[4] = homogenousMatrix[4];
542 finalMatrix[5] = homogenousMatrix[5];
543 finalMatrix[6] = homogenousMatrix[6];
544 finalMatrix[7] = homogenousMatrix[13];
545 finalMatrix[8] = homogenousMatrix[8];
546 finalMatrix[9] = homogenousMatrix[9];
547 finalMatrix[10] = homogenousMatrix[10];
548 finalMatrix[11] = homogenousMatrix[14];
549 finalMatrix[12] = homogenousMatrix[3];
550 finalMatrix[13] = homogenousMatrix[7];
551 finalMatrix[14] = homogenousMatrix[11];
552 finalMatrix[15] = 1.0;
555 Matrix result(4, 4, finalMatrix);
556 delete homogenousMatrix;
564 vector<string>
files;
569 if (files.size() == 0) {
574 bool tempfilefound =
false;
579 tempfilefound =
false;
581 for (
size_t j = 0; j < files.size(); j++) {
582 if (files[j].find(matrixName) != string::npos) {
583 tempfilefound =
true;
589 if (!tempfilefound) {
590 return tempfilefound;
598 vector<string>
files;
603 if (files.size() == 0) {
607 string matrixName1, matrixName2;
608 bool tempfilefound =
false;
614 tempfilefound =
false;
618 for (
size_t j = 0; j < files.size(); j++) {
619 if (files[j].find(matrixName1) != string::npos) {
621 tempfilefound =
true;
627 if (!tempfilefound) {
628 return tempfilefound;
636 filename <<
"/pairs-" << overlapId;
637 incentimeters ? filename <<
"-cm.bin" : filename <<
"-px.bin";
638 return filename.str();
642 stringstream matrixName;
644 incentimeters ? matrixName << overlapId <<
"cm.mat" : matrixName << overlapId <<
"px.mat";
645 return matrixName.str();
652 it->second.setMaximumNumberOfHitPairs(maxPairs);
657 cout <<
"warning. max pairs must be larger than 0!\n";
664 cout <<
"\x1B[2J\x1B[H";
669 bool pairAdded =
false;
681 pairAdded =
aligners[thisID].addSimplePair(pair);
685 verbosePrint(
"pair is not sane. processing failed.\n", 1);
704 verbosePrint(
"all aligners are already full. no further files will be read.\n", 1);
713 cout <<
"starting remaining aligners.\n";
726 cout << notStarted <<
" aligners remained.\n";
728 cout <<
"waiting for all aligners to finish...";
737 for (
auto &
id : overlapIDs) {
744 cout <<
"ERROR: could not write matrix " << matrixFilename <<
"\n";
747 _info <<
"aligner " <<
id <<
":\n";
748 _info <<
"no of pairs: " <<
aligners[id].getNoOfPairs() <<
"\n";
752 cout <<
"Error: aligner for " <<
id <<
" failed.\n";
767 cout <<
"all aligners done.\n";
773 cout <<
"error: binary pair file directory not set.\n";
774 cout <<
"use PndLmdAlignManager::setBinaryPairFileDirectory()\n";
782 cout <<
"writing all pairs to binary files\n";
800 cout <<
"error: binary pair file directory not set.\n";
801 cout <<
"use PndLmdAlignManager::setBinaryPairFileDirectory()\n";
809 cout <<
"reading all pairs from binary files and starting aligners...\n";
854 if (!boost::filesystem::exists(filename)) {
855 cerr <<
"PndLmdAlignManager::readConfig: ERROR! File " << filename <<
" not found!\n";
858 std::ifstream
is(filename);
859 boost::property_tree::ptree root;
861 boost::property_tree::read_json(is, root);
863 catch (std::exception &e) {
864 cerr <<
"PndLmdAlignManager::readConfig: ERROR! Can't parse json file " << filename <<
".\n";
870 bool replaceExisting) {
872 if (boost::filesystem::exists(filename) && !replaceExisting) {
873 cerr <<
"PndLmdAlignManager::writeConfig: Config already exists, will not be replaced.\n";
877 boost::filesystem::path outPath(filename);
878 if (!boost::filesystem::exists(outPath.parent_path())) {
879 boost::filesystem::create_directories(outPath.parent_path());
882 std::ofstream os(filename);
883 boost::property_tree::write_json(os, configTree);
889 it->second.clearPairs();
std::vector< std::string > fileNames
void runSensorAligner(PndLmdSensorAligner &aligner)
bool addFile(std::string filename)
printf("RealTime=%f seconds, CpuTime=%f seconds\n", rtime, ctime)
Matrix transformToSensorOne()
std::vector< int > getAvailableOverlapIDs()
void setInCentimeters(bool inCentimeters)
std::string binaryPairFileDirectory
bool writePairsToBinary(const std::string directory)
void waitForCompletion(PndLmdThreadPool &threadPool)
bool checkForLmdMatrixFiles()
static const UInt_t success
static TGeoHMatrix readTGeoHMatrix(std::string filename)
std::map< int, bool > alignersFull
static int searchDirectories(std::string curr_directory, std::vector< std::string > &list, bool includeSubDirs=true)
static PndLmdGeometryHelper & getInstance()
static int searchFiles(std::string curr_directory, std::vector< std::string > &list, std::string extension="", bool includeSubDirs=true)
bool addPairAndStartAligner(PndLmdHitPair &pair, PndLmdThreadPool &threadPool)
std::map< int, PndLmdSensorAligner > aligners
std::map< int, TString > files
void setOverlapId(Int_t overlapId)
static bool mkdir(std::string path)
void applyDynamicCut(double percent=5.0)
static Matrix castTGeoHMatrixToMatrix(const TGeoHMatrix &matrix)
bool readPairsFromBinaryFilesAndAlign()
static std::string makeBinaryPairFileName(int overlapId=0, bool incentimeters=true)
PndLmdGeometryHelper * helper
Int_t getOverlapId() const
static std::vector< std::string > findRegex(std::string source, std::string regex)
void setZasTimestamp(bool timestamp)
bool checkForBinaryFiles()
void setInCentimeters(bool value)
static void clearScreen()
void loadBar(int current, int total, int resolution, int width, std::string message="")
std::map< int, PndLmdSensorAligner >::iterator mapIt
void setZasTimetamp(bool value)
static bool writeMatrix(Matrix &mat, std::string filename)
static boost::property_tree::ptree readConfigFile(std::string filename)
void setManager(PndLmdAlignManager *manager)
static std::string makeMatrixFileName(int overlapId=0, bool incentimeters=true)
static bool exists(std::string file)
void verbosePrint(std::string input, int level=3)
static bool writeConfigFile(boost::property_tree::ptree configTree, std::string filename, bool replaceExisting=true)
bool writePairsToBinaryFiles()
virtual ~PndLmdAlignManager()
static Matrix readMatrix(std::string filename)
int addFilesFromDirectory(std::string directory, int maxFiles=0)
void setMaxPairs(int maxPairs)
static std::vector< std::vector< double > > readFromCSVFile(std::string filename)
bool readPairsFromBinary(const std::string directory)