10 #include <boost/asio.hpp>
11 #include <boost/asio/io_service.hpp>
12 #include <boost/bind.hpp>
13 #include <boost/lexical_cast.hpp>
14 #include <boost/filesystem.hpp>
15 #include <boost/ref.hpp>
16 #include <boost/regex.hpp>
17 #include <boost/shared_ptr.hpp>
18 #include <boost/thread.hpp>
19 #include <boost/thread/mutex.hpp>
32 #include <TClonesArray.h>
34 #include <TGeoMatrix.h>
42 using std::stringstream;
77 float ratio =
_i / (float)
_n;
81 printf(
"%3d%% [", (
int) (ratio * 100));
84 for (
int x = 0;
x <
c;
x++)
87 for (
int x = c;
x <
_w;
x++)
102 _info <<
"info for aligned areas\n";
138 std::cout <<
"PndLmdAlignManager::Init(): Initialization successful." <<
"\n";
182 cout <<
"pair is not sane. processing failed.\n";
200 cout <<
"all aligners are already full. no further files will be read.\n";
207 std::cout <<
"using " <<
aligners.size() <<
" aligners, which have:\n";
209 cout <<
"id: " << it->second.getModuleID() <<
" has " << it->second.getNoOfPairs() <<
" pairs."
231 std::vector<string> list;
233 for (
size_t i = 0;
i < list.size();
i++) {
235 if ((
int) i == maxFiles - 1) {
240 cout <<
"looking for files in " << directory <<
". choose " <<
fileNames.size()
241 <<
" files of maximum of " << maxFiles <<
".\n";
305 cout <<
"found " << noOfFiles <<
" file(s). reading...\n";
308 cout <<
"no files found. exiting.\n";
312 TChain* chainPairs =
new TChain(
"pndsim");
315 if (
fileNames[
i].find(
"Lumi_Pairs") != std::string::npos) {
321 TClonesArray* hitPairs =
new TClonesArray(
"PndLmdHitPair");
322 chainPairs->SetBranchAddress(
"PndLmdHitPair", &hitPairs);
323 int nEntries = chainPairs->GetEntries();
324 cout <<
"HitPairs no of entries: " << nEntries <<
"\n";
326 cout <<
"Sorting Pairs to Manager...\n";
328 for (
int i_event = 0; i_event < nEntries; i_event++) {
330 loadBar(i_event, nEntries, 1000, 60);
331 chainPairs->GetEntry(i_event);
332 int nPairs = hitPairs->GetEntries();
335 for (
int i_Pair = 0; i_Pair < nPairs; i_Pair++) {
345 cout <<
"================================ \n";
346 cout <<
"total Pairs: " << totalPairs <<
"\n";
347 cout <<
"All done. Running Align Manager. \n";
348 cout <<
"================================ \n";
372 void WorkerThread(boost::shared_ptr<boost::asio::io_service> io_service) {
386 it->second.calculateMatrix();
387 if (it->second.successful()) {
389 Matrix result = it->second.getResultMatrix();
394 _info <<
"aligner " << it->second.getOverlapId() <<
":\n";
395 _info <<
"no of pairs: " << it->second.getNoOfPairs() <<
"\n";
400 cout <<
"Error: aligner for " << it->second.getOverlapId() <<
" failed.\n";
410 boost::shared_ptr<boost::asio::io_service> io_service(
new boost::asio::io_service);
411 boost::shared_ptr<boost::asio::io_service::work> work(
new boost::asio::io_service::work(*io_service));
414 boost::thread_group worker_threads;
418 nThreads = boost::thread::hardware_concurrency();
422 cout <<
"INFO:: could not detect number of cores. assuming 4.\n";
428 for (
int i = 0;
i < nThreads;
i++) {
429 worker_threads.create_thread(boost::bind(&
WorkerThread, io_service));
443 if (
_verboseLevel >= 3) cout <<
"posting work for aligner " << it->second.getOverlapId() <<
"... ";
450 if (
_verboseLevel >= 3) cout <<
"waiting for all threads to complete.\n";
452 worker_threads.join_all();
453 if (
_verboseLevel >= 3) cout <<
"all threads done. getting info.\n";
455 if (it->second.successful()) {
456 if (
_verboseLevel >= 3) cout <<
"gtting matrix from " << it->second.getOverlapId() <<
"\n";
457 Matrix result = it->second.getResultMatrix();
462 cout <<
"ERROR: could not write matrix " << matrixFilename <<
"\n";
465 _info <<
"aligner " << it->second.getOverlapId() <<
":\n";
466 _info <<
"no of pairs: " << it->second.getNoOfPairs() <<
"\n";
472 cout <<
"Error: aligner for " << it->second.getOverlapId() <<
" failed.\n";
481 cerr <<
"WARNING! You can't use rotation correction in pixel ICP mode!\n";
485 cout <<
"starting aligners...\n";
505 cout <<
"all aligners done.\n";
579 if (i % (n / r) != 0) {
586 float ratio = i / (float) n;
590 printf(
"%3d%% [", (
int) (ratio * 100));
593 for (
int x = 0;
x <
c;
x++)
596 for (
int x = c;
x < w;
x++)
601 printf(
"] %s %d of %d \n\033[F\033[J", message.c_str(),
i,
n);
610 for (
size_t iFile = 0; iFile <
fileNames.size(); iFile++) {
611 if (!boost::filesystem::exists(
fileNames[iFile])) {
612 cout <<
"error opening file:";
620 if (!boost::filesystem::exists(outPath.parent_path())) {
621 boost::filesystem::create_directories(outPath.parent_path());
637 stringstream *valueStream =
new stringstream();
640 ifs.open(filename.c_str());
641 if (!ifs.is_open()) {
642 cout <<
"could not read ";
644 cout <<
"! aborting." <<
"\n";
648 while (std::getline(ifs, linebuffer)) {
649 *valueStream << linebuffer <<
"\n";
661 if (temp.size() < 1) {
662 cout <<
"warning! can't read matrix from file " << filename <<
"\n";
665 if (temp[0].size() < 1) {
666 cout <<
"warning! can't read matrix from file " << filename <<
"\n";
672 columns = temp[0].size();
673 Matrix result(rows, columns);
675 for (
int i = 0;
i < rows;
i++) {
676 for (
int j = 0; j < columns; j++) {
677 result.
val[
i][j] = temp[
i][j];
687 if (!(filename ==
"")) {
688 boost::filesystem::path outPath(filename);
689 if (!boost::filesystem::exists(outPath.parent_path())) {
690 boost::filesystem::create_directories(outPath.parent_path());
695 std::ofstream outFileStream;
696 outFileStream.open(filename.c_str());
698 if (outFileStream.fail()) {
702 outFileStream << std::setprecision(16);
703 outFileStream << mat;
704 outFileStream.close();
714 std::vector<std::vector<double> > data;
715 while (getline(*ss, line)) {
717 std::vector<double> tempvec;
718 while (getline(iss, token,
',')) {
719 tempvec.push_back(boost::lexical_cast<double>(token));
721 data.push_back(tempvec);
732 bool includeSubDirs) {
734 if (!boost::filesystem::exists(path)) {
737 boost::filesystem::directory_iterator iterator(path);
738 for (; iterator != boost::filesystem::directory_iterator(); ++iterator) {
739 if (boost::filesystem::is_directory(iterator->path()) && includeSubDirs) {
740 list.push_back(iterator->path().string());
741 searchFiles(iterator->path().string(), list, detail, includeSubDirs);
743 else if (boost::filesystem::is_regular_file(iterator->path())) {
744 if (iterator->path().string().find(detail) != std::string::npos) {
745 list.push_back(iterator->path().string());
749 sort(list.begin(), list.end());
754 boost::filesystem::path bpath(path);
755 if (boost::filesystem::exists(bpath)) {
758 return boost::filesystem::create_directories(bpath);
762 boost::filesystem::path bpath(path);
763 if (boost::filesystem::exists(bpath)) {
772 boost::regex expression(regex);
773 std::string::const_iterator start, end;
774 start = source.begin();
776 boost::match_results<std::string::const_iterator> result;
777 boost::match_flag_type flags = boost::match_default;
779 vector<string> resultStrings;
781 while (regex_search(start, end, result, expression, flags)) {
783 start = result[0].second;
785 for (
size_t j = 0; j < result.size(); j++) {
786 resultStrings.push_back(boost::lexical_cast<string>(result[j]));
790 flags |= boost::match_prev_avail;
791 flags |= boost::match_not_bob;
794 return resultStrings;
798 bool includeSubDirs) {
800 if (!boost::filesystem::exists(curr_directory)) {
803 boost::filesystem::directory_iterator iterator(curr_directory);
804 for (; iterator != boost::filesystem::directory_iterator(); ++iterator) {
805 if (boost::filesystem::is_directory(iterator->path())) {
806 list.push_back(iterator->path().string());
809 if (includeSubDirs) {
814 sort(list.begin(), list.end());
836 double* homogenousMatrix =
new double[16];
837 double* finalMatrix =
new double[16];
838 matrix.GetHomogenousMatrix(homogenousMatrix);
877 finalMatrix[0] = homogenousMatrix[0];
878 finalMatrix[1] = homogenousMatrix[1];
879 finalMatrix[2] = homogenousMatrix[2];
880 finalMatrix[3] = homogenousMatrix[12];
881 finalMatrix[4] = homogenousMatrix[4];
882 finalMatrix[5] = homogenousMatrix[5];
883 finalMatrix[6] = homogenousMatrix[6];
884 finalMatrix[7] = homogenousMatrix[13];
885 finalMatrix[8] = homogenousMatrix[8];
886 finalMatrix[9] = homogenousMatrix[9];
887 finalMatrix[10] = homogenousMatrix[10];
888 finalMatrix[11] = homogenousMatrix[14];
889 finalMatrix[12] = homogenousMatrix[3];
890 finalMatrix[13] = homogenousMatrix[7];
891 finalMatrix[14] = homogenousMatrix[11];
893 finalMatrix[15] = 1.0;
896 Matrix result(4, 4, finalMatrix);
897 delete homogenousMatrix;
905 vector<string>
files;
910 if (files.size() == 0) {
915 bool tempfilefound =
false;
920 tempfilefound =
false;
922 for (
size_t j = 0; j < files.size(); j++) {
923 if (files[j].find(matrixName) != string::npos) {
924 tempfilefound =
true;
930 if (!tempfilefound) {
931 return tempfilefound;
939 vector<string>
files;
944 if (files.size() == 0) {
948 string matrixName1, matrixName2;
949 bool tempfilefound =
false;
955 tempfilefound =
false;
959 for (
size_t j = 0; j < files.size(); j++) {
960 if (files[j].find(matrixName1) != string::npos) {
963 tempfilefound =
true;
969 if (!tempfilefound) {
970 return tempfilefound;
979 filename <<
"/pairs-";
980 filename << overlapId;
988 return filename.str();
997 stringstream matrixName;
1000 matrixName << overlapId <<
"cm.mat";
1003 matrixName << overlapId <<
"px.mat";
1005 return matrixName.str();
1385 it->second.setMaximumNumberOfHitPairs(maxPairs);
1390 cout <<
"warning. max pairs must be larger than 0!\n";
1397 cout <<
"\x1B[2J\x1B[H";
1405 cout <<
"starting remaining aligners.\n";
1407 if (!(it->second)) {
1416 cout << notStarted <<
" aligners remained.\n";
1418 cout <<
"waiting for all aligners to finish...";
1425 if (it->second.successful()) {
1427 Matrix result = it->second.getResultMatrix();
1432 cout <<
"ERROR: could not write matrix " << matrixFilename <<
"\n";
1435 _info <<
"aligner " << it->second.getOverlapId() <<
":\n";
1436 _info <<
"no of pairs: " << it->second.getNoOfPairs() <<
"\n";
1440 cout <<
"Error: aligner for " << it->second.getOverlapId() <<
" failed.\n";
1455 cout <<
"all aligners done.\n";
1484 cout <<
"error: binary pair file directory not set.\n";
1485 cout <<
"use PndLmdAlignManager::setBinaryPairFileDirectory()\n";
1493 cout <<
"writing all pairs to binary files\n";
1498 loadBar(cur++, tot, 1000, 60);
1511 cout <<
"error: binary pair file directory not set.\n";
1512 cout <<
"use PndLmdAlignManager::setBinaryPairFileDirectory()\n";
1520 cout <<
"reading all pairs from binary files\n";
1523 loadBar(cur++, tot, 1000, 60);
1543 if (!boost::filesystem::exists(filename)) {
1544 cerr <<
"PndLmdAlignManager::readConfig: ERROR! File " << filename <<
" not found!\n";
1547 std::ifstream
is(filename);
1548 boost::property_tree::ptree root;
1550 boost::property_tree::read_json(is, root);
1552 catch (std::exception &e) {
1553 cerr <<
"PndLmdAlignManager::readConfig: ERROR! Can't parse json file " << filename <<
".\n";
1559 bool replaceExisting) {
1563 if (boost::filesystem::exists(filename) && !replaceExisting) {
1564 cerr <<
"PndLmdAlignManager::writeConfig: Config already exists, will not be replaced.\n";
1568 boost::filesystem::path outPath(filename);
1569 if (!boost::filesystem::exists(outPath.parent_path())) {
1570 boost::filesystem::create_directories(outPath.parent_path());
1573 std::ofstream os(filename);
1574 boost::property_tree::write_json(os, configTree);
1580 it->second.clearPairs();
std::vector< std::string > fileNames
boost::thread_group alignerThreadGroup
bool addFile(std::string filename)
printf("RealTime=%f seconds, CpuTime=%f seconds\n", rtime, ctime)
void setInCentimeters(bool inCentimeters)
static std::vector< int > getAvailableOverlapIDs()
void setInCentimeters(bool inCentimeters)
bool writePairsToBinary(const std::string directory)
bool checkForLmdMatrixFiles()
static const UInt_t success
std::map< int, bool > alignersFull
static int searchDirectories(std::string curr_directory, std::vector< std::string > &list, bool includeSubDirs=true)
static int searchFiles(std::string curr_directory, std::vector< std::string > &list, std::string extension="", bool includeSubDirs=true)
std::map< int, PndLmdSensorAligner > aligners
std::map< int, TString > files
void setOverlapId(Int_t overlapId)
static bool mkdir(std::string path)
static Matrix castTGeoHMatrixToMatrix(const TGeoHMatrix &matrix)
static std::string makeBinaryPairFileName(int overlapId=0, bool incentimeters=true)
Int_t getOverlapId() const
static std::vector< std::string > findRegex(std::string source, std::string regex)
void WorkerThread(boost::shared_ptr< boost::asio::io_service > io_service)
void setZasTimestamp(bool timestamp)
std::string _matrixOutDir
bool checkForBinaryFiles()
void resetMTLB(int n, int r, int w)
static std::stringstream * readFile(std::string filename)
static void clearScreen()
static void loadBar(int current, int total, int resolution, int width, std::string message="")
std::string _binaryPairFileDirectory
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)
bool readPairsFromBinaryFiles()
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()
boost::mutex incrementMutex
virtual ~PndLmdAlignManager()
static Matrix readMatrix(std::string filename)
int addFilesFromDirectory(std::string directory, int maxFiles=0)
void setMaxPairs(int maxPairs)
bool addPairAndStartAligner(PndLmdHitPair &pair)
static std::vector< std::vector< double > > readFromCSVFile(std::string filename)
void alignOne(PndLmdSensorAligner &aligner)