FairRoot/PandaRoot
Public Member Functions | Static Public Member Functions | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | Friends | List of all members
PndLmdAlignManager Class Reference

#include <PndLmdAlignManager.h>

Public Member Functions

 PndLmdAlignManager ()
 
virtual ~PndLmdAlignManager ()
 
void init ()
 
bool addFile (std::string filename)
 
void readFilesAndAlign ()
 
bool writePairsToBinaryFiles ()
 
bool readPairsFromBinaryFilesAndAlign ()
 
bool checkForBinaryFiles ()
 
bool checkForLmdMatrixFiles ()
 
void clearPairs ()
 
int addFilesFromDirectory (std::string directory, int maxFiles=0)
 
void validate ()
 
void loadBar (int current, int total, int resolution, int width, std::string message="")
 
void setMaxPairs (int maxPairs)
 
void setSingleAligner (bool singleAligner)
 
void setOutFilename (const std::string &filename)
 
void setMatrixOutDir (std::string directory)
 
void setInCentimeters (bool inCentimeters)
 
void setZasTimestamp (bool timestamp)
 
void setBinaryPairFileDirectory (const std::string &directory)
 
void verboseLevel (int verbose)
 
void setMaxThreads (int NmaxThreads=0)
 

Static Public Member Functions

static Matrix castTGeoHMatrixToMatrix (const TGeoHMatrix &matrix)
 
static Matrix readMatrix (std::string filename)
 
static TGeoHMatrix readTGeoHMatrix (std::string filename)
 
static bool writeMatrix (Matrix &mat, std::string filename)
 
static std::vector
< std::vector< double > > 
readFromCSVFile (std::string filename)
 
static int searchFiles (std::string curr_directory, std::vector< std::string > &list, std::string extension="", bool includeSubDirs=true)
 
static void clearScreen ()
 
static int searchDirectories (std::string curr_directory, std::vector< std::string > &list, bool includeSubDirs=true)
 
static bool mkdir (std::string path)
 
static bool exists (std::string file)
 
static std::vector< std::string > findRegex (std::string source, std::string regex)
 
static bool writeConfigFile (boost::property_tree::ptree configTree, std::string filename, bool replaceExisting=true)
 
static boost::property_tree::ptree readConfigFile (std::string filename)
 

Private Types

typedef std::map< int,
PndLmdSensorAligner >
::iterator 
mapIt
 

Private Member Functions

bool allAlignersFull ()
 
bool addPairAndStartAligner (PndLmdHitPair &pair, PndLmdThreadPool &threadPool)
 
void runSensorAligner (PndLmdSensorAligner &aligner)
 
void checkIOpaths ()
 
void waitForCompletion (PndLmdThreadPool &threadPool)
 
void verbosePrint (std::string input, int level=3)
 

Static Private Member Functions

static std::string makeBinaryPairFileName (int overlapId=0, bool incentimeters=true)
 
static std::string makeMatrixFileName (int overlapId=0, bool incentimeters=true)
 

Private Attributes

std::mutex MTLBmutex
 
std::mutex geometryHelperMutex
 
int _verboseLevel
 
unsigned int maxThreads
 
std::stringstream _info
 
std::map< int,
PndLmdSensorAligner
aligners
 
std::map< int, bool > alignersFull
 
bool _allFilesAdded
 
std::vector< std::string > fileNames
 
bool _singleAligner
 
bool _inCentimeters
 
bool enableHelperMatrix
 
bool _zIsTimestamp
 
std::string outFilename
 
std::string matrixOutDir
 
std::string binaryPairFileDirectory
 
bool _firstInitDone
 
bool _multithreaded
 
vector< int > overlapIDs
 

Friends

class PndLmdSensorAligner
 
class PndLmdAlignQA
 

Detailed Description

Definition at line 31 of file PndLmdAlignManager.h.

Member Typedef Documentation

typedef std::map<int, PndLmdSensorAligner>::iterator PndLmdAlignManager::mapIt
private

Definition at line 33 of file PndLmdAlignManager.h.

Constructor & Destructor Documentation

PndLmdAlignManager::PndLmdAlignManager ( )

Definition at line 41 of file PndLmdAlignManager.cxx.

References init().

41  {
42  init();
43 }
PndLmdAlignManager::~PndLmdAlignManager ( )
virtual

Definition at line 84 of file PndLmdAlignManager.cxx.

84  {
85 }

Member Function Documentation

bool PndLmdAlignManager::addFile ( std::string  filename)

