Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JFitMultiplicity.cc
Go to the documentation of this file.
1 
2 #include <string>
3 #include <iostream>
4 #include <iomanip>
5 #include <map>
6 #include <cmath>
7 
8 #include "TROOT.h"
9 #include "TFile.h"
10 #include "TH1D.h"
11 #include "TH2D.h"
12 #include "TF2.h"
13 #include "TMath.h"
14 #include "TFitResult.h"
15 
16 #include "JTools/JConstants.hh"
19 
20 #include "JROOT/JRootToolkit.hh"
21 #include "JTools/JRange.hh"
22 #include "JSupport/JMeta.hh"
23 
25 #include "JCalibrate/JFitK40.hh"
26 
27 #include "JGizmo/JManager.hh"
28 
29 #include "Jeep/JPrint.hh"
30 #include "Jeep/JParser.hh"
31 #include "Jeep/JMessage.hh"
32 
33 const char* livetime_t = "LiveTime";
34 
35 /**
36  * \file
37  *
38  * Auxiliary program to fit multiplicity rates from JMonitorMultiplicity output.
39  * \author mlincett
40  */
41 
42 int main(int argc, char **argv) {
43  using namespace std;
44  using namespace JPP;
45 
46  string inputFile;
47  string outputFile;
48  string summaryFile;
49  string option;
50  int debug;
51  bool fitBackground;
52  bool nominalLiveTime;
53 
54  try {
55  JParser<> zap("Auxiliary program to fit multiplicity rates from JMonitorMultiplicity output.");
56 
57  zap['f'] = make_field(inputFile, "input file");
58  zap['o'] = make_field(outputFile, "output file") = "fitmultiplicity.root";
59  zap['O'] = make_field(option, "fitting option") = "Q";
60  zap['B'] = make_field(fitBackground, "get signal by fit and subtraction from background from the integral");
61  zap['L'] = make_field(nominalLiveTime, "use nominal live time from LiveTime histogram, instead of individual DOMs");
62  zap['d'] = make_field(debug) = 1;
63 
64 
65  zap(argc, argv);
66  }
67  catch(const exception &error) {
68  FATAL(error.what() << endl);
69  }
70 
71  TFile* in = TFile::Open(inputFile.c_str(), "exist");
72 
73  if (in == NULL || !in->IsOpen()) {
74  FATAL("File: " << inputFile << " not opened." << endl);
75  }
76 
77 
78  //----------------------------------------------------------
79  // general histograms
80  //----------------------------------------------------------
81 
82  const int nx = 1 + NUMBER_OF_PMTS;
83  const double xmin = -0.5;
84  const double xmax = nx - 0.5;
85 
86  typedef JManager<TString, TH1D> JManager_t;
87 
88  // inclusive
89  JManager_t HTF(new TH1D("%F", "", nx, xmin, xmax));
90  // exclusive
91  JManager_t HTFE(new TH1D("%FE", "", nx, xmin, xmax));
92 
93  HTF->Sumw2(kFALSE);
94 
95  //----------------------------------------------------------
96  // get overall normalization constants
97  //----------------------------------------------------------
98 
99  TH1D* hLiveTime = (TH1D*) in->Get(livetime_t);
100 
101  if (hLiveTime == NULL) {
102  FATAL("File " << inputFile << " does not contain livetime histogram." << endl);
103  }
104 
105  //----------------------------------------------------------
106  // initialise output file
107  //----------------------------------------------------------
108 
109  TFile out(outputFile.c_str(), "recreate");
110 
111  //----------------------------------------------------------
112  // loop over bins of livetime histogram
113  //----------------------------------------------------------
114 
115  TDirectory *fdir = out.mkdir("FIT");
116  TDirectory *pdir = out.mkdir("MUL");
117 
118  // nominal live time
119  double nT = hLiveTime->GetBinContent(1);
120 
121  for (int bin = 2; bin <= hLiveTime->GetNbinsX(); bin++) {
122 
123  TString prefix = hLiveTime->GetXaxis()->GetBinLabel(bin);
124 
125  DEBUG("Processing data from " << prefix << endl);
126 
127  double T = nominalLiveTime ? nT : hLiveTime->GetBinContent(bin);
128 
129  TString h1D_label = prefix + TString("_P" );
130  TString h2D_label = prefix + TString("_HT");
131 
132  if (T > 0) {
133 
134  TH2D* h1D = (TH2D*) in->Get(h1D_label);
135  TH2D* h2D = (TH2D*) in->Get(h2D_label);
136 
137  if (h1D != NULL) {
138 
139  h1D->Scale(1.0 / T);
140 
141  pdir->cd();
142 
143  h1D->Write();
144 
145  } else {
146  DEBUG(h1D_label << " not found" << endl);
147  }
148 
149  if (h2D != NULL) {
150 
151  TDirectory* cdir = fdir->mkdir(h2D_label); cdir->cd();
152 
153  for (int M = 2; M <= NUMBER_OF_PMTS; M++) {
154 
155  int Mbin = h2D->GetXaxis()->FindBin(M);
156 
157  TH1D* hI = h2D->ProjectionY(h2D_label + Form("_%d", M), Mbin, NUMBER_OF_PMTS);
158 
159  hI->Sumw2();
160 
161  if (hI->GetEntries() > 0) {
162 
163  const double W = (hI->GetXaxis()->GetXmax() - hI->GetXaxis()->GetXmin());
164  const double N = hI->GetNbinsX();
165  const double V = W / N;
166  const double minRMS = 0.1;
167 
168  double inclusiveCountVal = 0, inclusiveCountErr = 0;
169 
170  if (hI->GetRMS() > minRMS) {
171 
172  hI->Scale(1.0 / V);
173 
174  TF1 f1("f1", "[0]*exp(-0.5*(x-[1])*(x-[1])/([2]*[2]))/(TMath::Sqrt(2*TMath::Pi())*[2]) + [3]");
175 
176  f1.SetParameter(0, hI->GetMaximum());
177  f1.SetParameter(1, hI->GetMean());
178  f1.SetParameter(2, hI->GetRMS() * 0.25);
179  f1.SetParameter(3, hI->GetMinimum());
180 
181  hI->Fit(&f1, option.c_str(), "same");
182 
183  double sg_v = f1.GetParameter(0);
184  double sg_e = f1.GetParError( 0);
185 
186  double bg_v = f1.GetParameter(3);
187  double bg_e = f1.GetParError( 3);
188 
189  // 2-sigma acceptance criteria for fitted values
190  if (bg_v / bg_e <= 2.0) { bg_v = bg_e = 0; }
191  if (sg_v / sg_e <= 2.0) { sg_v = sg_e = 0; }
192 
193  double integral = hI->GetSumOfWeights();
194 
195  if (fitBackground) {
196 
197  inclusiveCountVal = V * (integral - (bg_v * N));
198  inclusiveCountErr = V * bg_e * N;
199 
200  } else {
201 
202  inclusiveCountErr = sg_e;
203  inclusiveCountVal = sg_v;
204 
205  }
206 
207  hI->Scale(1.0 / T);
208 
209  // re-fit after scaling
210  hI->Fit(&f1, option.c_str(), "same");
211 
212  }
213 
214  if (inclusiveCountVal <= 0) { inclusiveCountVal = 0; }
215 
216  // --------------------------------
217  // write fitted histogram
218  // --------------------------------
219 
220  hI->Write();
221 
222  // --------------------------------
223  // build inclusive rates histograms
224  // --------------------------------
225 
226  int bin = HTF[h2D_label]->FindBin(M);
227 
228  HTF[h2D_label]->SetBinContent(bin, inclusiveCountVal);
229 
230  double binError = sqrt(inclusiveCountVal + pow(inclusiveCountErr, 2));
231 
232  HTF[h2D_label]->SetBinError(bin, binError);
233 
234  // --------------------------------
235  // build exclusive rates histograms
236  // --------------------------------
237 
238  double exclusiveCountVal = 0;
239 
240  if (M == NUMBER_OF_PMTS) {
241  exclusiveCountVal = inclusiveCountVal;
242  } else {
243  int prevBin = HTF[h2D_label]->GetXaxis()->FindBin(M+1);
244  exclusiveCountVal = inclusiveCountVal - HTF[h2D_label]->GetBinContent(prevBin);
245  }
246 
247  HTFE[h2D_label]->SetBinContent(HTFE[h2D_label]->FindBin(M), exclusiveCountVal);
248 
249 
250  }
251 
252  }
253 
254  } else {
255  DEBUG(h1D_label << " not found" << endl);
256  }
257 
258  // HTF[hLabel]->Sumw2(kTRUE);
259 
260  HTFE[h2D_label]->Sumw2();
261 
262  HTF[h2D_label ]->Scale(1.0 / T);
263  HTFE[h2D_label]->Scale(1.0 / T);
264 
265  }
266  }
267 
268  fdir = out.mkdir("HTF" ); fdir->cd();
269 
270  HTF.Write(*fdir);
271 
272  fdir = out.mkdir("HTFE"); fdir->cd();
273 
274  HTFE.Write(*fdir);
275 
276  out.Close();
277 
278 }
279 
280 
Utility class to parse command line options.
Definition: JParser.hh:1493
do $JPP JMEstimator M
Definition: JMEstimator.sh:37
esac print_variable DETECTOR INPUT_FILE OUTPUT_FILE CDF for TYPE in
Definition: JSirene.sh:45
Dynamic ROOT object management.
string outputFile
Auxiliary class to manage set of compatible ROOT objects (e.g.
Definition: JManager.hh:42
Constants.
I/O formatting auxiliaries.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1954
do set_variable OUTPUT_DIRECTORY $WORKDIR T
ROOT I/O of application specific meta data.
const char * livetime_t
int debug
debug level
Definition: JSirene.cc:61
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
Auxiliary class to define a range between two values.
Utility class to parse command line options.
KM3NeT DAQ constants, bit handling, etc.
static const int NUMBER_OF_PMTS
Total number of PMTs in module.
Definition: JDAQ.hh:26
then usage $script[input file[working directory[option]]] nWhere option can be N
Definition: JMuonPostfit.sh:37
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
int main(int argc, char *argv[])
Definition: Main.cpp:15