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"
17 #include "JDAQ/JDAQ.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 int main(int argc, char **argv) {
42  using namespace std;
43  using namespace JPP;
44 
45  string inputFile;
46  string outputFile;
47  string summaryFile;
48  string option;
49  int debug;
50  bool fitBackground;
51 
52  try {
53 
54  JParser<> zap("Auxiliary program to fit multiplicity rates from JMonitorMultiplicity output.");
55 
56  zap['f'] = make_field(inputFile, "input file (output from JMonitorMultiplicity).");
57  zap['o'] = make_field(outputFile, "output file.") = "fitmultiplicity.root";
58  zap['O'] = make_field(option, "fitting option") = "Q";
59  zap['B'] = make_field(fitBackground, "fit and subtract background instead of fitting signal");
60  zap['d'] = make_field(debug) = 1;
61 
62  zap(argc, argv);
63  }
64  catch(const exception &error) {
65  FATAL(error.what() << endl);
66  }
67 
68  TFile* in = TFile::Open(inputFile.c_str(), "exist");
69 
70  if (in == NULL || !in->IsOpen()) {
71  FATAL("File: " << inputFile << " not opened." << endl);
72  }
73 
74 
75  //----------------------------------------------------------
76  // general histograms
77  //----------------------------------------------------------
78 
79  const int nx = 1 + NUMBER_OF_PMTS;
80  const double xmin = -0.5;
81  const double xmax = nx - 0.5;
82 
83  typedef JManager<TString, TH1D> JManager_t;
84 
85  JManager_t HTF(new TH1D("%F", "", nx, xmin, xmax));
86 
87  HTF->Sumw2(kFALSE);
88 
89  JManager_t HTFE(new TH1D("%FE", "", nx, xmin, xmax));
90 
91  //----------------------------------------------------------
92  // get overall normalization constants
93  //----------------------------------------------------------
94 
95  TH1D* hLiveTime = (TH1D*) in->Get(livetime_t);
96 
97  if (hLiveTime == NULL) {
98  FATAL("File " << inputFile << " does not contain livetime histogram." << endl);
99  }
100 
101  TFile out(outputFile.c_str(), "recreate");
102 
103  //----------------------------------------------------------
104  // loop over bins of livetime histogram
105  //----------------------------------------------------------
106 
107  // bin = 1 -> nominal livetime
108  for (int bin = 2; bin <= hLiveTime->GetNbinsX(); bin++) {
109 
110  TString hLabel = hLiveTime->GetXaxis()->GetBinLabel(bin) + TString("_HT");
111 
112  DEBUG("Reading livetime for " << hLabel << endl);
113 
114  double T = hLiveTime->GetBinContent(bin);
115 
116  if (T > 0) {
117 
118  TDirectory *cdir = out.mkdir(hLabel);
119 
120  cdir->cd();
121 
122  TH2D* hDOM = (TH2D*) in->Get(hLabel);
123 
124  if (hDOM == NULL) {
125  // inconsistent file, should be FATAL?
126  ERROR("Histogram " << hLabel << " not found, skipping DOM." << endl);
127  continue;
128  }
129 
130  //----------------------------------------------------------
131  // loop over multiplicities
132  //----------------------------------------------------------
133 
134  TH1D *hI, *hIF;
135 
136  for (int M = NUMBER_OF_PMTS; M >= 2; M--) {
137 
138  int Mbin = hDOM->GetXaxis()->FindBin(M);
139 
140  TH1D* hM = hDOM->ProjectionY(hLabel + Form("_%d", M), Mbin, Mbin);
141 
142  if (M == NUMBER_OF_PMTS) {
143  hI = (TH1D*) hM->Clone(hLabel + "I");
144  } else {
145  hI->Add(hM);
146  }
147 
148  if (hI->GetEntries() > 0) {
149 
150  const double W = (hI->GetXaxis()->GetXmax() - hI->GetXaxis()->GetXmin());
151  const double N = hI->GetNbinsX();
152  const double V = W / N;
153 
154  double inclusiveCountVal = 0, inclusiveCountErr = 0;
155 
156  hIF = (TH1D*) hI->Clone(hLabel + "IF" + Form("_%d", M)); hIF->Sumw2();
157 
158  if (hIF->GetRMS() > 0.1) {
159 
160  hIF->Scale(1.0 / V);
161 
162  TF1 f1("f1", "[0]*exp(-0.5*(x-[1])*(x-[1])/([2]*[2]))/(TMath::Sqrt(2*TMath::Pi())*[2]) + [3]");
163 
164  f1.SetParameter(0, hIF->GetMaximum());
165  f1.SetParameter(1, hIF->GetMean());
166  f1.SetParameter(2, hIF->GetRMS() * 0.25);
167  f1.SetParameter(3, hIF->GetMinimum());
168 
169  hIF->Fit(&f1, option.c_str(), "same");
170 
171  double sg_v = f1.GetParameter(0);
172  double sg_e = f1.GetParError( 0);
173 
174  double bg_v = f1.GetParameter(3);
175  double bg_e = f1.GetParError( 3);
176 
177  // 2-sigma acceptance criteria for fitted values
178  if (bg_v / bg_e <= 2.0) { bg_v = bg_e = 0; }
179  if (sg_v / sg_e <= 2.0) { sg_v = sg_e = 0; }
180 
181  double integral = hIF->GetSumOfWeights();
182 
183  if (fitBackground) {
184 
185  inclusiveCountVal = V * (integral - (bg_v * N));
186  inclusiveCountErr = V * bg_e * N;
187 
188  } else {
189 
190  inclusiveCountErr = sg_e;
191  inclusiveCountVal = sg_v;
192 
193  }
194 
195  hIF->Scale(1.0 / T);
196  hIF->Fit(&f1, option.c_str(), "same");
197 
198 
199  }
200 
201  if (inclusiveCountVal <= 0) { inclusiveCountVal = 0; }
202 
203  // --------------------------------
204  // write fitted histogram
205  // --------------------------------
206 
207  hIF->Write();
208 
209  // --------------------------------
210  // build inclusive rates histograms
211  // --------------------------------
212 
213  int bin = HTF[hLabel]->FindBin(M);
214 
215  HTF[hLabel]->SetBinContent(bin, inclusiveCountVal);
216 
217  double binError = sqrt(inclusiveCountVal + pow(inclusiveCountErr, 2));
218 
219  HTF[hLabel]->SetBinError(bin, binError);
220 
221  // --------------------------------
222  // build exclusive rates histograms
223  // --------------------------------
224 
225  double exclusiveCountVal = 0;
226 
227  if (M == NUMBER_OF_PMTS) {
228  exclusiveCountVal = inclusiveCountVal;
229  } else {
230  int prevBin = HTF[hLabel]->GetXaxis()->FindBin(M+1);
231  exclusiveCountVal = inclusiveCountVal - HTF[hLabel]->GetBinContent(prevBin);
232  }
233 
234  HTFE[hLabel]->SetBinContent(HTFE[hLabel]->FindBin(M), exclusiveCountVal);
235 
236 
237  }
238  }
239 
240 
241  // HTF[hLabel]->Sumw2(kTRUE);
242 
243  HTFE[hLabel]->Sumw2();
244 
245  HTF[hLabel ]->Scale(1.0 / T);
246  HTFE[hLabel]->Scale(1.0 / T);
247 
248  }
249  }
250 
251  HTF.Write(out);
252 
253  HTFE.Write(out);
254 
255  out.Close();
256 
257 }
258 
259 
Utility class to parse command line options.
Definition: JParser.hh:1410
Auxiliary class to manage set of histograms.
Dynamic ROOT object management.
string outputFile
Constants.
I/O formatting auxiliaries.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1836
ROOT I/O of application specific meta data.
#define ERROR(A)
Definition: JMessage.hh:64
const char * livetime_t
int debug
debug level
Definition: JSirene.cc:59
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:65
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
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:60
int main(int argc, char *argv[])
Definition: Main.cpp:15