Definition at line 95 of file PndLmdAlignManager.cxx.

References _allFilesAdded, and fileNames.

95  {
96 
97  if (_allFilesAdded) {
98  return false;
99  }
100  else {
101  fileNames.push_back(filename);
102  return true;
103  }
104 }
std::vector< std::string > fileNames
const string filename
int PndLmdAlignManager::addFilesFromDirectory ( std::string  directory,
int  maxFiles = 0 
)

Definition at line 106 of file PndLmdAlignManager.cxx.

References _allFilesAdded, fileNames, i, and searchFiles().

Referenced by runLumiPixel2fMatrixFinder().

106  {
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 }
std::vector< std::string > fileNames
Int_t i
Definition: run_full.C:25
static int searchFiles(std::string curr_directory, std::vector< std::string > &list, std::string extension="", bool includeSubDirs=true)
TString directory
bool PndLmdAlignManager::addPairAndStartAligner ( PndLmdHitPair pair,
PndLmdThreadPool threadPool 
)
private

Definition at line 667 of file PndLmdAlignManager.cxx.

References aligners, alignersFull, PndLmdHitPair::check(), PndLmdThreadPool::enqueue(), PndLmdHitPair::getOverlapId(), PndLmdHitPair::isSane(), runSensorAligner(), and verbosePrint().

Referenced by readFilesAndAlign().

667  {
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 }
void runSensorAligner(PndLmdSensorAligner &aligner)
void enqueue(job_t job)
std::map< int, bool > alignersFull
std::map< int, PndLmdSensorAligner > aligners
Int_t getOverlapId() const
bool isSane() const
Definition: PndLmdHitPair.h:75
void verbosePrint(std::string input, int level=3)
bool PndLmdAlignManager::allAlignersFull ( )
private

Definition at line 697 of file PndLmdAlignManager.cxx.

References alignersFull, overlapIDs, and verbosePrint().

Referenced by readFilesAndAlign().

697  {
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 }
std::map< int, bool > alignersFull
void verbosePrint(std::string input, int level=3)
vector< int > overlapIDs
Matrix PndLmdAlignManager::castTGeoHMatrixToMatrix ( const TGeoHMatrix &  matrix)
static

Definition at line 493 of file PndLmdAlignManager.cxx.

Referenced by readMatrix(), PndLmdSensorAligner::transformToLmdLocal(), and PndLmdSensorAligner::transformToSensorOne().

493  {
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 }
Definition: matrix.h:50
bool PndLmdAlignManager::checkForBinaryFiles ( )

Definition at line 562 of file PndLmdAlignManager.cxx.

References _inCentimeters, binaryPairFileDirectory, files, i, makeBinaryPairFileName(), overlapIDs, and searchFiles().

Referenced by runLumiPixel2fMatrixFinder(), and runSensorAligner().

562  {
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 }
std::string binaryPairFileDirectory
Int_t i
Definition: run_full.C:25
static int searchFiles(std::string curr_directory, std::vector< std::string > &list, std::string extension="", bool includeSubDirs=true)
std::map< int, TString > files
Definition: simubg.C:28
static std::string makeBinaryPairFileName(int overlapId=0, bool incentimeters=true)
vector< int > overlapIDs
bool PndLmdAlignManager::checkForLmdMatrixFiles ( )

Definition at line 596 of file PndLmdAlignManager.cxx.

References binaryPairFileDirectory, files, i, makeMatrixFileName(), overlapIDs, and searchFiles().

Referenced by runLumiPixel2fMatrixFinder().

596  {
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 }
std::string binaryPairFileDirectory
Int_t i
Definition: run_full.C:25
static int searchFiles(std::string curr_directory, std::vector< std::string > &list, std::string extension="", bool includeSubDirs=true)
std::map< int, TString > files
Definition: simubg.C:28
static std::string makeMatrixFileName(int overlapId=0, bool incentimeters=true)
vector< int > overlapIDs
void PndLmdAlignManager::checkIOpaths ( )
private

Definition at line 256 of file PndLmdAlignManager.cxx.

References exit(), fileNames, matrixOutDir, and outFilename.

Referenced by setOutFilename().

256  {
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 }
std::vector< std::string > fileNames
exit(0)
void PndLmdAlignManager::clearPairs ( )

Definition at line 887 of file PndLmdAlignManager.cxx.

References aligners.

887  {
888  for (mapIt it = aligners.begin(); it != aligners.end(); it++) {
889  it->second.clearPairs();
890  }
891 }
std::map< int, PndLmdSensorAligner > aligners
std::map< int, PndLmdSensorAligner >::iterator mapIt
void PndLmdAlignManager::clearScreen ( )
static

