CMS 3D CMS Logo

GCP.cc
Go to the documentation of this file.
1 #include <cstdlib>
2 #include <iostream>
3 #include <vector>
4 
5 #include "TString.h"
6 
7 #include "exceptions.h"
8 #include "toolbox.h"
9 #include "Options.h"
10 
11 #include "boost/regex.hpp"
12 #include "boost/filesystem.hpp"
13 #include "boost/algorithm/string.hpp"
14 #include "boost/container/vector.hpp"
15 #include "boost/property_tree/ptree.hpp"
16 #include "boost/property_tree/json_parser.hpp"
17 
19 #include "Alignment/OfflineValidation/scripts/visualizationTracker.C"
20 #include "Alignment/OfflineValidation/macros/makeArrowPlots.C"
21 
22 // for debugging
23 #include "TObject.h"
24 
25 using namespace std;
26 using namespace AllInOneConfig;
27 
28 namespace pt = boost::property_tree;
29 namespace fs = boost::filesystem;
30 namespace bc = boost::container;
31 
32 void comparisonScript(pt::ptree GCPoptions,
33  TString inFile, //="mp1510_vs_mp1509.Comparison_commonTracker.root", // TODO: get ROOT file
34  TString outDir = "outputDir/",
35  TString alignmentName = "Alignment",
36  TString referenceName = "Ideal") {
37  // the output directory is created if it does not exist
38  fs::create_directories(outDir.Data());
39 
40  TString modulesToPlot = "all";
41  TString transDir = outDir + "/Translations";
42  TString rotDir = outDir + "/Rotations";
43  fs::create_directories(transDir.Data());
44  fs::create_directories(rotDir.Data());
45 
46  bool plotOnlyGlobal = GCPoptions.count("plotOnlyGlobal") ? GCPoptions.get<bool>("plotOnlyGlobal") : false;
47  bool plotPng = GCPoptions.count("plotPng") ? GCPoptions.get<bool>("plotPng") : false;
48  bool makeProfilePlots = GCPoptions.count("makeProfilePlots") ? GCPoptions.get<bool>("makeProfilePlots") : true;
49 
50  // Plot Translations
52  inFile, transDir, modulesToPlot, alignmentName, referenceName, plotOnlyGlobal, makeProfilePlots, 0);
53  // x and y contain the couples to plot
54  // -> every combination possible will be performed
55  // /!\ always give units (otherwise, unexpected bug from root...)
56  vector<TString> x{"r", "phi", "z"};
57  vector<TString> y{"dr", "dz", "rdphi", "dx", "dy"};
58  vector<TString> xmean{"x", "y", "z", "r"};
59 
60  trans->SetBranchUnits("x", "cm");
61  trans->SetBranchUnits("y", "cm");
62  trans->SetBranchUnits("z", "cm"); //trans->SetBranchMax("z", 100); trans->SetBranchMin("z", -100);
63  trans->SetBranchUnits("r", "cm");
64  trans->SetBranchUnits("phi", "rad");
65  trans->SetBranchUnits("dx", "#mum"); //trans->SetBranchMax("dx", 10); trans->SetBranchMin("dx", -10);
66  trans->SetBranchUnits("dy", "#mum"); //trans->SetBranchMax("dy", 10); trans->SetBranchMin("dy", -10);
67  trans->SetBranchUnits("dz", "#mum");
68  trans->SetBranchUnits("dr", "#mum");
69  trans->SetBranchUnits("rdphi", "#mum rad");
70 
71  trans->SetBranchSF("dx", 10000);
72  trans->SetBranchSF("dy", 10000);
73  trans->SetBranchSF("dz", 10000);
74  trans->SetBranchSF("dr", 10000);
75  trans->SetBranchSF("rdphi", 10000);
76 
77  trans->SetGrid(1, 1);
78  trans->MakePlots(x, y, GCPoptions); // default output is pdf, but png gives a nicer result, so we use it as well
79  // remark: what takes the more time is the creation of the output files,
80  // not the looping on the tree (because the code is perfect, of course :p)
81  if (plotPng) {
82  trans->SetPrintOption("png");
83  trans->MakePlots(x, y, GCPoptions);
84  }
85 
86  trans->MakeTables(xmean, y, GCPoptions);
87 
88  // Plot Rotations
90  inFile, rotDir, modulesToPlot, alignmentName, referenceName, plotOnlyGlobal, makeProfilePlots, 2);
91  // x and y contain the couples to plot
92  // -> every combination possible will be performed
93  // /!\ always give units (otherwise, unexpected bug from root...)
94  vector<TString> b{"dalpha", "dbeta", "dgamma"};
95 
96  rot->SetBranchUnits("z", "cm");
97  rot->SetBranchUnits("r", "cm");
98  rot->SetBranchUnits("phi", "rad");
99  rot->SetBranchUnits("dalpha", "mrad");
100  rot->SetBranchUnits("dbeta", "mrad");
101  rot->SetBranchUnits("dgamma", "mrad");
102 
103  rot->SetBranchSF("dalpha", 1000);
104  rot->SetBranchSF("dbeta", 1000);
105  rot->SetBranchSF("dgamma", 1000);
106 
107  rot->SetGrid(1, 1);
108  rot->SetPrintOption("pdf");
109  rot->MakePlots(x, b, GCPoptions);
110  if (plotPng) {
111  rot->SetPrintOption("png");
112  rot->MakePlots(x, b, GCPoptions);
113  }
114 
115  delete trans;
116  delete rot;
117 }
118 
119 void vizualizationScript(TString inFile, TString outDir, TString alignmentName, TString referenceName) {
120  TString outputFileName = outDir + "/Visualization";
121  fs::create_directories(outputFileName.Data());
122  //title
123  std::string line1 = alignmentName.Data();
124  std::string line2 = referenceName.Data();
125  //set subdetectors to see
126  int subdetector1 = 1;
127  int subdetector2 = 2;
128  //translation scale factor
129  int sclftr = 50;
130  //rotation scale factor
131  int sclfrt = 1;
132  //module size scale factor
133  float sclfmodulesizex = 1;
134  float sclfmodulesizey = 1;
135  float sclfmodulesizez = 1;
136  //beam pipe radius
137  float piperadius = 2.25;
138  //beam pipe xy coordinates
139  float pipexcoord = 0;
140  float pipeycoord = 0;
141  //beam line xy coordinates
142  float linexcoord = 0;
143  float lineycoord = 0;
144  runVisualizer(inFile,
145  outputFileName.Data(),
146  line1,
147  line2,
148  subdetector1,
149  subdetector2,
150  sclftr,
151  sclfrt,
152  sclfmodulesizex,
153  sclfmodulesizey,
154  sclfmodulesizez,
155  piperadius,
156  pipexcoord,
157  pipeycoord,
158  linexcoord,
159  lineycoord);
160 }
161 
162 int GCP(int argc, char* argv[]) {
163  /*
164  TObject* printer = new TObject();
165  printer->Info("GCPvalidation", "Hello!");
166  // Hack to push through messages even without -v running
167  // Very ugly coding, to run with std::cout -> run with -v option (GCP cfg.json -v)
168  */
169 
170  // parse the command line
172  options.helper(argc, argv);
173  options.parser(argc, argv);
174 
175  std::cout << " ----- GCP validation plots -----" << std::endl;
176  std::cout << " --- Digesting configuration" << std::endl;
177 
178  pt::ptree main_tree;
179  pt::read_json(options.config, main_tree);
180 
181  pt::ptree alignments = main_tree.get_child("alignments");
182  pt::ptree validation = main_tree.get_child("validation");
183 
184  pt::ptree GCPoptions = validation.get_child("GCP");
185 
186  // Disable some of the features for unit tests
187  bool doUnitTest = GCPoptions.count("doUnitTest") ? GCPoptions.get<bool>("doUnitTest") : false;
188 
189  // If useDefaultRange, update ranges if not defined in GCPoptions
190  bool useDefaultRange = GCPoptions.count("useDefaultRange") ? GCPoptions.get<bool>("useDefaultRange") : false;
191  if (useDefaultRange) {
192  // Read default ranges
193  pt::ptree default_range;
194  bc::vector<fs::path> possible_base_paths;
195  boost::split(possible_base_paths, std::getenv("CMSSW_SEARCH_PATH"), boost::is_any_of(":"));
196  fs::path default_range_path = "";
197  fs::path default_range_file = "Alignment/OfflineValidation/data/GCP/GCP_defaultRange.json";
198  for (const fs::path& path : possible_base_paths) {
199  if (fs::exists(path / default_range_file)) {
200  default_range_path = path / default_range_file;
201  }
202  }
203  assert((fs::exists(default_range_path)) &&
204  "Check if 'Alignment/OfflineValidation/test/GCP_defaultRange.json' exists");
205  pt::read_json(default_range_path.c_str(), default_range);
206 
207  for (pair<string, pt::ptree> it : default_range) {
208  if (GCPoptions.count(it.first) < 1) {
209  GCPoptions.put(it.first, it.second.data());
210  }
211  }
212  }
213 
214  pt::ptree comAl = alignments.get_child("comp");
215  pt::ptree refAl = alignments.get_child("ref");
216 
217  // Read the options
218  TString inFile = main_tree.get<std::string>("output") + "/GCPtree.root";
219  TString outDir = main_tree.get<std::string>("output");
220  TString modulesToPlot = "all";
221  TString alignmentName = comAl.get<std::string>("title");
222  TString referenceName = refAl.get<std::string>("title");
223 
224  std::cout << " --- Running comparison script" << std::endl;
225  // Compare script
226  comparisonScript(GCPoptions, inFile, outDir, alignmentName, referenceName);
227 
228  if (!doUnitTest) {
229  std::cout << " --- Running visualization script" << std::endl;
230  // Visualization script
231  vizualizationScript(inFile, outDir, alignmentName, referenceName);
232  } else {
233  std::cout << " --- Skipping visualization script for unit test purpose" << std::endl;
234  }
235 
236  std::cout << " --- Running arrow plot script" << std::endl;
237  // Arrow plot
238  TString arrowDir = outDir + "/ArrowPlots";
239  makeArrowPlots(inFile.Data(), arrowDir.Data());
240 
241  std::cout << " --- Finished running GCP.cpp" << std::endl;
242  return EXIT_SUCCESS;
243 }
244 
245 #ifndef DOXYGEN_SHOULD_SKIP_THIS
246 int main(int argc, char* argv[]) { return exceptions<GCP>(argc, argv); }
247 #endif
void comparisonScript(pt::ptree GCPoptions, TString inFile, TString outDir="outputDir/", TString alignmentName="Alignment", TString referenceName="Ideal")
Definition: GCP.cc:32
int main(int argc, char *argv[])
Definition: GCP.cc:246
assert(be >=bs)
int GCP(int argc, char *argv[])
Definition: GCP.cc:162
double b
Definition: hdecay.h:118
void vizualizationScript(TString inFile, TString outDir, TString alignmentName, TString referenceName)
Definition: GCP.cc:119