CMS 3D CMS Logo

Zmumumerge.cc
Go to the documentation of this file.
1 #include <cmath>
2 #include <fstream>
3 #include <iomanip>
4 #include <iostream>
5 #include <string>
6 
7 #include "toolbox.h"
8 #include "FitWithRooFit.cc"
9 #include "Options.h"
10 
11 #include "TAxis.h"
12 #include "TBranch.h"
13 #include "TCanvas.h"
14 #include "TChain.h"
15 #include "TCut.h"
16 #include "TF1.h"
17 #include "TFile.h"
18 #include "TFrame.h"
19 #include "TH1.h"
20 #include "TH1D.h"
21 #include "TH1F.h"
22 #include "TH2.h"
23 #include "TH2F.h"
24 #include "THStack.h"
25 #include "TLatex.h"
26 #include "TLegend.h"
27 #include "TMath.h"
28 #include "TPad.h"
29 #include "TPaveText.h"
30 #include "TRandom.h"
31 #include "TString.h"
32 #include "TStyle.h"
33 #include "TSystem.h"
34 #include "TTree.h"
35 //#include "RooGlobalFunc.h"
36 
37 #include <boost/filesystem.hpp>
38 #include <boost/property_tree/ptree.hpp>
39 #include <boost/property_tree/json_parser.hpp>
40 #include <boost/range/adaptor/indexed.hpp>
41 
42 using namespace RooFit;
43 using namespace std;
44 using namespace AllInOneConfig;
45 namespace pt = boost::property_tree;
46 /*--------------------------------------------------------------------*/
47 void makeNicePlotStyle(RooPlot* plot) {
48  plot->GetXaxis()->CenterTitle(true);
49  plot->GetYaxis()->CenterTitle(true);
50  plot->GetXaxis()->SetTitleFont(42);
51  plot->GetYaxis()->SetTitleFont(42);
52  plot->GetXaxis()->SetTitleSize(0.05);
53  plot->GetYaxis()->SetTitleSize(0.05);
54  plot->GetXaxis()->SetTitleOffset(0.9);
55  plot->GetYaxis()->SetTitleOffset(1.3);
56  plot->GetXaxis()->SetLabelFont(42);
57  plot->GetYaxis()->SetLabelFont(42);
58  plot->GetYaxis()->SetLabelSize(.05);
59  plot->GetXaxis()->SetLabelSize(.05);
60 }
61 /*--------------------------------------------------------------------*/
62 
63 RooRealVar MuMu_mass("MuMu_mass", "MuMu_mass", 70, 110);
64 static TString GT = "";
65 TLatex* tlxg = new TLatex();
66 class FitOut {
67 public:
68  double mean;
69  double mean_err;
70  double sigma;
71  double sigma_err;
72  double chi2;
73  FitOut(double a, double b, double c, double d) : mean(a), mean_err(b), sigma(c), sigma_err(d) {}
74 };
75 
76 FitOut ZMassBinFit_OldTool(TH1D* th1d_input, TString s_name = "zmumu_fitting", TString output_path = "./") {
77  double xMin(75), xMax(105), xMean(91);
78  double sigma = 2;
79  double sigmaMin = 0.1;
80  double sigmaMax = 10;
81 
82  FitWithRooFit* fitter = new FitWithRooFit();
83  double sigma2(0.1), sigma2Min(0.), sigma2Max(10.), useChi2(false);
84  fitter->useChi2_ = useChi2;
85  fitter->initMean(xMean, xMin, xMax);
86  fitter->initSigma(sigma, sigmaMin, sigmaMax);
87  fitter->initSigma2(sigma2, sigma2Min, sigma2Max);
88  fitter->initAlpha(1.5, 0.05, 10.);
89  fitter->initN(1, 0.01, 100.);
90  fitter->initFGCB(0.4, 0., 1.);
91 
92  fitter->initMean(91.1876, xMin, xMax);
93  fitter->initGamma(2.4952, 0., 10.);
94  fitter->gamma()->setConstant(kTRUE);
95  fitter->initMean2(0., -20., 20.);
96  fitter->mean2()->setConstant(kTRUE);
97  fitter->initSigma(1.2, 0., 5.);
98  fitter->initAlpha(1.5, 0.05, 10.);
99  fitter->initN(1, 0.01, 100.);
100  fitter->initExpCoeffA0(-1., -10., 10.);
101  fitter->initExpCoeffA1(0., -10., 10.);
102  fitter->initExpCoeffA2(0., -2., 2.);
103  fitter->initFsig(0.9, 0., 1.);
104  fitter->initA0(0., -10., 10.);
105  fitter->initA1(0., -10., 10.);
106  fitter->initA2(0., -10., 10.);
107  fitter->initA3(0., -10., 10.);
108  fitter->initA4(0., -10., 10.);
109  fitter->initA5(0., -10., 10.);
110  fitter->initA6(0., -10., 10.);
111  TCanvas* c1 = new TCanvas();
112  c1->Clear();
113  c1->SetLeftMargin(0.15);
114  c1->SetRightMargin(0.10);
115 
116  fitter->fit(th1d_input, "breitWignerTimesCB", "exponential", xMin, xMax, false);
117 
118  c1->Print(Form("%s/fitResultPlot/%s_oldtool.pdf", output_path.Data(), s_name.Data()));
119  c1->Print(Form("%s/fitResultPlot/%s_oldtool.root", output_path.Data(), s_name.Data()));
120 
121  FitOut fitRes(
122  fitter->mean()->getValV(), fitter->mean()->getError(), fitter->sigma()->getValV(), fitter->sigma()->getError());
123  return fitRes;
124 }
125 void Draw_th1d(TH1D* th1d_input, TString variable_name) {
126  TCanvas* c = new TCanvas();
127  c->cd();
128  gStyle->SetOptStat(0);
129  th1d_input->SetMarkerStyle(kFullCircle);
130  th1d_input->SetMarkerColor(kRed);
131  th1d_input->SetLineColor(kRed);
132  th1d_input->SetMaximum(91.4);
133  th1d_input->SetMinimum(90.85);
134  th1d_input->GetXaxis()->SetTitle(variable_name.Data());
135  th1d_input->GetXaxis()->SetTitleOffset(1.2);
136  th1d_input->GetYaxis()->SetTitle("Mass mean (GeV)");
137  th1d_input->Draw();
138  tlxg->DrawLatexNDC(0.2, 0.8, Form("%s", GT.Data()));
139  c->Print(Form("%s/fitResultPlot/mass_VS_%s.pdf", GT.Data(), variable_name.Data()));
140 }
141 
142 const static int variables_number = 8;
144  "CosThetaCS", "DeltaEta", "EtaMinus", "EtaPlus", "PhiCS", "PhiMinus", "PhiPlus", "Pt"};
145 void Fitting_GetMassmeanVSvariables(TString inputfile_name, TString output_path) {
146  TH2D* th2d_mass_variables[variables_number];
147  TFile* inputfile = TFile::Open(inputfile_name.Data());
148  TDirectoryFile* tdirectory = (TDirectoryFile*)inputfile->Get("DiMuonMassValidation");
149  for (int i = 0; i < variables_number; i++) {
150  TString th2d_name = Form("th2d_mass_%s", tstring_variables_name[i].Data());
151  th2d_mass_variables[i] = (TH2D*)tdirectory->Get(th2d_name);
152  }
153 
154  gSystem->Exec(Form("mkdir -p %s", output_path.Data()));
155  gSystem->Exec(Form("mkdir -p %s/fitResultPlot", output_path.Data()));
156  TFile* outpufile = TFile::Open(Form("%s/fitting_output.root", output_path.Data()), "recreate");
157  TH1D* th1d_variables_meanmass[variables_number];
158  TH1D* th1d_variables_entries[variables_number];
159  const int variables_rebin[variables_number] = {1, 1, 1, 1, 1, 1, 1, 1};
160  const double xaxis_range[variables_number][2] = {
161  {-1, 1}, {-4.8, 4.8}, {-2.4, 2.4}, {-2.4, 2.4}, {-1, 1}, {-M_PI, M_PI}, {-M_PI, M_PI}, {0, 100}};
162  for (int i = 0; i < variables_number; i++) {
163  TString th1d_name = Form("th1d_meanmass_%s", tstring_variables_name[i].Data());
164 
165  th2d_mass_variables[i]->RebinY(variables_rebin[i]);
166  th1d_variables_meanmass[i] = th2d_mass_variables[i]->ProjectionY(th1d_name, 1, 1, "d");
167  for (int j = 0; j < th1d_variables_meanmass[i]->GetNbinsX(); j++) {
168  if (i == 7 and j > 25) {
169  continue;
170  }
171  cout << "th1d_variables_meanmass[i]->GetNbinsX()=" << th1d_variables_meanmass[i]->GetNbinsX() << endl;
172  cout << "th2d_mass_variables[i]->GetNbinsY()=" << th2d_mass_variables[i]->GetNbinsY() << endl;
173  th1d_variables_meanmass[i]->SetBinContent(j, 0);
174  th1d_variables_meanmass[i]->SetBinError(j, 0);
175 
176  TString th1d_mass_temp_name = Form("th1d_mass_%s_%d", tstring_variables_name[i].Data(), j);
177  TH1D* th1d_i = th2d_mass_variables[i]->ProjectionX(th1d_mass_temp_name, j, j, "d");
178  th1d_i->Write(th1d_mass_temp_name);
179  TString s_cut = Form("nocut");
180  TString s_name = Form("%s_%d", tstring_variables_name[i].Data(), j);
181 
182  FitOut fitR = ZMassBinFit_OldTool(th1d_i, s_name, output_path);
183  th1d_variables_meanmass[i]->SetBinContent(j, fitR.mean);
184  th1d_variables_meanmass[i]->SetBinError(j, fitR.mean_err);
185  }
186  th1d_variables_meanmass[i]->GetXaxis()->SetRangeUser(xaxis_range[i][0], xaxis_range[i][1]);
187  Draw_th1d(th1d_variables_meanmass[i], tstring_variables_name[i]);
188  outpufile->cd();
189  th1d_variables_meanmass[i]->Write(th1d_name);
190 
191  TString th1d_name_entries = Form("th1d_entries_%s", tstring_variables_name[i].Data());
192  th1d_variables_entries[i] = th2d_mass_variables[i]->ProjectionY(th1d_name_entries, 0, -1, "d");
193  th1d_variables_entries[i]->GetXaxis()->SetTitle(tstring_variables_name[i].Data());
194  th1d_variables_entries[i]->GetYaxis()->SetTitle("Entry");
195  outpufile->cd();
196  th1d_variables_entries[i]->Write(th1d_name_entries);
197  }
198 
199  outpufile->Write();
200  outpufile->Close();
201  delete outpufile;
202 }
203 
204 const static int max_file_number = 10;
205 void Draw_TH1D_forMultiRootFiles(const vector<TString>& file_names,
206  const vector<TString>& label_names,
207  const vector<int>& colors,
208  const vector<int>& styles,
209  const TString& th1d_name,
210  const TString& output_name) {
211  if (file_names.empty() || label_names.empty()) {
212  cout << "Provided an empty list of file and label names" << std::endl;
213  return;
214  }
215 
216  // do not allow the list of files and labels names to differ
217  assert(file_names.size() == label_names.size());
218 
219  TH1D* th1d_input[max_file_number];
220  TFile* file_input[max_file_number];
221  for (auto const& filename : file_names | boost::adaptors::indexed(0)) {
222  file_input[filename.index()] = TFile::Open(filename.value());
223  th1d_input[filename.index()] = (TH1D*)file_input[filename.index()]->Get(th1d_name);
224  }
225 
226  TCanvas* c = new TCanvas();
227  TLegend* lg = new TLegend(0.2, 0.7, 0.5, 0.95);
228  c->cd();
229  gStyle->SetOptStat(0);
230  th1d_input[0]->SetTitle("");
231 
232  for (auto const& labelname : label_names | boost::adaptors::indexed(0)) {
233  th1d_input[labelname.index()]->SetMarkerColor(colors[labelname.index()]);
234  th1d_input[labelname.index()]->SetLineColor(colors[labelname.index()]);
235  th1d_input[labelname.index()]->SetMarkerStyle(styles[labelname.index()]);
236  th1d_input[labelname.index()]->Draw("same");
237  lg->AddEntry(th1d_input[labelname.index()], labelname.value());
238  }
239  lg->Draw("same");
240  c->SaveAs(output_name);
241  if (output_name.Contains(".pdf")) {
242  TString output_name_png(output_name); // output_name is const, copy to modify
243  output_name_png.Replace(output_name_png.Index(".pdf"), 4, ".png");
244  c->SaveAs(output_name_png);
245  }
246 }
247 
248 int Zmumumerge(int argc, char* argv[]) {
249  vector<TString> vec_single_file_path;
250  vector<TString> vec_single_file_name;
251  vector<TString> vec_global_tag;
252  vector<TString> vec_title;
253  vector<int> vec_color;
254  vector<int> vec_style;
255 
257  options.helper(argc, argv);
258  options.parser(argc, argv);
259  pt::ptree main_tree;
260  pt::read_json(options.config, main_tree);
261  pt::ptree alignments = main_tree.get_child("alignments");
262  pt::ptree validation = main_tree.get_child("validation");
263  for (const auto& childTree : alignments) {
264  vec_single_file_path.push_back(childTree.second.get<std::string>("file"));
265  vec_single_file_name.push_back(childTree.second.get<std::string>("file") + "/Zmumu.root");
266  vec_color.push_back(childTree.second.get<int>("color"));
267  vec_style.push_back(childTree.second.get<int>("style"));
268  vec_global_tag.push_back(childTree.second.get<std::string>("globaltag"));
269  vec_title.push_back(childTree.second.get<std::string>("title"));
270 
271  //Fitting_GetMassmeanVSvariables(childTree.second.get<std::string>("file") + "/Zmumu.root", childTree.second.get<std::string>("file"));
272  }
273  TString merge_output = main_tree.get<std::string>("output");
274  //=============================================
275  vector<TString> vec_single_fittingoutput;
276  vec_single_fittingoutput.clear();
277  for (unsigned i = 0; i < vec_single_file_path.size(); i++) {
278  Fitting_GetMassmeanVSvariables(vec_single_file_name[i], vec_single_file_path[i]);
279  vec_single_fittingoutput.push_back(vec_single_file_path[i] + "/fitting_output.root");
280  }
281 
282  int files_number = vec_single_file_path.size();
283  cout << "files_number=" << files_number << endl;
284  for (int idx_variable = 0; idx_variable < variables_number; idx_variable++) {
285  TString th1d_name = Form("th1d_meanmass_%s", tstring_variables_name[idx_variable].Data());
287  vec_single_fittingoutput,
288  vec_title,
289  vec_color,
290  vec_style,
291  th1d_name,
292  merge_output + Form("/meanmass_%s_GTs.pdf", tstring_variables_name[idx_variable].Data()));
293  TString th1d_name_entries = Form("th1d_entries_%s", tstring_variables_name[idx_variable].Data());
295  vec_single_fittingoutput,
296  vec_title,
297  vec_color,
298  vec_style,
299  th1d_name_entries,
300  merge_output + Form("/entries_%s_GTs.pdf", tstring_variables_name[idx_variable].Data()));
301  }
302  //=============================================
303  return EXIT_SUCCESS;
304 }
305 #ifndef DOXYGEN_SHOULD_SKIP_THIS
306 int main(int argc, char* argv[]) { return Zmumumerge(argc, argv); }
307 #endif
void initA6(const double &value, const double &min, const double &max, const TString &name="a6", const TString &title="a6")
int Zmumumerge(int argc, char *argv[])
Definition: Zmumumerge.cc:248
void Draw_TH1D_forMultiRootFiles(const vector< TString > &file_names, const vector< TString > &label_names, const vector< int > &colors, const vector< int > &styles, const TString &th1d_name, const TString &output_name)
Definition: Zmumumerge.cc:205
static TString GT
Definition: Zmumumerge.cc:64
void makeNicePlotStyle(RooPlot *plot)
Definition: Zmumumerge.cc:47
void Draw_th1d(TH1D *th1d_input, TString variable_name)
Definition: Zmumumerge.cc:125
RooRealVar * sigma()
static const int variables_number
Definition: Zmumumerge.cc:142
vector< Style_t > styles
void initA1(const double &value, const double &min, const double &max, const TString &name="a1", const TString &title="a1")
static const int max_file_number
Definition: Zmumumerge.cc:204
void initFsig(const double &value, const double &min, const double &max, const TString &name="fsig", const TString &title="signal fraction")
FitOut ZMassBinFit_OldTool(TH1D *th1d_input, TString s_name="zmumu_fitting", TString output_path="./")
Definition: Zmumumerge.cc:76
void fit(TH1 *histo, const TString signalType, const TString backgroundType, const double &xMin=0., const double &xMax=0., bool sumW2Error=false)
void initGamma(const double &value, const double &min, const double &max, const TString &name="gamma", const TString &title="gamma")
FitOut(double a, double b, double c, double d)
Definition: Zmumumerge.cc:73
assert(be >=bs)
int main(int argc, char *argv[])
Definition: Zmumumerge.cc:306
void initA3(const double &value, const double &min, const double &max, const TString &name="a3", const TString &title="a3")
RooRealVar * mean()
RooRealVar * mean2()
void initA4(const double &value, const double &min, const double &max, const TString &name="a4", const TString &title="a4")
RooRealVar MuMu_mass("MuMu_mass", "MuMu_mass", 70, 110)
d
Definition: ztail.py:151
RooRealVar * gamma()
void initMean(const double &value, const double &min, const double &max, const TString &name="mean", const TString &title="mean")
#define M_PI
void initN(const double &value, const double &min, const double &max, const TString &name="n", const TString &title="n")
double sigma_err
Definition: Zmumumerge.cc:71
const TString tstring_variables_name[variables_number]
Definition: Zmumumerge.cc:143
void initA0(const double &value, const double &min, const double &max, const TString &name="a0", const TString &title="a0")
void initAlpha(const double &value, const double &min, const double &max, const TString &name="alpha", const TString &title="alpha")
void initA5(const double &value, const double &min, const double &max, const TString &name="a5", const TString &title="a5")
void initExpCoeffA0(const double &value, const double &min, const double &max, const TString &name="expCoeffa0", const TString &title="expCoeffa0")
double b
Definition: hdecay.h:118
void initExpCoeffA1(const double &value, const double &min, const double &max, const TString &name="expCoeffa1", const TString &title="expCoeffa1")
void initA2(const double &value, const double &min, const double &max, const TString &name="a2", const TString &title="a2")
Definition: colors.py:1
double a
Definition: hdecay.h:119
double mean
Definition: Zmumumerge.cc:68
void Fitting_GetMassmeanVSvariables(TString inputfile_name, TString output_path)
Definition: Zmumumerge.cc:145
void initSigma2(const double &value, const double &min, const double &max, const TString &name="sigma2", const TString &title="sigma2")
void initFGCB(const double &value, const double &min, const double &max, const TString &name="fGCB", const TString &title="fGCB")
double chi2
Definition: Zmumumerge.cc:72
TLatex * tlxg
Definition: Zmumumerge.cc:65
double mean_err
Definition: Zmumumerge.cc:69
double sigma
Definition: Zmumumerge.cc:70
void initExpCoeffA2(const double &value, const double &min, const double &max, const TString &name="expCoeffa2", const TString &title="expCoeffa2")
void initSigma(const double &value, const double &min, const double &max, const TString &name="sigma", const TString &title="sigma")
void initMean2(const double &value, const double &min, const double &max, const TString &name="mean2", const TString &title="mean2")