PandaRoot
PndLmdAlignManager.h
Go to the documentation of this file.
1 /*
2  * PndLmdAlignManager.h
3  *
4  * Created on: May 26, 2015
5  * Author: Roman Klasen, roklasen@uni-mainz.de or klasen@kph.uni-mainz.de
6  *
7  * This is a manager for all SensorAligner objects. It gathers all PndLmdHitPairs indiscriminately,
8  * and sorts them to 40 (depends on sensor geometry) SensorAligner Objects. It also stores the
9  * resultant matrices and writes them to disk (appropriate format is yet to come, for now just ascii).
10  */
11 
12 #ifndef LMD_LMDSENSORALIGNMENT_PNDLMDALIGNMANAGER_H_
13 #define LMD_LMDSENSORALIGNMENT_PNDLMDALIGNMANAGER_H_
14 
15 #include "PndLmdHitPair.h"
16 
17 #include <boost/property_tree/ptree.hpp>
18 #include <boost/property_tree/json_parser.hpp>
19 
20 #include <PndLmdAlignStructs.h>
21 #include <PndLmdGeometryHelper.h>
22 #include <PndLmdSensorAligner.h>
23 #include "PndLmdThreadPool.h"
24 
25 #include <TGeoMatrix.h>
26 
27 #include <matrix.h>
28 #include <string>
29 #include <sstream>
30 
32 
33  typedef std::map<int, PndLmdSensorAligner>::iterator mapIt;
34 
35  private:
36  // PndLmdSensorAligner needs private functions from here
37  friend class PndLmdSensorAligner;
38  friend class PndLmdAlignQA;
39 
40  // loadBarMultiThreaded
41  std::mutex MTLBmutex;
42  std::mutex geometryHelperMutex;
43 
44  int _verboseLevel;
45  unsigned int maxThreads;
46 
47  std::stringstream _info;
48 
49  std::map<int, PndLmdSensorAligner> aligners;
50  std::map<int, bool> alignersFull;
51 
52  bool _allFilesAdded;
53  std::vector<std::string> fileNames;
54  bool _singleAligner, _inCentimeters, enableHelperMatrix, _zIsTimestamp;
55  std::string outFilename, matrixOutDir, binaryPairFileDirectory;
56  bool _firstInitDone;
57  bool _multithreaded;
58 
59  vector<int> overlapIDs;
60 
61  bool allAlignersFull();
62 
63  // adds pairs just like the other function but starts an aligner if it is full
64  bool addPairAndStartAligner(PndLmdHitPair &pair, PndLmdThreadPool &threadPool);
65 
66  void runSensorAligner(PndLmdSensorAligner &aligner);
67  void checkIOpaths();
68 
69  // let all threads finish their work
70  void waitForCompletion(PndLmdThreadPool &threadPool);
71 
72  // generate the file name of a matrix or pair file, so changes must only be made once
73  static std::string makeBinaryPairFileName(int overlapId = 0, bool incentimeters = true);
74  static std::string makeMatrixFileName(int overlapId = 0, bool incentimeters = true);
75 
76  void verbosePrint(std::string input, int level = 3);
77 
78  public:
79  // An empty AlignManager will use standard values. If you want to reuse a manager, call Init() again and set values.
81  virtual ~PndLmdAlignManager();
82 
83  // initializes Manager on construction or resets every value to default
84  void init();
85 
86  // add filename, so the aligner adds the pairs itself
87  bool addFile(std::string filename);
88  void readFilesAndAlign();
89 
92  bool checkForBinaryFiles();
94  void clearPairs();
95 
96  // add all pair files that can be found in directory, up to a maximum of maxFiles
97  // returns number of files found (including 0 for no files) or -1 if "pretend" option is set
98  // set maxFiles=0 for all available files
99  int addFilesFromDirectory(std::string directory, int maxFiles = 0);
100 
101  void validate();
102 
103  // perform last checks and run calculations on all aligners
104  // void alignAllSensors();
105 
106  void loadBar(int current, int total, int resolution, int width, std::string message = "");
107 
108  void setMaxPairs(int maxPairs);
109 
110  static Matrix castTGeoHMatrixToMatrix(const TGeoHMatrix &matrix);
111 
112  // read and write matrix files to and from disk
113  static Matrix readMatrix(std::string filename);
114  static TGeoHMatrix readTGeoHMatrix(std::string filename);
115 
116  static bool writeMatrix(Matrix &mat, std::string filename);
117 
118  void setSingleAligner(bool singleAligner) { _singleAligner = singleAligner; }
119 
120  void setOutFilename(const std::string &filename)
121  {
122  outFilename = filename;
123  checkIOpaths();
124  }
125 
126  // static std::stringstream readFile(std::string filename);
127  static std::vector<std::vector<double>> readFromCSVFile(std::string filename);
128 
129  /*
130  * recursively searches files by extension in curr_directory and adds to list
131  * finds all extensions by default, but you can specify which extension
132  * (or part of filename) you want
133  */
134  static int searchFiles(std::string curr_directory, std::vector<std::string> &list, std::string extension = "", bool includeSubDirs = true);
135 
136  // clear console
137  static void clearScreen();
138 
139  // searches directories in curr_directory, adds to list
140  static int searchDirectories(std::string curr_directory, std::vector<std::string> &list, bool includeSubDirs = true);
141  static bool mkdir(std::string path);
142  static bool exists(std::string file);
143  static std::vector<std::string> findRegex(std::string source, std::string regex);
144 
145  void setMatrixOutDir(std::string directory) { matrixOutDir = directory; }
146 
147  /*
148  * set this BEFORE reading pair files. if inCentimeters==true, then hits will be decoded by PndLmdDim to centimeters
149  * and all calculations will be done in cm. Misalginment matrices are close to identity matrix, which could introduce
150  * numerical errors.
151  * If inCentimeters==false, then the ICP will work directly on row/col values and matrix will be in pixels. AlignManager
152  * converts matrix to cm, but the resultant matrix is the product of misalignment*ideal!
153  */
154  void setInCentimeters(bool inCentimeters);
155  void setZasTimestamp(bool timestamp);
156 
157  // write config file
158  static bool writeConfigFile(boost::property_tree::ptree configTree, std::string filename, bool replaceExisting = true);
159 
160  // read json config file
161  static boost::property_tree::ptree readConfigFile(std::string filename);
162 
163  void setBinaryPairFileDirectory(const std::string &directory) { binaryPairFileDirectory = directory; }
164 
165  void verboseLevel(int verbose)
166  {
167  _verboseLevel = verbose;
168  for (auto &a : aligners) {
169  a.second.verboseLevel(_verboseLevel);
170  }
171  }
172 
173  void setMaxThreads(int NmaxThreads = 0)
174  {
175  if (NmaxThreads < 0 || NmaxThreads > 256) {
176  // cerr << "Invalid number of threasds!\n";
177  } else {
178  maxThreads = NmaxThreads;
179  }
180  }
181 };
182 
183 #endif /* LMD_LMDSENSORALIGNMENT_PNDLMDALIGNMANAGER_H_ */
void setMatrixOutDir(std::string directory)
bool addFile(std::string filename)
void setInCentimeters(bool inCentimeters)
static int searchFiles(std::string curr_directory, std::vector< std::string > &list, std::string extension="", bool includeSubDirs=true)
void setOutFilename(const std::string &filename)
bool checkForLmdMatrixFiles()
virtual ~PndLmdAlignManager()
static bool writeMatrix(Matrix &mat, std::string filename)
static std::vector< std::string > findRegex(std::string source, std::string regex)
static bool exists(std::string file)
void setSingleAligner(bool singleAligner)
bool readPairsFromBinaryFilesAndAlign()
static Matrix readMatrix(std::string filename)
static TGeoHMatrix readTGeoHMatrix(std::string filename)
void setZasTimestamp(bool timestamp)
static bool writeConfigFile(boost::property_tree::ptree configTree, std::string filename, bool replaceExisting=true)
bool checkForBinaryFiles()
void loadBar(int current, int total, int resolution, int width, std::string message="")
Definition: matrix.h:50
void setMaxThreads(int NmaxThreads=0)
void verboseLevel(int verbose)
static std::vector< std::vector< double > > readFromCSVFile(std::string filename)
void checkIOpaths()
bool writePairsToBinaryFiles()
void setBinaryPairFileDirectory(const std::string &directory)
static int searchDirectories(std::string curr_directory, std::vector< std::string > &list, bool includeSubDirs=true)
static Matrix castTGeoHMatrixToMatrix(const TGeoHMatrix &matrix)
static void clearScreen()
static bool mkdir(std::string path)
int addFilesFromDirectory(std::string directory, int maxFiles=0)
void setMaxPairs(int maxPairs)
static boost::property_tree::ptree readConfigFile(std::string filename)