Definition at line 663 of file PndLmdAlignManager.cxx.

663  {
664  cout << "\x1B[2J\x1B[H";
665 }
bool PndLmdAlignManager::exists ( std::string  file)
static

Definition at line 424 of file PndLmdAlignManager.cxx.

424  {
425  return boost::filesystem::exists(boost::filesystem::path(path));
426 }
vector< string > PndLmdAlignManager::findRegex ( std::string  source,
std::string  regex 
)
static

Definition at line 428 of file PndLmdAlignManager.cxx.

Referenced by PndLmdAlignQA::readMatrixInfo().

428  {
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 }
void PndLmdAlignManager::init ( )

Definition at line 45 of file PndLmdAlignManager.cxx.

References _allFilesAdded, _firstInitDone, _inCentimeters, _info, _multithreaded, _singleAligner, _verboseLevel, _zIsTimestamp, aligners, alignersFull, enableHelperMatrix, fileNames, PndLmdGeometryHelper::getAvailableOverlapIDs(), PndLmdGeometryHelper::getInstance(), helper, i, outFilename, overlapIDs, PndLmdSensorAligner::setInCentimeters(), PndLmdSensorAligner::setManager(), PndLmdSensorAligner::setOverlapId(), and PndLmdSensorAligner::setZasTimetamp().

Referenced by PndLmdAlignManager().

45  {
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 }
std::vector< std::string > fileNames
std::vector< int > getAvailableOverlapIDs()
Int_t i
Definition: run_full.C:25
std::map< int, bool > alignersFull
static PndLmdGeometryHelper & getInstance()
std::map< int, PndLmdSensorAligner > aligners
void setOverlapId(Int_t overlapId)
PndLmdGeometryHelper * helper
void setInCentimeters(bool value)
void setZasTimetamp(bool value)
void setManager(PndLmdAlignManager *manager)
std::stringstream _info
vector< int > overlapIDs
void PndLmdAlignManager::loadBar ( int  current,
int  total,
int  resolution,
int  width,
std::string  message = "" 
)

Definition at line 217 of file PndLmdAlignManager.cxx.

References c, i, MTLBmutex, n, printf(), and x.

Referenced by readFilesAndAlign(), readPairsFromBinaryFilesAndAlign(), and writePairsToBinaryFiles().

217  {
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 }
printf("RealTime=%f seconds, CpuTime=%f seconds\n", rtime, ctime)
double r
Definition: RiemannTest.C:14
Int_t i
Definition: run_full.C:25
int n
Double_t x
std::string PndLmdAlignManager::makeBinaryPairFileName ( int  overlapId = 0,
bool  incentimeters = true 
)
staticprivate

Definition at line 634 of file PndLmdAlignManager.cxx.

References filename.

Referenced by checkForBinaryFiles(), PndLmdSensorAligner::readPairsFromBinary(), and PndLmdSensorAligner::writePairsToBinary().

634  {
635  std::stringstream filename;
636  filename << "/pairs-" << overlapId;
637  incentimeters ? filename << "-cm.bin" : filename << "-px.bin";
638  return filename.str();
639 }
const string filename
std::string PndLmdAlignManager::makeMatrixFileName ( int  overlapId = 0,
bool  incentimeters = true 
)
staticprivate

Definition at line 641 of file PndLmdAlignManager.cxx.

Referenced by checkForLmdMatrixFiles(), PndLmdAlignQA::checkForMatrixFiles(), and waitForCompletion().

641  {
642  stringstream matrixName;
643  matrixName << "/m";
644  incentimeters ? matrixName << overlapId << "cm.mat" : matrixName << overlapId << "px.mat";
645  return matrixName.str();
646 }
bool PndLmdAlignManager::mkdir ( std::string  path)
static

Definition at line 416 of file PndLmdAlignManager.cxx.

Referenced by PndLmdAlignQA::createHist(), PndLmdSensorAligner::writePairsToBinary(), and writePairsToBinaryFiles().

416  {
417  boost::filesystem::path bpath(path);
418  if (boost::filesystem::exists(bpath)) {
419  return true;
420  }
421  return boost::filesystem::create_directories(bpath);
422 }
boost::property_tree::ptree PndLmdAlignManager::readConfigFile ( std::string  filename)
static

Definition at line 851 of file PndLmdAlignManager.cxx.

References is.

