FairRoot/PandaRoot
PndLmdAlignManager.cxx
Go to the documentation of this file.
1 /*
2  * PndLmdAlignManager.cpp
3  *
4  * Created on: May 26, 2015
5  * Author: Roman Klasen, roklasen@uni-mainz.de or klasen@kph.uni-mainz.de
6  */
7 
8 #include <PndLmdAlignManager.h>
9 
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>
15 
16 #include <functional>
17 #include <fstream>
18 #include <iostream>
19 #include <iomanip>
20 #include <string>
21 #include <vector>
22 #include <stdexcept>
23 
24 #include "PndLmdSensorAligner.h"
25 #include "PndLmdHitPair.h"
26 
27 #include <TChain.h>
28 #include <TClonesArray.h>
29 #include <TFile.h>
30 #include <TGeoMatrix.h>
31 
32 using std::cerr;
33 using std::cout;
34 using std::ifstream;
35 using std::map;
36 using std::ofstream;
37 using std::string;
38 using std::stringstream;
39 using std::vector;
40 
42  init();
43 }
44 
46 
47  _info << "info for aligned areas\n";
48 
49  _zIsTimestamp = true;
50  _allFilesAdded = false;
51  _singleAligner = true;
52  _inCentimeters = false;
53  enableHelperMatrix = false;
54  _multithreaded = false;
55  _verboseLevel = 0;
56 
57  //int overlapId=-1;
58  fileNames.clear();
59  aligners.clear();
60 
63  for (size_t i = 0; i < overlapIDs.size(); i++) {
64  int overlapId = overlapIDs[i];
65 
66  PndLmdSensorAligner tempAligner;
67  tempAligner.setManager(this);
68  tempAligner.setOverlapId(overlapId);
69  tempAligner.setZasTimetamp(_zIsTimestamp);
70  tempAligner.setInCentimeters(_inCentimeters);
71  aligners[overlapId] = tempAligner;
72  }
73 
74  // we just started, all aligners are empty
75  for (size_t i = 0; i < overlapIDs.size(); i++) {
76  alignersFull[overlapIDs[i]] = false;
77  }
78 
79  outFilename = "";
80  _firstInitDone = true;
81  std::cout << "PndLmdAlignManager::Init(): Initialization successful." << "\n";
82 }
83 
85 }
86 
88  std::cout << "using " << aligners.size() << " aligners, which have:\n";
89  for (mapIt it = aligners.begin(); it != aligners.end(); it++) {
90  cout << "id: " << it->second.getModuleID() << " has " << it->second.getNoOfPairs() << " pairs."
91  << "\n";
92  }
93 }
94 
96 
97  if (_allFilesAdded) {
98  return false;
99  }
100  else {
101  fileNames.push_back(filename);
102  return true;
103  }
104 }
105 
106 int PndLmdAlignManager::addFilesFromDirectory(std::string directory, int maxFiles) {
107 
108  if (_allFilesAdded) {
109  return fileNames.size();
110  }
111  else {
112  std::vector<string> list;
113  searchFiles(directory, list, ".root", false);
114  for (size_t i = 0; i < list.size(); i++) {
115  fileNames.push_back(list[i]);
116  if ((int) i == maxFiles - 1) { //we use == instead of >= so that maxFiles=0 always chooses all files
117  break;
118  }
119  }
120  _allFilesAdded = true;
121  cout << "looking for files in " << directory << ". choose " << fileNames.size()
122  << " files of maximum of " << maxFiles << ".\n";
123  return fileNames.size();
124  }
125 
126 }
127 
129 
130  _allFilesAdded = true;
131 
132  PndLmdThreadPool threadPool;
133 
134  int noOfFiles = fileNames.size();
135  if (noOfFiles > 0) {
136  cout << "found " << noOfFiles << " file(s). reading...\n";
137  }
138  else {
139  cout << "no files found. exiting.\n";
140  exit(0);
141  }
142 
143  TChain* chainPairs = new TChain("pndsim");
144  for (size_t i = 0; i < fileNames.size(); i++) {
145  //cout << files[i] << endl;
146  if (fileNames[i].find("Lumi_Pairs") != std::string::npos) {
147  chainPairs->Add(fileNames[i].c_str());
148  }
149  }
150 
151  //pairs of sensors in LMD coordinates
152  TClonesArray* hitPairs = new TClonesArray("PndLmdHitPair");
153  chainPairs->SetBranchAddress("PndLmdHitPair", &hitPairs);
154  int nEntries = chainPairs->GetEntries();
155  cout << "HitPairs no of entries: " << nEntries << "\n";
156 
157  cout << "Sorting Pairs to Manager...\n";
158  int totalPairs = 0;
159  for (int i_event = 0; i_event < nEntries; i_event++) {
160 
161  loadBar(i_event, nEntries, 1000, 60);
162  chainPairs->GetEntry(i_event);
163  int nPairs = hitPairs->GetEntries();
164 
165  //loop over hitPairs per Event
166  for (int i_Pair = 0; i_Pair < nPairs; i_Pair++) {
167  PndLmdHitPair* currentPair = (PndLmdHitPair*) hitPairs->At(i_Pair);
168  addPairAndStartAligner(*currentPair, threadPool);
169  totalPairs++;
170  }
171 
172  //should this be done at the end or the start of the for loop?
173  if (allAlignersFull()) {
174  break;
175  }
176  }
177 
178  //save matrices to disk
179  waitForCompletion(threadPool);
180 
181  cout << "================================ \n";
182  cout << "total Pairs: " << totalPairs << "\n";
183  cout << "All done. Running Align Manager. \n";
184  cout << "================================ \n";
185 
186  delete chainPairs;
187  delete hitPairs;
188 }
189 
190 //run a single SensorAligner, is thread safe
192 
193  //perform first checks
194  if (!aligner.check()) {
195  return;
196  }
197 
198  //write binary file only if not already present
199  if (!checkForBinaryFiles()) {
201  }
202 
203  // this must be done before dynamic cut!
204  aligner.transformToSensorOne();
205 
206  // apply dynamic cut. this changes the amount of pairs the aligner has,
207  // so don't re-save the pairs after that!
208  aligner.applyDynamicCut();
209 
210  //calculate the matrix
211  aligner.calculateMatrix();
212 
213  //free memory
214  aligner.clearPairs();
215 }
216 
217 void PndLmdAlignManager::loadBar(int i, int n, int r, int w, std::string message) {
218 
219  std::lock_guard<std::mutex> lock(MTLBmutex);
220 
221  // Only update r times.
222  if (n == 0) return;
223  if (n == 1) return;
224 
225  if (r > n) {
226  r = n;
227  }
228 
229  //calculate ev / sec every 100000 iterations
230  if (i % (n / r) != 0) {
231  return;
232  }
233 
234  flush(cout);
235 
236  // Calculate the ratio of complete-to-incomplete.
237  float ratio = i / (float) n;
238  int c = ratio * w;
239 
240  // Show the percentage complete.
241  printf("%3d%% [", (int) (ratio * 100));
242 
243  // Show the load bar.
244  for (int x = 0; x < c; x++)
245  printf("=");
246 
247  for (int x = c; x < w; x++)
248  printf(" ");
249 
250  // ANSI Control codes to go back to the
251  // previous line and clear it.
252  printf("] %s %d of %d \n\033[F\033[J", message.c_str(), i, n);
253 
254 }
255 
257 
258  if (fileNames.size() == 0) {
259  //cout << "no pair files specified, are we using binary data?\n";
260  }
261  for (size_t iFile = 0; iFile < fileNames.size(); iFile++) {
262  if (!boost::filesystem::exists(fileNames[iFile])) {
263  cout << "error opening file:";
264  cout << fileNames[iFile] << "\n";
265  exit(1);
266  }
267  }
268 
269  if (!(outFilename == "")) {
270  boost::filesystem::path outPath(outFilename);
271  if (!boost::filesystem::exists(outPath.parent_path())) {
272  boost::filesystem::create_directories(outPath.parent_path());
273  }
274  }
275 
276  if (!(matrixOutDir == "")) {
277  if (!boost::filesystem::exists(matrixOutDir)) {
278  boost::filesystem::create_directories(matrixOutDir);
279  }
280  }
281 }
282 
284  // avoid code duplication
285  return castTGeoHMatrixToMatrix(readTGeoHMatrix(filename));
286 }
287 
289 
290  vector<vector<double> > temp = readFromCSVFile(filename);
291 
292  if (temp.size() < 1) {
293  cout << "warning! can't read matrix from file " << filename << "\n";
294  return TGeoHMatrix();
295  }
296  if (temp[0].size() < 1) {
297  cout << "warning! can't read matrix from file " << filename << "\n";
298  return TGeoHMatrix();
299  }
300 
301  int rows, columns;
302  rows = temp.size();
303  columns = temp[0].size();
304  Matrix resultMat(rows, columns);
305 
306  double rotation[9];
307  double translation[3];
308  TGeoHMatrix result;
309 
310  for (int i = 0; i < rows; i++) {
311  for (int j = 0; j < columns; j++) {
312  resultMat.val[i][j] = temp[i][j];
313  }
314  }
315 
316  // have we even read a real matrix?
317  if (resultMat.m != 4 || resultMat.n != 4) {
318  cerr << "Error! Invalid matrix!\n";
319  return TGeoHMatrix();
320  }
321 
322  // fuck it, we do it by hand
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];
332 
333  translation[0] = resultMat.val[0][3];
334  translation[1] = resultMat.val[1][3];
335  translation[2] = resultMat.val[2][3];
336 
337  result.SetRotation(rotation);
338  result.SetTranslation(translation);
339 
340  return result;
341 }
342 
344 
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());
349  }
350  }
351 
352  //cout << "writing matrix " << filename << "\n";
353  std::ofstream outFileStream;
354  outFileStream.open(filename.c_str());
355 
356  if (outFileStream.fail()) {
357  return false;
358  }
359 
360  outFileStream << std::setprecision(16);
361  outFileStream << mat;
362  outFileStream.close();
363  return true;
364 }
365 
366 vector<vector<double> > PndLmdAlignManager::readFromCSVFile(std::string filename) {
367 
368  std::vector<std::vector<double> > data;
369  ifstream ifs;
370  ifs.open(filename.c_str());
371  if (ifs.is_open()) {
372  stringstream iss;
373  string line, token;
374 
375  while (getline(ifs, line)) {
376  iss << line;
377  std::vector<double> tempvec;
378  while (getline(iss, token, ',')) {
379  tempvec.push_back(boost::lexical_cast<double>(token));
380  }
381  data.push_back(tempvec);
382  iss.clear();
383  }
384  ifs.close();
385  }
386  else {
387  throw std::runtime_error("Unable to open file " + filename);
388  }
389 
390  return data;
391 }
392 
393 int PndLmdAlignManager::searchFiles(std::string path, std::vector<std::string> &list, std::string detail,
394  bool includeSubDirs) {
395 
396  if (!boost::filesystem::exists(path)) {
397  return 0;
398  }
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);
405  }
406  else if (boost::filesystem::is_regular_file(thisPath)) {
407  if (thisPath.string().find(detail) != std::string::npos) {
408  list.push_back(thisPath.string());
409  }
410  }
411  }
412  sort(list.begin(), list.end());
413  return list.size();
414 }
415 
416 bool PndLmdAlignManager::mkdir(std::string path) {
417  boost::filesystem::path bpath(path);
418  if (boost::filesystem::exists(bpath)) {
419  return true;
420  }
421  return boost::filesystem::create_directories(bpath);
422 }
423 
424 bool PndLmdAlignManager::exists(std::string path) {
425  return boost::filesystem::exists(boost::filesystem::path(path));
426 }
427 
428 vector<string> PndLmdAlignManager::findRegex(std::string source, std::string regex) {
429 
430  boost::regex expression(regex);
431  std::string::const_iterator start, end;
432  start = source.begin();
433  end = source.end();
434  boost::match_results<std::string::const_iterator> result;
435  boost::match_flag_type flags = boost::match_default;
436 
437  vector<string> resultStrings;
438 
439  while (regex_search(start, end, result, expression, flags)) {
440  // update search position:
441  start = result[0].second;
442 
443  for (size_t j = 0; j < result.size(); j++) {
444  resultStrings.push_back(boost::lexical_cast<string>(result[j]));
445  }
446 
447  // update flags:
448  flags |= boost::match_prev_avail;
449  flags |= boost::match_not_bob;
450  }
451 
452  return resultStrings;
453 }
454 
455 int PndLmdAlignManager::searchDirectories(std::string curr_directory, std::vector<std::string> &list,
456  bool includeSubDirs) {
457 
458  if (!boost::filesystem::exists(curr_directory)) {
459  return 0;
460  }
461  boost::filesystem::directory_iterator iterator(curr_directory);
462 
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());
467 
468  //recursively call for sub directories
469  if (includeSubDirs) {
470  searchDirectories(thisPath.string(), list, includeSubDirs);
471  }
472  }
473  }
474  sort(list.begin(), list.end());
475  return list.size();
476 
477 }
478 
479 void PndLmdAlignManager::setInCentimeters(bool inCentimeters) {
480  _inCentimeters = inCentimeters;
481  for (mapIt it = aligners.begin(); it != aligners.end(); it++) {
482  it->second.setInCentimeters(_inCentimeters);
483  }
484 }
485 
487  _zIsTimestamp = timestamp;
488  for (mapIt it = aligners.begin(); it != aligners.end(); it++) {
489  it->second.setZasTimetamp(_zIsTimestamp);
490  }
491 }
492 
494 
495  //allocate memory for matrix elements
496  double* homogenousMatrix = new double[16];
497  double* finalMatrix = new double[16];
498  matrix.GetHomogenousMatrix(homogenousMatrix);
499 
500  /*
501  * this code was tested on 2016-08-29 and works. The root implementation is still not
502  * according to ROOT documentation, in that the translation parts of a matrix are not
503  * where they should be and sometimes the homogenous coordinate is not set to 1.
504  * This fixes that.
505  *
506  * DO NOT REMOVE THESE COMMENTS!
507  *
508  * When in doubt, refer to the
509  * following code example, that shows where the translations (wrongfully) is:
510  */
511  /*
512 
513  //test tgeohmatrices
514  TGeoHMatrix *h1 = new TGeoHMatrix(TGeoTranslation(1,2,3));
515  TGeoHMatrix *h2 = new TGeoHMatrix();
516  TGeoHMatrix *h3 = new TGeoHMatrix();
517  TGeoHMatrix *h4 = new TGeoHMatrix();
518 
519  h2->SetDx(1.); h2->SetDy(2.); h2->SetDz(3.);
520  double tr[3] = {1.,2.,3.};
521  h3->SetTranslation(tr);
522  const double rot[9] = {0,1,0,-1,0,0,0,0,0};
523  h4->SetRotation(rot);
524 
525  Matrix m1 = castTGeoHMatrixToMatrix(*h1);
526  Matrix m2 = castTGeoHMatrixToMatrix(*h2);
527  Matrix m3 = castTGeoHMatrixToMatrix(*h3);
528  Matrix m4 = castTGeoHMatrixToMatrix(*h4);
529 
530  cout << "m1:\n" << m1 << "\n";
531  cout << "m2:\n" << m2 << "\n";
532  cout << "m3:\n" << m3 << "\n";
533  cout << "m4:\n" << m4 << "\n";
534  */
535 
536  //copy values from the wrong to the correct positions
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;
553 
554  //create matrix and clean up
555  Matrix result(4, 4, finalMatrix);
556  delete homogenousMatrix;
557  delete finalMatrix;
558  return result;
559 
560 }
561 
563 
564  vector<string> files;
565  searchFiles(binaryPairFileDirectory, files, "bin", false);
566  int foundFiles = 0;
567 
568  //no binary files at all!
569  if (files.size() == 0) {
570  return false;
571  }
572 
573  string matrixName;
574  bool tempfilefound = false;
575 
576  //check for every ID that should be there if there is a corresponding file
577  for (size_t i = 0; i < overlapIDs.size(); i++) {
578 
579  tempfilefound = false;
581  for (size_t j = 0; j < files.size(); j++) {
582  if (files[j].find(matrixName) != string::npos) {
583  tempfilefound = true;
584  foundFiles++;
585  }
586  }
587 
588  //file not found? then at least one is missing, return false
589  if (!tempfilefound) {
590  return tempfilefound;
591  }
592  }
593  return true;
594 }
595 
597 
598  vector<string> files;
599  searchFiles(binaryPairFileDirectory, files, "mat", false);
600  int foundFiles = 0;
601 
602  //no binary files at all!
603  if (files.size() == 0) {
604  return false;
605  }
606 
607  string matrixName1, matrixName2;
608  bool tempfilefound = false;
609 
610  //check for every ID that should be there if there is a corresponding file
611  for (size_t i = 0; i < overlapIDs.size(); i++) {
612 
613  //reset counter
614  tempfilefound = false;
615  matrixName1 = makeMatrixFileName(overlapIDs[i], true);
616  matrixName1 = makeMatrixFileName(overlapIDs[i], false);
617 
618  for (size_t j = 0; j < files.size(); j++) {
619  if (files[j].find(matrixName1) != string::npos) {
620  //file is present
621  tempfilefound = true;
622  foundFiles++;
623  }
624  }
625 
626  //file not found? then at least one is missing, return false
627  if (!tempfilefound) {
628  return tempfilefound;
629  }
630  }
631  return true;
632 }
633 
634 std::string PndLmdAlignManager::makeBinaryPairFileName(int overlapId, bool incentimeters) {
635  std::stringstream filename;
636  filename << "/pairs-" << overlapId;
637  incentimeters ? filename << "-cm.bin" : filename << "-px.bin";
638  return filename.str();
639 }
640 
641 std::string PndLmdAlignManager::makeMatrixFileName(int overlapId, bool incentimeters) {
642  stringstream matrixName;
643  matrixName << "/m";
644  incentimeters ? matrixName << overlapId << "cm.mat" : matrixName << overlapId << "px.mat";
645  return matrixName.str();
646 }
647 
649 
650  if (maxPairs > 0) {
651  for (mapIt it = aligners.begin(); it != aligners.end(); it++) {
652  it->second.setMaximumNumberOfHitPairs(maxPairs);
653  }
654  return;
655  }
656  else {
657  cout << "warning. max pairs must be larger than 0!\n";
658  return;
659  }
660 
661 }
662 
664  cout << "\x1B[2J\x1B[H";
665 }
666 
668 
669  bool pairAdded = false;
670 
671  int thisID = pair.getOverlapId();
672 
673  // check if the aligner for that pair is full. if yes, skip this pair.
674  // do this even before checking that pair, saves on cpu time.
675  if (alignersFull[thisID]) {
676  return false;
677  }
678 
679  pair.check();
680  if (pair.isSane()) {
681  pairAdded = aligners[thisID].addSimplePair(pair); //returns true if addPair succeeded
682  alignersFull[thisID] = !pairAdded; //if addPair failed, the aligner is full
683  }
684  else {
685  verbosePrint("pair is not sane. processing failed.\n", 1);
686  }
687 
688  //if aligner is full, start thread directly.
689  if (alignersFull[thisID]) {
690  //alignerThreadGroup.create_thread(
691  threadPool.enqueue(
692  boost::bind(&PndLmdAlignManager::runSensorAligner, this, boost::ref(aligners[thisID])));
693  }
694  return true;
695 }
696 
698  //check if all aligners are done
699  for (auto &id : overlapIDs) {
700  if (!alignersFull[id]) {
701  return false;
702  }
703  }
704  verbosePrint("all aligners are already full. no further files will be read.\n", 1);
705  return true;
706 }
707 
709 
710  //start all alignsers that have not already started (i.e. don't have required no of Pairs)
711  int notStarted = 0;
712 
713  cout << "starting remaining aligners.\n";
714  for (auto &id : overlapIDs) {
715 
716  //onlt start aligners that have not already started
717  if (!(alignersFull[id])) {
718 
719  //if pair could not be added, aligner is full. start thread directly.
720  //alignerThreadGroup.create_thread(
721  threadPool.enqueue(
722  boost::bind(&PndLmdAlignManager::runSensorAligner, this, boost::ref(aligners[id])));
723  notStarted++;
724  }
725  }
726  cout << notStarted << " aligners remained.\n";
727  //cout << "jobs queue size : " << alignerThreadGroup.size() << "/360\n";
728  cout << "waiting for all aligners to finish...";
729 
730  //wait for all threads to complete
731  //alignerThreadGroup.join_all();
732  flush(cout);
733  threadPool.wait();
734 
735  cout << "done!\n";
736 
737  for (auto &id : overlapIDs) {
738  if (aligners[id].successful()) {
739 
740  Matrix result = aligners[id].getResultMatrix();
741  string matrixFilename = matrixOutDir + makeMatrixFileName(id, _inCentimeters);
742 
743  if (!writeMatrix(result, matrixFilename)) {
744  cout << "ERROR: could not write matrix " << matrixFilename << "\n";
745  }
746 
747  _info << "aligner " << id << ":\n";
748  _info << "no of pairs: " << aligners[id].getNoOfPairs() << "\n";
749  _info << "\n";
750  }
751  else {
752  cout << "Error: aligner for " << id << " failed.\n";
753  }
754  }
755 
756  //write matrix info
757  ofstream of;
758  if (_inCentimeters) {
759  of.open((matrixOutDir + "/info-cm.txt").c_str());
760  }
761  else {
762  of.open((matrixOutDir + "/info-px.txt").c_str());
763  }
764 
765  of << _info.str();
766  of.close();
767  cout << "all aligners done.\n";
768 }
769 
771 
772  if (binaryPairFileDirectory == "") {
773  cout << "error: binary pair file directory not set.\n";
774  cout << "use PndLmdAlignManager::setBinaryPairFileDirectory()\n";
775  return false;
776  }
777 
778  int cur, tot;
779  cur = 0;
780  tot = aligners.size();
781 
782  cout << "writing all pairs to binary files\n";
784 
785  //maybe do this multithreaded?
786  for (auto &aligner : aligners) {
787  loadBar(cur++, tot, 1000, 60);
788  if (!aligner.second.writePairsToBinary(binaryPairFileDirectory)) {
789  return false;
790  }
791  }
792  return true;
793 }
794 
796 
797  bool success = true;
798 
799  if (binaryPairFileDirectory == "") {
800  cout << "error: binary pair file directory not set.\n";
801  cout << "use PndLmdAlignManager::setBinaryPairFileDirectory()\n";
802  return false;
803  }
804 
805  int cur, tot;
806  cur = 0;
807  tot = aligners.size();
808 
809  cout << "reading all pairs from binary files and starting aligners...\n";
810 
811  // use single thread as long as gGeoManger / GeometryHelper are not thread safe!
812  bool mt = true;
813  PndLmdThreadPool threadPool(maxThreads);
814 
815  for (auto &id : overlapIDs) {
816 
817  if (!mt) {
818  // single threaded, low memory consumption version
819  loadBar(cur++, tot, 1000, 60);
820  PndLmdSensorAligner &thisAligner = aligners[id];
821  if (thisAligner.readPairsFromBinary(binaryPairFileDirectory)) {
822  runSensorAligner(thisAligner);
823  }
824  }
825  else {
826  loadBar(cur++, tot, 1000, 60);
827  PndLmdSensorAligner &thisAligner = aligners[id];
828 
829  if (thisAligner.readPairsFromBinary(binaryPairFileDirectory)) {
830  alignersFull[id] = true;
831  threadPool.enqueue(
832  boost::bind(&PndLmdAlignManager::runSensorAligner, this, boost::ref(thisAligner)));
833  }
834  else {
835  success = false;
836  }
837  }
838  }
839 
840  // write matrices to disk etc
841  waitForCompletion(threadPool);
842  return success;
843 }
844 
845 void PndLmdAlignManager::verbosePrint(std::string input, int level) {
846  if (_verboseLevel >= level) {
847  cout << input;
848  }
849 }
850 
851 boost::property_tree::ptree PndLmdAlignManager::readConfigFile(std::string filename) {
852 
853  //check if file exists
854  if (!boost::filesystem::exists(filename)) {
855  cerr << "PndLmdAlignManager::readConfig: ERROR! File " << filename << " not found!\n";
856  }
857 
858  std::ifstream is(filename);
859  boost::property_tree::ptree root;
860  try {
861  boost::property_tree::read_json(is, root);
862  }
863  catch (std::exception &e) {
864  cerr << "PndLmdAlignManager::readConfig: ERROR! Can't parse json file " << filename << ".\n";
865  }
866  return root;
867 }
868 
869 bool PndLmdAlignManager::writeConfigFile(boost::property_tree::ptree configTree, std::string filename,
870  bool replaceExisting) {
871 
872  if (boost::filesystem::exists(filename) && !replaceExisting) {
873  cerr << "PndLmdAlignManager::writeConfig: Config already exists, will not be replaced.\n";
874  return false;
875  }
876 
877  boost::filesystem::path outPath(filename);
878  if (!boost::filesystem::exists(outPath.parent_path())) {
879  boost::filesystem::create_directories(outPath.parent_path());
880  }
881 
882  std::ofstream os(filename);
883  boost::property_tree::write_json(os, configTree);
884  return true;
885 }
886 
888  for (mapIt it = aligners.begin(); it != aligners.end(); it++) {
889  it->second.clearPairs();
890  }
891 }
std::vector< std::string > fileNames
void runSensorAligner(PndLmdSensorAligner &aligner)
bool addFile(std::string filename)
printf("RealTime=%f seconds, CpuTime=%f seconds\n", rtime, ctime)
std::vector< int > getAvailableOverlapIDs()
void setInCentimeters(bool inCentimeters)
double r
Definition: RiemannTest.C:14
std::string binaryPairFileDirectory
bool writePairsToBinary(const std::string directory)
Int_t i
Definition: run_full.C:25
PndTransMap * map
Definition: sim_emc_apd.C:99
exit(0)
void waitForCompletion(PndLmdThreadPool &threadPool)
void enqueue(job_t job)
static TGeoHMatrix readTGeoHMatrix(std::string filename)
std::map< int, bool > alignersFull
int n
TGeoRotation rotation
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
Definition: simubg.C:28
void setOverlapId(Int_t overlapId)
static bool mkdir(std::string path)
void applyDynamicCut(double percent=5.0)
static Matrix castTGeoHMatrixToMatrix(const TGeoHMatrix &matrix)
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)
bool isSane() const
Definition: PndLmdHitPair.h:75
void setZasTimestamp(bool timestamp)
void setInCentimeters(bool value)
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)
int32_t m
Definition: matrix.h:139
Definition: matrix.h:50
static int is
Definition: ranlxd.cxx:374
static std::string makeMatrixFileName(int overlapId=0, bool incentimeters=true)
static bool exists(std::string file)
int32_t n
Definition: matrix.h:139
void verbosePrint(std::string input, int level=3)
Double_t x
static bool writeConfigFile(boost::property_tree::ptree configTree, std::string filename, bool replaceExisting=true)
std::stringstream _info
static Matrix readMatrix(std::string filename)
TString directory
int addFilesFromDirectory(std::string directory, int maxFiles=0)
void setMaxPairs(int maxPairs)
vector< int > overlapIDs
FLOAT ** val
Definition: matrix.h:138
static std::vector< std::vector< double > > readFromCSVFile(std::string filename)
bool readPairsFromBinary(const std::string directory)
const string filename