Jpp  pmt_effective_area_update_2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JTwist.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <iomanip>
3 #include <cmath>
4 #include <vector>
5 
6 #include "TROOT.h"
7 #include "TFile.h"
8 #include "TH1D.h"
9 #include "TRandom3.h"
10 
11 #include "JLang/JException.hh"
12 #include "JMath/JConstants.hh"
14 #include "JGeometry3D/JEigen3D.hh"
16 #include "JCompass/JHit.hh"
17 #include "JCompass/JModel.hh"
18 #include "JFit/JSimplex.hh"
19 
20 #include "Jeep/JParser.hh"
21 #include "Jeep/JMessage.hh"
22 
23 
24 /**
25  * Test program for fit of quaternions.
26  */
27 int main(int argc, char**argv)
28 {
29  using namespace std;
30  using namespace JPP;
31 
32  string outputFile;
33  int numberOfEvents;
34  double X;
35  double Y;
36  double Z;
37  double alpha;
38  JPosition3D sigma;
39  UInt_t seed;
40  bool fit;
41  int debug;
42 
43  try {
44 
45  JParser<> zap("Test program for fit of quaternions.");
46 
47  zap['o'] = make_field(outputFile) = "twist.root";
48  zap['n'] = make_field(numberOfEvents) = 1000;
49  zap['x'] = make_field(X, "tilt angle around x-axis [deg]") = 0.0;
50  zap['y'] = make_field(Y, "tilt angle around y-axis [deg]") = 0.0;
51  zap['z'] = make_field(Z, "tilt angle around z-axis [deg]") = 0.0;
52  zap['a'] = make_field(alpha, "twist angle [deg/m]");
53  zap['s'] = make_field(sigma, "(x,y,z) resolution [deg]");
54  zap['S'] = make_field(seed) = 0;
55  zap['F'] = make_field(fit, "enable fit");
56  zap['d'] = make_field(debug) = 2;
57 
58  zap(argc, argv);
59  }
60  catch(const exception &error) {
61  FATAL(error.what() << endl);
62  }
63 
64 
65  gRandom->SetSeed(seed);
66 
67 
68  TFile out(outputFile.c_str(), "recreate");
69 
70  TH1D hc("chi2/NDF", NULL, 100, 0.0, 2.0);
71  TH1D h0("tilt", NULL, 100, 0.0, 2.0);
72  TH1D h1("twist", NULL, 100, 0.0, 0.1);
73 
74 
75  // model
76 
77  const JModel model(JQuaternion3D(JQuaternion3X(X * PI / 180.0),
78  JQuaternion3Y(Y * PI / 180.0),
79  JQuaternion3Z(Z * PI / 180.0)),
80  JQuaternion3D(JQuaternion3Z(alpha * PI / 180.0)));
81 
82  STATUS("Q0 " << FIXED(12,8) << model.Q0 << endl);
83  STATUS("Q1 " << FIXED(12,8) << model.Q1 << endl);
84 
85 
86  JSimplex<JModel> simplex;
87 
89 
90  simplex.debug = debug;
91 
92  const JChi2 getChi2(EM_NORMAL);
93 
94 
95  for (int counter = 0; counter != numberOfEvents; ++counter) {
96 
97  STATUS("event: " << setw(10) << counter << '\r'); DEBUG(endl);
98 
99  // generate data
100 
101  const int N = 18; // number of floors
102  const double z1 = 30.0; // height of first floor
103  const double dz = 10.0; // distance between floors
104  const double eps = 1.0e-6; // minimal sigma
105 
106  vector<JHit> data;
107 
108  for (int i = 1; i <= N; ++i) {
109 
110  const double z = z1 + (i - 1) * dz; // height of floor i
111 
112  data.push_back(JHit(i, z, model(z), max(eps, sigma.getLength())));
113  }
114 
115  // smearing
116 
117  for (vector<JHit>::iterator i = data.begin(); i != data.end(); ++i) {
118  i->mul(JQuaternion3D(JQuaternion3X(gRandom->Gaus(0.0, sigma.getX()) * PI / 180.0),
119  JQuaternion3Y(gRandom->Gaus(0.0, sigma.getY()) * PI / 180.0),
120  JQuaternion3Z(gRandom->Gaus(0.0, sigma.getZ()) * PI / 180.0)));
121  }
122 
123  for (vector<JHit>::const_iterator i = data.begin(); i != data.end(); ++i) {
124  DEBUG(FIXED(4,1) << i->getZ() << ' ' << FIXED(12,8) << i->getQuaternion() << endl);
125  }
126 
127 
128  JModel result(data.begin(), data.end()); // prefit
129 
130  DEBUG("R0 " << FIXED(12,8) << result.Q0 << ' ' << getAngle(model.Q0, result.Q0) << endl);
131  DEBUG("R1 " << FIXED(12,8) << result.Q1 << ' ' << getAngle(model.Q1, result.Q1) << endl);
132 
133  if (fit) {
134 
135  simplex.value = result; // start value
136 
137  simplex.step.resize(4);
138 
139  simplex.step[0] = JModel(JQuaternion3X(0.50 * PI / 180.0), JQuaternion3D::getIdentity());
140  simplex.step[1] = JModel(JQuaternion3Y(0.50 * PI / 180.0), JQuaternion3D::getIdentity());
141  simplex.step[2] = JModel(JQuaternion3Z(0.50 * PI / 180.0), JQuaternion3D::getIdentity());
142  simplex.step[3] = JModel(JQuaternion3D::getIdentity(), JQuaternion3Z(0.05 * PI / 180.0));
143 
144  const double chi2 = simplex(getChi2, data.begin(), data.end());
145  const int ndf = data.size() * 4 - simplex.step.size();
146 
147  DEBUG("number of iterations " << simplex.numberOfIterations << endl);
148 
149  result = simplex.value; // final value
150 
151  DEBUG("chi2 " << chi2 << '/' << ndf << endl);
152  DEBUG("R0 " << FIXED(12,8) << simplex.value.Q0 << ' ' << getAngle(model.Q0, result.Q0) << endl);
153  DEBUG("R1 " << FIXED(12,8) << simplex.value.Q1 << ' ' << getAngle(model.Q1, result.Q1) << endl);
154 
155  hc.Fill(chi2 / ndf);
156  }
157 
158  //h0.Fill(getAngle(model.Q0, result.Q0));
159  h0.Fill(getAngle(JDirection3D(0.0, 0.0, 1.0).rotate(model .Q0),
160  JDirection3D(0.0, 0.0, 1.0).rotate(result.Q0)));
161  h1.Fill(getAngle(model.Q1, result.Q1));
162  }
163  STATUS(endl);
164 
165  out.Write();
166  out.Close();
167 }
Utility class to parse command line options.
Definition: JParser.hh:1500
double getAngle(const JQuaternion3D &first, const JQuaternion3D &second)
Get space angle between quanternions.
Exceptions.
int main(int argc, char *argv[])
Definition: Main.cc:15
#define STATUS(A)
Definition: JMessage.hh:63
then for HISTOGRAM in h0 h1
Definition: JMatrixNZ.sh:71
then fatal Wrong number of arguments fi set_variable STRING $argv[1] set_variable DETECTORXY_TXT $WORKDIR $DETECTORXY_TXT tail read X Y CHI2 RMS printf optimum n $X $Y $CHI2 $RMS awk v Y
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
string outputFile
Mathematical constants.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
return result
Definition: JPolint.hh:727
then break fi done getCenter read X Y Z let X
static const double PI
Mathematical constants.
int debug
debug level
Definition: JSirene.cc:63
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
Utility class to parse command line options.
then usage $script< input_file >< detector_file > fi set_variable OUTPUT_DIR set_variable SELECTOR JDAQTimesliceL1 set_variable DEBUG case set_variable DEBUG
static int MAXIMUM_ITERATIONS
maximal number of iterations
Definition: JSimplex.hh:237
double getChi2(const double P)
Get chi2 corresponding to given probability.
Definition: JFitToolkit.hh:70
then usage $script[input file[working directory[option]]] nWhere option can be N
Definition: JMuonPostfit.sh:36