851  {
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 }
static int is
Definition: ranlxd.cxx:374
const string filename
void PndLmdAlignManager::readFilesAndAlign ( )

Definition at line 128 of file PndLmdAlignManager.cxx.

References _allFilesAdded, addPairAndStartAligner(), allAlignersFull(), exit(), fileNames, i, loadBar(), and waitForCompletion().

Referenced by runLumiPixel2fMatrixFinder().

128  {
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 }
std::vector< std::string > fileNames
Int_t i
Definition: run_full.C:25
exit(0)
void waitForCompletion(PndLmdThreadPool &threadPool)
bool addPairAndStartAligner(PndLmdHitPair &pair, PndLmdThreadPool &threadPool)
void loadBar(int current, int total, int resolution, int width, std::string message="")
vector< vector< double > > PndLmdAlignManager::readFromCSVFile ( std::string  filename)
static

Definition at line 366 of file PndLmdAlignManager.cxx.

Referenced by readTGeoHMatrix().

366  {
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 }
const string filename
Matrix PndLmdAlignManager::readMatrix ( std::string  filename)
static

Definition at line 283 of file PndLmdAlignManager.cxx.

References castTGeoHMatrixToMatrix(), and readTGeoHMatrix().

283  {
284  // avoid code duplication
286 }
static TGeoHMatrix readTGeoHMatrix(std::string filename)
static Matrix castTGeoHMatrixToMatrix(const TGeoHMatrix &matrix)
const string filename
bool PndLmdAlignManager::readPairsFromBinaryFilesAndAlign ( )

Definition at line 795 of file PndLmdAlignManager.cxx.

References aligners, alignersFull, binaryPairFileDirectory, PndLmdThreadPool::enqueue(), loadBar(), maxThreads, overlapIDs, PndLmdSensorAligner::readPairsFromBinary(), runSensorAligner(), mrfdata_8b_error::success, and waitForCompletion().

Referenced by runLumiPixel2fMatrixFinder().

795  {
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 }
void runSensorAligner(PndLmdSensorAligner &aligner)
std::string binaryPairFileDirectory
void waitForCompletion(PndLmdThreadPool &threadPool)
std::map< int, bool > alignersFull
std::map< int, PndLmdSensorAligner > aligners
void loadBar(int current, int total, int resolution, int width, std::string message="")
vector< int > overlapIDs
bool readPairsFromBinary(const std::string directory)
TGeoHMatrix PndLmdAlignManager::readTGeoHMatrix ( std::string  filename)
static

Definition at line 288 of file PndLmdAlignManager.cxx.

References i, Matrix::m, Matrix::n, readFromCSVFile(), rotation, and Matrix::val.

Referenced by PndLmdAlignQA::getMatrixDiffCM(), PndLmdAlignQA::getMisalignedOverlapFromICP(), and readMatrix().

288  {
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 }
Int_t i
Definition: run_full.C:25
TGeoRotation rotation
Definition: matrix.h:50
static std::vector< std::vector< double > > readFromCSVFile(std::string filename)
const string filename
void PndLmdAlignManager::runSensorAligner ( PndLmdSensorAligner aligner)
private

Definition at line 191 of file PndLmdAlignManager.cxx.

References PndLmdSensorAligner::applyDynamicCut(), binaryPairFileDirectory, PndLmdSensorAligner::calculateMatrix(), PndLmdSensorAligner::check(), checkForBinaryFiles(), PndLmdSensorAligner::clearPairs(), PndLmdSensorAligner::transformToSensorOne(), and PndLmdSensorAligner::writePairsToBinary().

Referenced by addPairAndStartAligner(), readPairsFromBinaryFilesAndAlign(), and waitForCompletion().

191  {
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 }
std::string binaryPairFileDirectory
bool writePairsToBinary(const std::string directory)
void applyDynamicCut(double percent=5.0)
int PndLmdAlignManager::searchDirectories ( std::string  curr_directory,
std::vector< std::string > &  list,
bool  includeSubDirs = true 
)
static

Definition at line 455 of file PndLmdAlignManager.cxx.

456  {
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 }
static int searchDirectories(std::string curr_directory, std::vector< std::string > &list, bool includeSubDirs=true)
int PndLmdAlignManager::searchFiles ( std::string  curr_directory,
std::vector< std::string > &  list,
std::string  extension = "",
bool  includeSubDirs = true 
)
static

Definition at line 393 of file PndLmdAlignManager.cxx.

