FairRoot/PandaRoot
convertRootMatricesToJSON.C
Go to the documentation of this file.
1 #include "json.hpp"
2 
3 using std::cout;
4 using std::string;
6 
7 std::map<std::string, TGeoHMatrix> *readRootMatrices(std::string filename) {
8  cout << "reding file: " << filename << "\n";
9  TFile *misalignmentMatrixRootfile = new TFile(filename.c_str(), "READ");
10  if (misalignmentMatrixRootfile->IsOpen()) {
11  std::map<std::string, TGeoHMatrix> *matrices;
12  gDirectory->GetObject("PndLmdMisalignMatrices", matrices);
13  misalignmentMatrixRootfile->Close();
14  cout << "read " << matrices->size() << " matrices from file.\n";
15  return matrices;
16  } else {
17  cout << "file could not be read\n";
18  exit(1);
19  }
20 }
21 
22 void saveMatricesToJson(std::map<std::string, TGeoHMatrix> matrices, std::string outfilename) {
23  // matrices is a map with string->TGeoHMatrix
24  json j;
25  double thisMatrixDoubles[16];
26  TGeoHMatrix thisMatrix;
27 
28  for (auto &path : matrices) {
29  thisMatrix = path.second;
30  thisMatrix.GetHomogenousMatrix(thisMatrixDoubles);
31 
32  j[path.first] = {
33  thisMatrixDoubles[0], thisMatrixDoubles[1], thisMatrixDoubles[2], thisMatrixDoubles[12],
34  thisMatrixDoubles[4], thisMatrixDoubles[5], thisMatrixDoubles[6], thisMatrixDoubles[13],
35  thisMatrixDoubles[8], thisMatrixDoubles[9], thisMatrixDoubles[10], thisMatrixDoubles[14],
36  thisMatrixDoubles[3], thisMatrixDoubles[7], thisMatrixDoubles[11], thisMatrixDoubles[15]};
37  }
38 
39  // save!
40  cout << "save " << j.size() << " matrices to json!\n";
41  std::ofstream o(outfilename.c_str());
42  o << std::setw(2) << j << std::endl;
43 }
44 
47  cout << "starting...\n";
48 
49  // read root file, this will be a map<TString, TGeoHMatrix>
50  std::map<std::string, TGeoHMatrix> rootMatrices = *(readRootMatrices(filename));
51 
52  // save file to json
53  cout << "saving " << filename << " to json file!\n";
54  saveMatricesToJson(rootMatrices, filename + ".json");
55 }
56 
57 // dummy function in case someone calls the macro without argument
59  cout << "You must specify a file name!\n";
60  exit(1);
61 }
exit(0)
std::map< std::string, TGeoHMatrix > * readRootMatrices(std::string filename)
void saveMatricesToJson(std::map< std::string, TGeoHMatrix > matrices, std::string outfilename)
nlohmann::json json
void convertRootMatricesToJSON(std::string filename)
main function
const string filename