FairRoot/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 
37  // PndLmdSensorAligner needs private functions from here
38  friend class PndLmdSensorAligner;
39  friend class PndLmdAlignQA;
40 
41  //loadBarMultiThreaded
42  std::mutex MTLBmutex;
43  std::mutex geometryHelperMutex;
44 
46  unsigned int maxThreads;
47 
48  std::stringstream _info;
49 
50  std::map<int, PndLmdSensorAligner> aligners;
51  std::map<int, bool> alignersFull;
52 
54  std::vector<std::string> fileNames;
59 
60  vector<int> overlapIDs;
61 
62  bool allAlignersFull();
63 
64  //adds pairs just like the other function but starts an aligner if it is full
65  bool addPairAndStartAligner(PndLmdHitPair &pair, PndLmdThreadPool &threadPool);
66 
68  void checkIOpaths();
69 
70  //let all threads finish their work
71  void waitForCompletion(PndLmdThreadPool &threadPool);
72 
73  //generate the file name of a matrix or pair file, so changes must only be made once
74  static std::string makeBinaryPairFileName(int overlapId = 0, bool incentimeters = true);
75  static std::string makeMatrixFileName(int overlapId = 0, bool incentimeters = true);
76 
77  void verbosePrint(std::string input, int level = 3);
78 
79 public:
80 
81  //An empty AlignManager will use standard values. If you want to reuse a manager, call Init() again and set values.
83  virtual ~PndLmdAlignManager();
84 
85  // initializes Manager on construction or resets every value to default
86  void init();
87 
88  //add filename, so the aligner adds the pairs itself
89  bool addFile(std::string filename);
90  void readFilesAndAlign();
91 
94  bool checkForBinaryFiles();
96  void clearPairs();
97 
98  //add all pair files that can be found in directory, up to a maximum of maxFiles
99  //returns number of files found (including 0 for no files) or -1 if "pretend" option is set
100  //set maxFiles=0 for all available files
101  int addFilesFromDirectory(std::string directory, int maxFiles = 0);
102 
103  void validate();
104 
105  //perform last checks and run calculations on all aligners
106  //void alignAllSensors();
107 
108  void loadBar(int current, int total, int resolution, int width, std::string message = "");
109 
110  void setMaxPairs(int maxPairs);
111 
112  static Matrix castTGeoHMatrixToMatrix(const TGeoHMatrix &matrix);
113 
114  // read and write matrix files to and from disk
115  static Matrix readMatrix(std::string filename);
116  static TGeoHMatrix readTGeoHMatrix(std::string filename);
117 
118  static bool writeMatrix(Matrix &mat, std::string filename);
119 
120  void setSingleAligner(bool singleAligner) {
121  _singleAligner = singleAligner;
122  }
123 
124  void setOutFilename(const std::string& filename) {
125  outFilename = filename;
126  checkIOpaths();
127  }
128 
129  //static std::stringstream readFile(std::string filename);
130  static std::vector<std::vector<double> > readFromCSVFile(std::string filename);
131 
132  /*
133  * recursively searches files by extension in curr_directory and adds to list
134  * finds all extensions by default, but you can specify which extension
135  * (or part of filename) you want
136  */
137  static int searchFiles(std::string curr_directory, std::vector<std::string> &list,
138  std::string extension = "", bool includeSubDirs = true);
139 
140  //clear console
141  static void clearScreen();
142 
143  //searches directories in curr_directory, adds to list
144  static int searchDirectories(std::string curr_directory, std::vector<std::string> &list,
145  bool includeSubDirs = true);
146  static bool mkdir(std::string path);
147  static bool exists(std::string file);
148  static std::vector<std::string> findRegex(std::string source, std::string regex);
149 
150  void setMatrixOutDir(std::string directory) {
151  matrixOutDir = directory;
152  }
153 
154  /*
155  * set this BEFORE reading pair files. if inCentimeters==true, then hits will be decoded by PndLmdDim to centimeters
156  * and all calculations will be done in cm. Misalginment matrices are close to identity matrix, which could introduce
157  * numerical errors.
158  * If inCentimeters==false, then the ICP will work directly on row/col values and matrix will be in pixels. AlignManager
159  * converts matrix to cm, but the resultant matrix is the product of misalignment*ideal!
160  */
161  void setInCentimeters(bool inCentimeters);
162  void setZasTimestamp(bool timestamp);
163 
164  //write config file
165  static bool writeConfigFile(boost::property_tree::ptree configTree, std::string filename,
166  bool replaceExisting = true);
167 
168  //read json config file
169  static boost::property_tree::ptree readConfigFile(std::string filename);
170 
171  void setBinaryPairFileDirectory(const std::string& directory) {
172  binaryPairFileDirectory = directory;
173  }
174 
175  void verboseLevel(int verbose) {
176  _verboseLevel = verbose;
177  for (auto &a : aligners) {
178  a.second.verboseLevel(_verboseLevel);
179  }
180  }
181 
182  void setMaxThreads(int NmaxThreads = 0) {
183  if (NmaxThreads < 0 || NmaxThreads > 256) {
184  //cerr << "Invalid number of threasds!\n";
185  }
186  else {
187  maxThreads = NmaxThreads;
188  }
189  }
190 };
191 
192 #endif /* LMD_LMDSENSORALIGNMENT_PNDLMDALIGNMANAGER_H_ */
void setMatrixOutDir(std::string directory)
std::mutex geometryHelperMutex
std::vector< std::string > fileNames
void runSensorAligner(PndLmdSensorAligner &aligner)
bool addFile(std::string filename)
void setInCentimeters(bool inCentimeters)
std::string binaryPairFileDirectory
TFile * file
void waitForCompletion(PndLmdThreadPool &threadPool)
void setOutFilename(const std::string &filename)
#define verbose
static TGeoHMatrix readTGeoHMatrix(std::string filename)
std::map< int, bool > alignersFull
static int searchDirectories(std::string curr_directory, std::vector< std::string > &list, bool includeSubDirs=true)
static 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
static bool mkdir(std::string path)
void setSingleAligner(bool singleAligner)
static Matrix castTGeoHMatrixToMatrix(const TGeoHMatrix &matrix)
static std::string makeBinaryPairFileName(int overlapId=0, bool incentimeters=true)
Int_t a
Definition: anaLmdDigi.C:126
static std::vector< std::string > findRegex(std::string source, std::string regex)
void setZasTimestamp(bool timestamp)
void loadBar(int current, int total, int resolution, int width, std::string message="")
std::map< int, PndLmdSensorAligner >::iterator mapIt
TF1 * total
static bool writeMatrix(Matrix &mat, std::string filename)
static boost::property_tree::ptree readConfigFile(std::string filename)
Definition: matrix.h:50
void setMaxThreads(int NmaxThreads=0)
static std::string makeMatrixFileName(int overlapId=0, bool incentimeters=true)
void verboseLevel(int verbose)
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)
void setBinaryPairFileDirectory(const std::string &directory)
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
static std::vector< std::vector< double > > readFromCSVFile(std::string filename)
const string filename