Referenced by addFilesFromDirectory(), checkForBinaryFiles(), checkForLmdMatrixFiles(), and PndLmdAlignQA::checkForMatrixFiles().

394  {
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 }
static int searchFiles(std::string curr_directory, std::vector< std::string > &list, std::string extension="", bool includeSubDirs=true)
void PndLmdAlignManager::setBinaryPairFileDirectory ( const std::string &  directory)
inline

Definition at line 171 of file PndLmdAlignManager.h.

References directory.

Referenced by runLumiPixel2fMatrixFinder().

171  {
173  }
std::string binaryPairFileDirectory
TString directory
void PndLmdAlignManager::setInCentimeters ( bool  inCentimeters)

Definition at line 479 of file PndLmdAlignManager.cxx.

References _inCentimeters, and aligners.

Referenced by PndLmdAlignQA::checkCyclicMatrices(), and runLumiPixel2fMatrixFinder().

479  {
480  _inCentimeters = inCentimeters;
481  for (mapIt it = aligners.begin(); it != aligners.end(); it++) {
482  it->second.setInCentimeters(_inCentimeters);
483  }
484 }
std::map< int, PndLmdSensorAligner > aligners
std::map< int, PndLmdSensorAligner >::iterator mapIt
void PndLmdAlignManager::setMatrixOutDir ( std::string  directory)
inline

Definition at line 150 of file PndLmdAlignManager.h.

References directory.

Referenced by PndLmdAlignQA::checkCombined(), PndLmdAlignQA::checkCyclicMatrices(), and runLumiPixel2fMatrixFinder().

150  {
152  }
TString directory
void PndLmdAlignManager::setMaxPairs ( int  maxPairs)

Definition at line 648 of file PndLmdAlignManager.cxx.

References aligners.

Referenced by runLumiPixel2fMatrixFinder().

648  {
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 }
std::map< int, PndLmdSensorAligner > aligners
std::map< int, PndLmdSensorAligner >::iterator mapIt
void PndLmdAlignManager::setMaxThreads ( int  NmaxThreads = 0)
inline

Definition at line 182 of file PndLmdAlignManager.h.

Referenced by runLumiPixel2fMatrixFinder().

182  {
183  if (NmaxThreads < 0 || NmaxThreads > 256) {
184  //cerr << "Invalid number of threasds!\n";
185  }
186  else {
187  maxThreads = NmaxThreads;
188  }
189  }
void PndLmdAlignManager::setOutFilename ( const std::string &  filename)
inline

Definition at line 124 of file PndLmdAlignManager.h.

References checkIOpaths(), and filename.

124  {
126  checkIOpaths();
127  }
const string filename
void PndLmdAlignManager::setSingleAligner ( bool  singleAligner)
inline

Definition at line 120 of file PndLmdAlignManager.h.

120  {
121  _singleAligner = singleAligner;
122  }
void PndLmdAlignManager::setZasTimestamp ( bool  timestamp)

Definition at line 486 of file PndLmdAlignManager.cxx.

References _zIsTimestamp, and aligners.

486  {
487  _zIsTimestamp = timestamp;
488  for (mapIt it = aligners.begin(); it != aligners.end(); it++) {
489  it->second.setZasTimetamp(_zIsTimestamp);
490  }
491 }
std::map< int, PndLmdSensorAligner > aligners
std::map< int, PndLmdSensorAligner >::iterator mapIt
void PndLmdAlignManager::validate ( )

Definition at line 87 of file PndLmdAlignManager.cxx.

References aligners.

87  {
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 }
std::map< int, PndLmdSensorAligner > aligners
std::map< int, PndLmdSensorAligner >::iterator mapIt
void PndLmdAlignManager::verboseLevel ( int  verbose)
inline

Definition at line 175 of file PndLmdAlignManager.h.

References a, and verbose.

Referenced by runLumiPixel2fMatrixFinder().

175  {
177  for (auto &a : aligners) {
178  a.second.verboseLevel(_verboseLevel);
179  }
180  }
#define verbose
std::map< int, PndLmdSensorAligner > aligners
Int_t a
Definition: anaLmdDigi.C:126
void PndLmdAlignManager::verbosePrint ( std::string  input,
int  level = 3 
)
private

Definition at line 845 of file PndLmdAlignManager.cxx.

References _verboseLevel.

Referenced by addPairAndStartAligner(), and allAlignersFull().

845  {
846  if (_verboseLevel >= level) {
847  cout << input;
848  }
849 }
void PndLmdAlignManager::waitForCompletion ( PndLmdThreadPool threadPool)
private

