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