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/asio.hpp>
18 #include <boost/asio/io_service.hpp>
19 #include <boost/thread/mutex.hpp>
20 #include <boost/shared_ptr.hpp>
21 #include <boost/thread.hpp>
22 #include <boost/property_tree/ptree.hpp>
23 #include <boost/property_tree/json_parser.hpp>
24 
25 #include <PndLmdAlignStructs.h>
26 #include <PndLmdSensorAligner.h>
27 
28 #include <TGeoMatrix.h>
29 
30 #include <matrix.h>
31 #include <string>
32 #include <sstream>
33 
35 
36  typedef std::map<int, PndLmdSensorAligner>::iterator mapIt;
37 
38 private:
39 
40  // PndLmdSensorAligner needs private functions from here
41  friend class PndLmdSensorAligner;
42  friend class PndLmdAlignQA;
43 
44  //loadBarMultiThreaded
45  int _i, _r, _w, _n;
46 
48 
49  std::stringstream _info;
50 
51  std::map<int, PndLmdSensorAligner> aligners;
52  std::map<int, bool> alignersFull;
53 
55  std::vector<std::string> fileNames;
60 
61  vector<int> overlapIDs;
62 
63  //FIXME: remove
64  // we don't want that here anymore
65  //PndLmdGeometryHelper *helper;
66 
67  void alignST();
68  void alignMT();
69 
70  void alignOne(PndLmdSensorAligner &aligner);
71  void resetMTLB(int n, int r, int w);
72  void incrementMTLB();
73  void checkIOpaths();
74 
75  //don't use, doesn't work with root like that
76  static void readPairsFromChainMT(std::vector<std::string> files,
77  std::map<int, PndLmdSensorAligner> &aligners, PndLmdAlignManager &manager);
78 
79  //produces matrices 0 -> 1,2,3,4,5,6,7,8,9
80  //Matrix combineMatrix(int id1, int id2);
81 
82  //produces matrices i -> ... -> i (should be almost identity matrix)
83  //Matrix combineCyclicMatrix(int id);
84 
85  //generate the file name of a matrix or pair file, so changes must only be made once
86  static std::string makeBinaryPairFileName(int overlapId = 0, bool incentimeters = true);
87  static std::string makeMatrixFileName(int overlapId = 0, bool incentimeters = true);
88 
89  void verbosePrint(std::string input, int level = 3);
90 
91 public:
92 
93  //An empty AlignManager will use standard values. If you want to reuse a manager, call Init() again and set values.
95  virtual ~PndLmdAlignManager();
96 
97  // initializes Manager on construction or RESETS every value to default
98  void init();
99 
100  // returns true if successful or false if aligner has enough pairs
101  //bool addPair(PndLmdHitPair &pair);
102 
103  //adds pairs just like the other function but starts an aligner if it is full
105 
106  //add filename, so the aligner adds the pairs itself
107  bool addFile(std::string filename);
108  //void readFiles(); // deprecated
109  void readFilesAndAlign();
110  void waitForCompletion();
111 
112  //don't use, doesn't work with root like this
113  //void readFilesMT();
114 
117  bool checkForBinaryFiles();
118  bool checkForLmdMatrixFiles();
119  void clearPairs();
120 
121  //add all pair files that can be found in directory, up to a maximum of maxFiles
122  //returns number of files found (including 0 for no files) or -1 if "pretend" option is set
123  //set maxFiles=0 for all available files
124  int addFilesFromDirectory(std::string directory, int maxFiles = 0);
125 
126  void validate();
127 
128  //perform last checks and run calculations on all aligners
129  void alignAllSensors();
130 
131  static void loadBar(int current, int total, int resolution, int width, std::string message = "");
132 
133  void setMaxPairs(int maxPairs);
134 
135  //considers inactive area, guard rings, pixel size etc
136  //TODO: remove, replace, deprecate, return
137  //static Matrix transformMatrixFromPixelsToCm(const Matrix &input);
138 
139  static Matrix castTGeoHMatrixToMatrix(const TGeoHMatrix &matrix);
140 
141  //static Matrix getPixelToCentimeterTransformation();
142  //Matrix getMatrixSensorToSensor(int fromSensor, int toSensor);
143 
144  // read and write matrix files to and from disk
145  static Matrix readMatrix(std::string filename);
146  static bool writeMatrix(Matrix &mat, std::string filename);
147 
148  void setSingleAligner(bool singleAligner) {
149  _singleAligner = singleAligner;
150  }
151 
152  void setOutFilename(const std::string& filename) {
154  checkIOpaths();
155  }
156 
157  static std::stringstream* readFile(std::string filename);
158  static std::vector<std::vector<double> > readFromCSVFile(std::string filename);
159 
160  /*
161  * recursively searches files by extension in curr_directory and adds to list
162  * finds all extensions by default, but you can specify which extension
163  * (or part of filename) you want
164  */
165  static int searchFiles(std::string curr_directory, std::vector<std::string> &list,
166  std::string extension = "", bool includeSubDirs = true);
167 
168  //clear console
169  static void clearScreen();
170 
171  //searches directories in curr_directory, adds to list
172  static int searchDirectories(std::string curr_directory, std::vector<std::string> &list,
173  bool includeSubDirs = true);
174  static bool mkdir(std::string path);
175  static bool exists(std::string file);
176  static std::vector<std::string> findRegex(std::string source, std::string regex);
177 
178  void setMatrixOutDir(std::string matrixOutDir) {
179  _matrixOutDir = matrixOutDir;
180  }
181 
182  /*
183  * set this BEFORE reading pair files. if inCentimeters==true, then hits will be decoded by PndLmdDim to centimeters
184  * and all calculations will be done in cm. Misalginment matrices are close to identity matrix, which could introduce
185  * numerical errors.
186  * If inCentimeters==false, then the ICP will work directly on row/col values and matrix will be in pixels. AlignManager
187  * converts matrix to cm, but the resultant matrix is the product of misalignment*ideal!
188  */
189  void setInCentimeters(bool inCentimeters);
190  void setZasTimestamp(bool timestamp);
191 
192  //when supplied with a function object, this function executes in a new thread
193  void workerThread(boost::shared_ptr<boost::asio::io_service> io_service);
194 
195  //write config file
196  static bool writeConfigFile(boost::property_tree::ptree configTree, std::string filename,
197  bool replaceExisting = true);
198 
199  //read json config file
200  static boost::property_tree::ptree readConfigFile(std::string filename);
201 
202  void setBinaryPairFileDirectory(const std::string& binaryPairFileDirectory) {
203  _binaryPairFileDirectory = binaryPairFileDirectory;
204  }
205 
206  void setStartAlignerWhenFull(bool value) {
207  startAlignerWhenFull = value;
208  }
209 
210  void verboseLevel(int verbose) {
212  for (auto &a : aligners) {
213  a.second.verboseLevel(_verboseLevel);
214  }
215  }
216 };
217 
218 #endif /* LMD_LMDSENSORALIGNMENT_PNDLMDALIGNMANAGER_H_ */
std::vector< std::string > fileNames
bool addFile(std::string filename)
void setInCentimeters(bool inCentimeters)
double r
Definition: RiemannTest.C:14
TFile * file
void setOutFilename(const std::string &filename)
#define verbose
std::map< int, bool > alignersFull
int n
static int searchDirectories(std::string curr_directory, std::vector< std::string > &list, bool includeSubDirs=true)
void setStartAlignerWhenFull(bool value)
static int searchFiles(std::string curr_directory, std::vector< std::string > &list, std::string extension="", bool includeSubDirs=true)
void setBinaryPairFileDirectory(const std::string &binaryPairFileDirectory)
std::map< int, PndLmdSensorAligner > aligners
PndLmdAlignManager manager
Definition: PndLmdAlignQA.h:42
std::map< int, TString > files
Definition: simubg.C:28
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 resetMTLB(int n, int r, int w)
static std::stringstream * readFile(std::string filename)
static void loadBar(int current, int total, int resolution, int width, std::string message="")
static void readPairsFromChainMT(std::vector< std::string > files, std::map< int, PndLmdSensorAligner > &aligners, PndLmdAlignManager &manager)
std::string _binaryPairFileDirectory
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 setMatrixOutDir(std::string matrixOutDir)
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 workerThread(boost::shared_ptr< boost::asio::io_service > io_service)
std::stringstream _info
static Matrix readMatrix(std::string filename)
TString directory
int addFilesFromDirectory(std::string directory, int maxFiles=0)
void setMaxPairs(int maxPairs)
bool addPairAndStartAligner(PndLmdHitPair &pair)
vector< int > overlapIDs
static std::vector< std::vector< double > > readFromCSVFile(std::string filename)
void alignOne(PndLmdSensorAligner &aligner)
const string filename