Definition at line 708 of file PndLmdAlignManager.cxx.

References _inCentimeters, _info, aligners, alignersFull, PndLmdThreadPool::enqueue(), makeMatrixFileName(), matrixOutDir, overlapIDs, runSensorAligner(), PndLmdThreadPool::wait(), and writeMatrix().

Referenced by readFilesAndAlign(), and readPairsFromBinaryFilesAndAlign().

708  {
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 }
void runSensorAligner(PndLmdSensorAligner &aligner)
void enqueue(job_t job)
std::map< int, bool > alignersFull
std::map< int, PndLmdSensorAligner > aligners
static bool writeMatrix(Matrix &mat, std::string filename)
Definition: matrix.h:50
static std::string makeMatrixFileName(int overlapId=0, bool incentimeters=true)
std::stringstream _info
vector< int > overlapIDs
bool PndLmdAlignManager::writeConfigFile ( boost::property_tree::ptree  configTree,
std::string  filename,
bool  replaceExisting = true 
)
static

Definition at line 869 of file PndLmdAlignManager.cxx.

870  {
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 }
const string filename
bool PndLmdAlignManager::writeMatrix ( Matrix mat,
std::string  filename 
)
static

Definition at line 343 of file PndLmdAlignManager.cxx.

Referenced by waitForCompletion().

343  {
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 }
const string filename
bool PndLmdAlignManager::writePairsToBinaryFiles ( )

Definition at line 770 of file PndLmdAlignManager.cxx.

References aligners, binaryPairFileDirectory, loadBar(), and mkdir().

770  {
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 }
std::string binaryPairFileDirectory
std::map< int, PndLmdSensorAligner > aligners
static bool mkdir(std::string path)
void loadBar(int current, int total, int resolution, int width, std::string message="")

Friends And Related Function Documentation

friend class PndLmdAlignQA
friend

Definition at line 39 of file PndLmdAlignManager.h.

friend class PndLmdSensorAligner
friend

Definition at line 38 of file PndLmdAlignManager.h.

Member Data Documentation

bool PndLmdAlignManager::_allFilesAdded
private

Definition at line 53 of file PndLmdAlignManager.h.

Referenced by addFile(), addFilesFromDirectory(), init(), and readFilesAndAlign().

bool PndLmdAlignManager::_firstInitDone
private

Definition at line 57 of file PndLmdAlignManager.h.

Referenced by init().

bool PndLmdAlignManager::_inCentimeters
private
std::stringstream PndLmdAlignManager::_info
private

Definition at line 48 of file PndLmdAlignManager.h.

Referenced by init(), and waitForCompletion().

bool PndLmdAlignManager::_multithreaded
private

Definition at line 58 of file PndLmdAlignManager.h.

Referenced by init().

bool PndLmdAlignManager::_singleAligner
private

Definition at line 55 of file PndLmdAlignManager.h.

Referenced by init().

int PndLmdAlignManager::_verboseLevel
private

Definition at line 45 of file PndLmdAlignManager.h.

Referenced by init(), and verbosePrint().

bool PndLmdAlignManager::_zIsTimestamp
private

Definition at line 55 of file PndLmdAlignManager.h.

Referenced by init(), and setZasTimestamp().

std::map<int, PndLmdSensorAligner> PndLmdAlignManager::aligners
private
std::map<int, bool> PndLmdAlignManager::alignersFull
private
std::string PndLmdAlignManager::binaryPairFileDirectory
private
bool PndLmdAlignManager::enableHelperMatrix
private

Definition at line 55 of file PndLmdAlignManager.h.

Referenced by init().

std::vector<std::string> PndLmdAlignManager::fileNames
private
std::mutex PndLmdAlignManager::geometryHelperMutex
private
std::string PndLmdAlignManager::matrixOutDir
private

Definition at line 56 of file PndLmdAlignManager.h.

Referenced by checkIOpaths(), and waitForCompletion().

unsigned int PndLmdAlignManager::maxThreads
private

Definition at line 46 of file PndLmdAlignManager.h.

Referenced by readPairsFromBinaryFilesAndAlign().

std::mutex PndLmdAlignManager::MTLBmutex
private

Definition at line 42 of file PndLmdAlignManager.h.

Referenced by loadBar().

std::string PndLmdAlignManager::outFilename
private

Definition at line 56 of file PndLmdAlignManager.h.

Referenced by checkIOpaths(), and init().

vector<int> PndLmdAlignManager::overlapIDs
private

The documentation for this class was generated from the following files: