Jpp  18.0.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JTTS.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <iomanip>
4 #include <fstream>
5 #include <vector>
6 #include <cmath>
7 #include <limits>
8 
9 #include "TROOT.h"
10 #include "TFile.h"
11 #include "TString.h"
12 #include "TRegexp.h"
13 #include "TGraph.h"
14 #include "TGraphErrors.h"
15 #include "TGraphSmooth.h"
16 #include "TF1.h"
17 #include "TH1D.h"
18 #include "TSpline.h"
19 
20 #include "Jeep/JPrint.hh"
21 #include "Jeep/JParser.hh"
22 #include "Jeep/JMessage.hh"
23 
24 
25 /**
26  * \file
27  *
28  * Auxiliary program to model transition time distribution generator from data.
29  * \author mdejong
30  */
31 int main(int argc, char **argv)
32 {
33  using namespace std;
34 
35  string inputFile;
36  string outputFile;
37  Double_t bass;
38  Double_t span;
39  Double_t xbin;
40  int numberOfBins;
41  string pdf;
42  string cdf;
43  int debug;
44 
45  try {
46 
47  JParser<> zap("Auxiliary program to model transition time distribution generator from data.");
48 
49  zap['f'] = make_field(inputFile);
50  zap['o'] = make_field(outputFile) = "";
51  zap['B'] = make_field(bass) = 0.0;
52  zap['S'] = make_field(span) = 0.0;
53  zap['x'] = make_field(xbin) = 1.0; // [ns]
54  zap['n'] = make_field(numberOfBins) = 1000;
55  zap['P'] = make_field(pdf) = "";
56  zap['C'] = make_field(cdf) = "";
57  zap['d'] = make_field(debug) = 1;
58 
59  zap(argc, argv);
60  }
61  catch(const exception &error) {
62  FATAL(error.what() << endl);
63  }
64 
65 
66  const TRegexp regexp("V[0-9]+");
67 
68  TString buffer(inputFile.c_str());
69 
70  Ssiz_t len = 0;
71  Ssiz_t pos = buffer.Index(regexp, &len);
72 
73  int version = 0;
74 
75  if (pos != -1) {
76  buffer = buffer(pos+1, len-1);
77  version = buffer.Atoi();
78  }
79 
80  NOTICE("File version " << version << endl);
81 
82 
83  typedef vector<Double_t> JVector_t;
84 
85  JVector_t X;
86  JVector_t Y;
87  JVector_t EX;
88  JVector_t EY;
89 
90  ifstream in(inputFile.c_str());
91 
92  if (version == 0) {
93 
94  for (Double_t x = 0.0, y; in >> y; x += xbin) {
95  X .push_back(x);
96  Y .push_back(y);
97  EX.push_back(0.0);
98  EY.push_back(sqrt(y));
99  }
100 
101  } else {
102 
103  in.ignore(numeric_limits<streamsize>::max(), '\n');
104 
105  for (Double_t x, y, dy, rms; in >> x >> y >> dy >> rms; ) {
106  X .push_back(x);
107  Y .push_back(y);
108  EX.push_back(0.0);
109  EY.push_back(dy);
110  }
111  }
112 
113  in.close();
114 
115 
116  int N = X.size();
117 
118  if (N < 25) {
119  FATAL("Not enough points." << endl);
120  }
121 
122  if (version != 0) {
123 
124  xbin = (X[N-1] - X[0]) / (N - 1);
125 
126  NOTICE("Bin width [ns] " << xbin << endl);
127  }
128 
129  // Histogram for log-likelihood fit
130 
131  TH1D h1("h1", NULL, N, X[0] - 0.5*xbin, X[N-1] + 0.5*xbin);
132 
133  h1.Sumw2();
134 
135  for (int i = 0; i != N; ++i) {
136  h1.SetBinContent(i+1, Y [i]);
137  h1.SetBinError (i+1, EY[i]);
138  }
139 
140 
141  // Fit function
142 
143  TF1 f1("f1", "[0]*exp(-0.5*(x-[1])*(x-[1])/([2]*[2])) + [3]");
144 
145 
146  // start values
147 
148  Double_t ymax = 0.0;
149  Double_t x0 = 0.0;
150  Double_t sigma = 2.0;
151  Double_t ymin = 0.0;
152 
153  for (int i = 0; i != N; ++i) {
154  if (Y[i] > ymax) {
155  ymax = Y[i];
156  x0 = X[i];
157  }
158  }
159 
160  f1.SetParameter(0, ymax);
161  f1.SetParameter(1, x0);
162  f1.SetParameter(2, sigma);
163  f1.SetParameter(3, ymin);
164 
165 
166  h1.Fit(&f1, "LL", "same", x0 - 5 * sigma, x0 + 5 * sigma);
167 
168 
169  // center data at position of Gauss
170 
171  ymax = f1.GetParameter(0);
172  x0 = f1.GetParameter(1);
173  sigma = f1.GetParameter(2);
174  ymin = f1.GetParameter(3);
175 
176  if (version == 0) {
177 
178  for (JVector_t::iterator x = X.begin(); x != X.end(); ++x) {
179  *x -= x0;
180  }
181 
182  f1.SetParameter(1, x0 = 0);
183 
184  if (ymin <= 0.0) {
185  f1.SetParameter(3, 1e-4 * ymax); // avoid division by zero
186  }
187  }
188 
189  // count pre- and delayed pulses
190 
191  Double_t yy = 0.0;
192  Double_t yl = 0.0;
193  Double_t yr = 0.0;
194 
195  for (int i = 0; i != N; ++i) {
196 
197  const Double_t x = X[i];
198  const Double_t y = Y[i];
199 
200  yy += y;
201 
202  if (x < x0 - 5.0 * sigma)
203  yl += y;
204  else if (x > x0 + 5.0 * sigma)
205  yr += y;
206  }
207 
208  NOTICE("Number of pulses: " << yy << endl);
209  NOTICE("Pre-pulses: " << yl / yy << endl);
210  NOTICE("Delayed pulses: " << yr / yy << endl);
211 
212  if (version == 0) {
213 
214  // skip leading and trailing zeros for smoothing operation
215 
216  int n1 = 0;
217  int n2 = N - 1;
218 
219  while (n1 != N - 1 && Y[n1] == 0) ++n1;
220  while (n2 != 0 && Y[n2] == 0) --n2;
221 
222  //if (n1 != 0 ) --n1;
223  //if (n2 != N - 1) ++n2;
224 
225  if (n2 <= n1) {
226  FATAL("No non-zero data." << endl);
227  }
228 
229  X = JVector_t(X .data() + n1, X .data() + n2);
230  Y = JVector_t(Y .data() + n1, Y .data() + n2);
231  EX = JVector_t(EX.data() + n1, EX.data() + n2);
232  EY = JVector_t(EY.data() + n1, EY.data() + n2);
233 
234  N = X.size();
235  }
236 
237  TGraphErrors g0(N, X.data(), Y.data(), EX.data(), EY.data());
238 
239  g0.SetName("g0");
240 
241  TGraphErrors g1(g0); // copy
242 
243  g1.SetName("g1");
244 
245 
246  if (version == 0) {
247 
248  // divide TGraph data by TF1 fit function
249 
250  for (int i = 0; i != N; ++i) {
251 
252  const Double_t x = g1.GetX()[i];
253  const Double_t f = f1.Eval(x);
254 
255  g1.GetY() [i] /= f;
256  g1.GetEY()[i] /= f;
257  }
258 
259  // smooth TGraph
260 
261  TGraphSmooth gs("gs");
262 
263  TGraph* gout = gs.SmoothSuper(&g1, "", bass, span, false, g1.GetEY());
264 
265 
266  // copy back results
267 
268  for (int i = 0; i != N; ++i) {
269  g1.GetY()[i] = gout->GetY()[i];
270  }
271 
272 
273  // multiply TGraph data by TF1 fit function
274 
275  for (int i = 0; i != N; ++i) {
276 
277  const Double_t x = g1.GetX()[i];
278  const Double_t f = f1.Eval(x);
279 
280  g1.GetY() [i] *= f;
281  g1.GetEY()[i] *= f;
282  }
283  }
284 
285  if (pdf != "") {
286 
287  TGraph g2(g1);
288 
289  Double_t W = 0.0;
290 
291  for (int i = 0; i != N; ++i) {
292  W += g2.GetY()[i];
293  }
294 
295  W *= xbin;
296 
297  for (int i = 0; i != N; ++i) {
298  g2.GetY()[i] /= W;
299  }
300 
301  ofstream out(pdf.c_str());
302 
303  const Double_t xmin = g2.GetX()[ 0 ];
304  const Double_t xmax = g2.GetX()[N-1];
305 
306  for (Double_t x = xmin, dx = (xmax - xmin) / numberOfBins; x <= xmax + 0.5*dx; x += dx) {
307 
308  out << "\t(*this)"
309  << "["
310  << showpos << FIXED(7,2) << x
311  << "] = "
312  << noshowpos << FIXED(6,4) << g2.Eval(x)
313  << ";" << endl;
314  }
315 
316  out.close();
317  }
318 
319  if (cdf != "") {
320 
321  TGraph g2(g1);
322 
323  for (int i = 0, j = 1; j != N; ++i, ++j) {
324  g2.GetY()[j] += g2.GetY()[i];
325  }
326 
327  const Double_t ymin = g2.GetY()[ 0 ];
328  const Double_t ymax = g2.GetY()[N-1];
329 
330  for (int i = 0; i != N; ++i) {
331 
332  const Double_t x = g2.GetX()[i];
333  const Double_t y = g2.GetY()[i];
334 
335  g2.GetX()[i] = (y - ymin) / ymax;
336  g2.GetY()[i] = x + 0.5 * xbin;
337  }
338 
339  ofstream out(cdf.c_str());
340 
341  const Double_t xmin = 0.0;
342  const Double_t xmax = 1.0;
343 
344  for (Double_t x = xmin, dx = (xmax - xmin) / numberOfBins; x <= xmax + 0.5*dx; x += dx) {
345 
346  out << "\t(*this)"
347  << "["
348  << noshowpos << FIXED(6,4) << x
349  << "] = "
350  << showpos << FIXED(9,5) << g2.Eval(x)
351  << ";" << endl;
352  }
353 
354  out.close();
355  }
356 
357 
358  if (outputFile != "") {
359 
360  // copy histogram centered at peak position
361 
362  const Double_t xmin = X[ 0 ];
363  const Double_t xmax = X[N-1];
364  const Double_t dx = (xmax - xmin) / N;
365 
366  TH1D h2("pmt", NULL, N, xmin - 0.5*dx, xmax + 0.5*dx);
367 
368  h2.Sumw2();
369 
370  Double_t W = 0.0;
371 
372  for (int i = 0; i != N; ++i) {
373  W += Y[i];
374  }
375 
376  W *= dx;
377 
378  for (int i = 0; i != N; ++i) {
379 
380  h2.SetBinContent(i+1, Y [i]/W);
381  h2.SetBinError (i+1, EY[i]/W);
382 
383  g1.GetY() [i] /= W;
384  g1.GetEY()[i] /= W;
385  }
386 
387  TFile out(outputFile.c_str(), "recreate");
388 
389  h1.Write();
390  h2.Write();
391  g0.Write();
392  g1.Write();
393 
394  out.Write();
395  out.Close();
396  }
397 }
const double xmax
Definition: JQuadrature.cc:24
Utility class to parse command line options.
Definition: JParser.hh:1514
int main(int argc, char *argv[])
Definition: Main.cc:15
version
Definition: JEditTuneHV.sh:5
o $QUALITY_ROOT d $DEBUG!CHECK_EXIT_CODE JPlot1D f
Definition: JDataQuality.sh:76
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
const double sigma[]
Definition: JQuadrature.cc:74
const JPolynome f1(1.0, 2.0, 3.0)
Function.
I/O formatting auxiliaries.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
#define NOTICE(A)
Definition: JMessage.hh:64
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
then for FUNCTION in pdf npe cdf
Definition: JPlotNPE-PDG.sh:73
const double xmin
Definition: JQuadrature.cc:23
then usage $script< input file >[option[primary[working directory]]] nWhere option can be N
Definition: JMuonPostfit.sh:40
Utility class to parse command line options.
no fit printf nominal n $STRING awk v X
int j
Definition: JPolint.hh:703
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:46
int numberOfBins
number of bins for average CDF integral of optical module
Definition: JSirene.cc:69
int debug
debug level
Double_t g1(const Double_t x)
Function.
Definition: JQuantiles.cc:25