5 #define ZMUMUVALIDATIONEXAMPLE_C
10 #include "TLorentzVector.h"
17 , std::string
const & s_outFileName
19 ) : m_fileNames( s_fileNames )
20 , m_outfilename( s_outFileName )
21 ,
m_file( s_outFileName.c_str(),
"RECREATE" )
22 , m_eventChain( s_treeName.c_str() )
23 , m_truthChain(
"TruthParams" )
53 std::cout <<
"Input Files:" << std::endl;
55 std::list<std::string>::const_iterator theEnd =
m_fileNames.end();
56 for( std::list<std::string>::const_iterator itr =
m_fileNames.begin()
61 std::cout <<
" "<< *itr << std::endl;
119 const int fnQoverPtBins = 100;
121 const double eta_bound = 2.5;
122 const double phi_bound =
M_PI;
123 const double p_bound = 0.5;
124 const double pt_bound = 15;
126 const double z_bound = 0.4;
127 const double d_bound = 0.15;
129 h_DELTA =
new TH1F(
"h_DELTA",
";#delta [GeV]",200,-50,50);
131 h_pt =
new TH1F(
"Pt",
"p_{T} of both #mu; p_{T} [GeV]", 100, 0., 100.);
132 h_pt_pos =
new TH1F(
"Pt_Pos",
"p_{T} of #mu^{+}; p_{T} [GeV]", 100, 0., 100.);
133 h_pt_neg =
new TH1F(
"Pt_Neg",
"p_{T} of #mu^{-}; p_{T} [GeV]", 100, 0., 100.);
135 h_z0 =
new TH1F(
"h_z0",
"z_{0}: longitudinal impact param.; z_{0} [mm]", 100, -150., 150.);
136 h_z0_pos =
new TH1F(
"h_z0_Pos",
"z_{0} of #mu^{+};z_{0} [mm]",100, -150., 150.);
137 h_z0_neg =
new TH1F(
"h_z0_Neg",
"z_{0} of #mu^{-};z_{0} [mm]",100, -150., 150.);
138 h_d0 =
new TH1F(
"h_d0",
"d_{0}: transvers. impact param.; d_{0} [mm]", 100, -0.08, 0.08);
139 h_d0_pos =
new TH1F(
"h_d0_Pos",
"d_{0} of #mu^{+};d_{0} [mm]",100, -0.08, 0.08);
140 h_d0_neg =
new TH1F(
"h_d0_Neg",
"d_{0} of #mu^{-};d_{0} [mm]",100, -0.08, 0.08);
142 h_mass =
new TH1F(
"ZMass",
";Mass [GeV]",100,70,110);
157 entries =
new TH2F(
"entries",
"Entries per #eta-#phi bin;#eta;#phi;entries", fnEtaBins,-eta_bound,eta_bound, fnPhiBins,-phi_bound,phi_bound);
159 z0delta_vs_etaphi =
new TH3F(
"Delta_z0_VsEtaPhi",
";#eta;#phi;#delta_{z_{0}} [mm]", fnEtaBins,-eta_bound,eta_bound, fnPhiBins,-phi_bound,phi_bound, 50,-z_bound,z_bound);
161 z0deltacorrections_vs_etaphi =
new TH2D(
"z0CorrectionVsEtaPhi",
";#eta;#phi;#delta_{z_{0}} [mm]", fnEtaBins,-eta_bound,eta_bound, fnPhiBins,-phi_bound,phi_bound);
163 z0deltacorrections_vs_etaphi_err =
new TH2D(
"z0CorrectionVsEtaPhi_Err",
";#eta;#phi;#delta_{z_{0}}", fnEtaBins,-eta_bound,eta_bound, fnPhiBins,-phi_bound,phi_bound);
166 z0delta =
new TH1D(
"Delta_z0",
";#delta_{z_{0}} [mm]", 100,-z_bound,z_bound);
168 z0delta_pos =
new TH1D(
"Delta_z0_Pos",
";#delta_{z_{0}} [mm]", 100,-z_bound,z_bound);
170 z0delta_neg =
new TH1D(
"Delta_z0_Neg",
";#delta_{z_{0}} [mm]", 100,-z_bound,z_bound);
172 z0delta_etaphi =
new TH1D(
"Delta_z0_etaphi",
";#delta_{z_{0}} [mm]", 100,-z_bound,z_bound);
174 z0delta_etaphi_pos =
new TH1D(
"Delta_z0_etaphi_Pos",
";#delta_{z_{0}} [mm]", 100,-z_bound,z_bound);
176 z0delta_etaphi_neg =
new TH1D(
"Delta_z0_etaphi_Neg",
";#delta_{z_{0}} [mm]", 100,-z_bound,z_bound);
180 d0delta_vs_etaphi =
new TH3F(
"Delta_d0_VsEtaPhi",
";#eta;#phi;#delta_{d_{0}} [mm]", fnEtaBins,-eta_bound,eta_bound, fnPhiBins,-phi_bound,phi_bound, 50,-d_bound, d_bound);
182 d0deltacorrections_vs_etaphi =
new TH2D(
"d0CorrectionVsEtaPhi",
";#eta;#phi;#delta_{d_{0}} [mm]", fnEtaBins,-eta_bound,eta_bound, fnPhiBins,-phi_bound,phi_bound);
184 d0deltacorrections_vs_etaphi_err =
new TH2D(
"d0CorrectionVsEtaPhi_Err",
";#eta;#phi;#delta_{d_{0}}", fnEtaBins,-eta_bound,eta_bound, fnPhiBins,-phi_bound,phi_bound);
186 d0delta =
new TH1D(
"Delta_d0",
";#delta_{d_{0}} [mm]", 100,-d_bound,d_bound);
188 d0delta_pos =
new TH1D(
"Delta_d0_Pos",
";#delta_{d_{0}} [mm]", 100,-d_bound,d_bound);
190 d0delta_neg =
new TH1D(
"Delta_d0_Neg",
";#delta_{d_{0}} [mm]", 100,-d_bound,d_bound);
192 d0delta_etaphi =
new TH1D(
"Delta_d0_etaphi",
";#delta_{d_{0}} [mm]", 100,-d_bound,d_bound);
194 d0delta_etaphi_pos =
new TH1D(
"Delta_d0_etaphi_Pos",
";#delta_{d_{0}} [mm]", 100,-d_bound,d_bound);
196 d0delta_etaphi_neg =
new TH1D(
"Delta_d0_etaphi_Neg",
";#delta_{d_{0}} [mm]", 100,-d_bound,d_bound);
199 delta_vs_etaphi =
new TH3F(
"DeltaPVsEtaPhi",
";#eta;#phi;#delta_{r}", fnEtaBins,-eta_bound,eta_bound, fnPhiBins,-phi_bound,phi_bound, 100,-p_bound,p_bound);
201 deltacorrections_vs_etaphi =
new TH2D(
"PCorrectionVsEtaPhi",
";#eta;#phi;#delta_{r}", fnEtaBins,-eta_bound,eta_bound, fnPhiBins,-phi_bound,phi_bound);
203 deltacorrections_vs_etaphi_err =
new TH2D(
"PCorrectionVsEtaPhi_Err",
";#eta;#phi;#delta_{r}", fnEtaBins,-eta_bound,eta_bound, fnPhiBins,-phi_bound,phi_bound);
206 lambda_vs_etaphi =
new TH3F(
"LambdaVsEtaPhi",
";#eta;#phi;#delta_{sagitta} [TeV^{-1}]", fnEtaBins,-eta_bound,eta_bound, fnPhiBins,-phi_bound,phi_bound, 100,-pt_bound,pt_bound);
208 lambdacorrections_vs_etaphi =
new TH2D(
"LambdaCorrectionVsEtaPhi",
";#eta;#phi;#delta_{sagitta} [TeV^{-1}]", fnEtaBins,-eta_bound,eta_bound, fnPhiBins,-phi_bound,phi_bound);
209 lambdacorrections_vs_etaphi_err =
new TH2D(
"LambdaCorrectionVsEtaPhi_Err",
";#eta;#phi;#delta_{sagitta} [TeV^{-1}]", fnEtaBins,-eta_bound,eta_bound, fnPhiBins,-phi_bound,phi_bound);
210 lambdacorrections_vs_etaphi_RMS =
new TH2D(
"LambdaCorrectionVsEtaPhi_RMS",
";#eta;#phi;RMS #delta_{sagitta} [GeV^{-1}]", fnEtaBins,-eta_bound,eta_bound, fnPhiBins,-phi_bound,phi_bound);
213 lambda_vs_eta =
new TH2F(
"LambdaVsEta",
";#eta;#delta_{sagitta} [TeV^{-1}]", fnEtaBins,-eta_bound,eta_bound, 100,-pt_bound,pt_bound);
214 lambda_vs_eta_pos =
new TH2F(
"LambdaVsEta_Pos",
";#eta;#delta_{sagitta} [TeV^{-1}]", fnEtaBins,-eta_bound,eta_bound, 100,-pt_bound,pt_bound);
215 lambda_vs_eta_neg =
new TH2F(
"LambdaVsEta_Neg",
";#eta;#delta_{sagitta} [TeV^{-1}]", fnEtaBins,-eta_bound,eta_bound, 100,-pt_bound,pt_bound);
217 lambdacorrections_vs_eta =
new TH1D(
"LambdaCorrectionVsEta",
";#eta;#delta_{sagitta} [TeV^{-1}]", fnEtaBins,-eta_bound,eta_bound);
219 lambda =
new TH1D(
"Lambda",
";#delta_{sagitta} [TeV^{-1}]", 100,-pt_bound,pt_bound);
220 lambda_pos =
new TH1D(
"Lambda_Pos",
";#delta_{sagitta} [TeV^{-1}]", 100,-pt_bound,pt_bound);
221 lambda_neg =
new TH1D(
"Lambda_Neg",
";#delta_{sagitta} [TeV^{-1}]", 100,-pt_bound,pt_bound);
223 lambda_eta =
new TH1D(
"Lambda_eta",
";#delta_{sagitta} [TeV^{-1}]", 100,-pt_bound,pt_bound);
224 lambda_eta_pos =
new TH1D(
"Lambda_eta_Pos",
";#delta_{sagitta} [TeV^{-1}]", 100,-pt_bound,pt_bound);
225 lambda_eta_neg =
new TH1D(
"Lambda_eta_Neg",
";#delta_{sagitta} [TeV^{-1}]", 100,-pt_bound,pt_bound);
227 lambda_etaphi =
new TH1D(
"Lambda_etaphi",
";#delta_{sagitta} [TeV^{-1}]", 100,-pt_bound,pt_bound);
228 lambda_etaphi_pos =
new TH1D(
"Lambda_etaphi_Pos",
";#delta_{sagitta} [TeV^{-1}]", 100,-pt_bound,pt_bound);
229 lambda_etaphi_neg =
new TH1D(
"Lambda_etaphi_Neg",
";#delta_{sagitta} [TeV^{-1}]", 100,-pt_bound,pt_bound);
233 h_pt_truth =
new TH1D(
"Pt_Truth",
";p_{T} [GeV]",500,0,500);
234 h_pt_pos_truth =
new TH1D(
"Pt_Pos_Truth",
";p_{T} [GeV]",500,0,500);
235 h_pt_neg_truth =
new TH1D(
"Pt_Neg_Truth",
";p_{T} [GeV]",500,0,500);
237 delta_vs_etaphi_truth =
new TH3F(
"DeltaPVsEtaPhi_Truth",
";#eta;#phi;#delta_{r}", fnEtaBins,-eta_bound,eta_bound, fnPhiBins,-phi_bound,phi_bound, 100,-p_bound,p_bound);
238 deltacorrections_vs_etaphi_truth =
new TH2D(
"PCorrectionVsEtaPhi_Truth",
";#eta;#phi;#delta_{sagitta} [TeV^{-1}]", fnEtaBins,-eta_bound,eta_bound, fnPhiBins,-phi_bound,phi_bound);
239 deltacorrections_vs_etaphi_truth_err =
new TH2D(
"PCorrectionVsEtaPhi_Truth_Err",
";#eta;#phi;#delta_{sagitta} [TeV^{-1}]", fnEtaBins,-eta_bound,eta_bound, fnPhiBins,-phi_bound,phi_bound);
241 lambda_vs_etaphi_truth =
new TH3F(
"LambdaVsEtaPhi_Truth",
";#eta;#phi;#delta_{sagitta} [TeV^{-1}]", fnEtaBins,-eta_bound,eta_bound, fnPhiBins,-phi_bound,phi_bound, 100,-pt_bound,pt_bound);
242 lambdacorrections_vs_etaphi_truth =
new TH2D(
"LambdaCorrectionVsEtaPhi_Truth",
";#eta;#phi;#delta_{sagitta} [TeV^{-1}]", fnEtaBins,-eta_bound,eta_bound, fnPhiBins,-phi_bound,phi_bound);
243 lambdacorrections_vs_etaphi_truth_err =
new TH2D(
"LambdaCorrectionVsEtaPhi_Truth_Err",
";#eta;#phi;#delta_{sagitta} [TeV^{-1}]", fnEtaBins,-eta_bound,eta_bound, fnPhiBins,-phi_bound,phi_bound);
245 lambda_vs_eta_truth =
new TH2F(
"LambdaVsEta_Truth",
";#eta;#delta_{sagitta} [TeV^{-1}]", fnEtaBins,-eta_bound,eta_bound, 100,-pt_bound,pt_bound);
248 truth_mom_bias_vs_eta =
new TH2F(
"TruthMomentumBiasVsEta",
";#eta;#delta_{sagitta} [TeV^{-1}]", fnEtaBins,-eta_bound,eta_bound, 100,-pt_bound,pt_bound);
251 truth_mass_bias_vs_eta =
new TH2F(
"TruthMassBiasVsEta",
";#eta;#delta_{sagitta} [TeV^{-1}]", fnEtaBins,-eta_bound,eta_bound, 100,-pt_bound,pt_bound);
254 lambda_truth =
new TH1D(
"Lambda_Truth",
";#delta_{sagitta} [TeV^{-1}]", 100,-pt_bound,pt_bound);
255 lambda_truth_pos =
new TH1D(
"Lambda_Truth_Pos",
";#delta_{sagitta} [TeV^{-1}]", 100,-pt_bound,pt_bound);
256 lambda_truth_neg =
new TH1D(
"Lambda_Truth_Neg",
";#delta_{sagitta} [TeV^{-1}]", 100,-pt_bound,pt_bound);
258 delta_phi_truth =
new TH1D(
"DeltaPhi_Truth",
";#phi_{Truth} - #phi_{Rec}", 100,-0.01,0.01);
260 delta_eta_truth =
new TH1D(
"DeltaEta_Truth",
";#eta_{Truth} - #eta_{Rec}", 100,-0.01,0.01);
262 delta_M2_vs_zpt_truth =
new TH2D(
"delta_M2_vs_zpt_truth",
";Z p_{T} [GeV]; #Delta (M^2)", 100,0,100, 100, -1.0, 1.0 );
263 delta_M2_vs_zpt =
new TH2D(
"delta_M2_vs_zpt",
";Z p_{T} [GeV]; #Delta (M^2)", 100,0,100, 100, -1.0, 1.0 );
265 delta_M2_vs_etaphi_pos =
new TProfile2D(
"delta_M2_vs_etaphi_pos",
";#eta;#phi; #Delta (M^2)", 20,-2.5,2.5, 20, -3.14, 3.14 );
266 delta_M2_vs_etaphi_neg =
new TProfile2D(
"delta_M2_vs_etaphi_neg",
";#eta;#phi; #Delta (M^2)", 20,-2.5,2.5, 20, -3.14, 3.14 );
271 h_QoverPt =
new TH2F(
"h_QoverPt",
"q/p_{T} whole ID", 2*fnPhiBins, -phi_bound, phi_bound, fnQoverPtBins, -1/pt_bound, 1/pt_bound);
272 h_QoverPt->GetXaxis()->SetTitle(
"#phi [rad]");
273 h_QoverPt->GetYaxis()->SetTitle(
"q/p_{T} [GeV^{-1}]");
275 h_QoverPt3D =
new TH3F(
"h_QoverPt3D",
"q/p_{T} whole ID",
276 fnEtaBins, -eta_bound, eta_bound,
277 2*fnPhiBins, -phi_bound, phi_bound,
278 fnQoverPtBins, -1/pt_bound, 1/pt_bound);
281 h_QoverPt3D->GetZaxis()->SetTitle(
"q/p_{T} [GeV^{-1}]");
290 std::cout <<
" TrkValidation::loop()" << std::endl;
300 if (
m_PrintLevel >= 1) cout <<
" ** ZmumuValidationExample::loop ** start loop on events ... " << endl;
303 if (
m_PrintLevel >= 1) cout<<
" ** ZmumuValidationExample::loop ** Fitting histograms" << endl;
333 std::cout <<
" Looping over " << maxItr <<
" events"<< std::endl;
335 for(
unsigned int eventItr = 0
340 if( eventItr % 10000 == 0 )
342 std::cout <<
" Processing event " << eventItr <<
" of " << maxItr << std::endl;
364 TLorentzVector* vec_pos =
new TLorentzVector();
365 TLorentzVector* vec_neg =
new TLorentzVector();
368 double corrected_z0_pos;
369 double corrected_z0_neg;
370 double corrected_d0_pos;
371 double corrected_d0_neg;
376 entries->Fill(vec_pos->Eta(),vec_pos->Phi(),1);
377 entries->Fill(vec_neg->Eta(),vec_neg->Phi(),1);
424 TLorentzVector* vec_truth_pos =
new TLorentzVector();
425 TLorentzVector* vec_truth_neg =
new TLorentzVector();
430 double z_mass = 91187.6;
431 double mass = ((*vec_pos) + (*vec_neg)).M();
432 double mass_truth = ((*vec_truth_pos) + (*vec_truth_neg)).M();
434 double delta_M2 = (
mass*
mass - z_mass*z_mass)/(z_mass*z_mass);
435 double delta_M2_truth = (
mass*
mass - mass_truth*mass_truth)/(mass_truth*mass_truth);
436 double delta_M2_truth_bias = (mass_truth*mass_truth - z_mass*z_mass)/(z_mass*z_mass);
444 double d_bias_truth_pos = +1/vec_truth_pos->Pt()*(1 - vec_truth_pos->P()/vec_pos->P())*1000000.0;
445 double d_bias_truth_neg = -1/vec_truth_neg->Pt()*(1 - vec_truth_neg->P()/vec_neg->P())*1000000.0;
451 double d_mass_bias_truth_pos = +1*delta_M2_truth_bias/vec_truth_pos->Pt()*1000000.0;
452 double d_mass_bias_truth_neg = -1*delta_M2_truth_bias/vec_truth_neg->Pt()*1000000.0;
458 double d_lambda_truth_pos = +1*delta_M2_truth/vec_truth_pos->Pt()*1000000.0;
459 double d_lambda_truth_neg = -1*delta_M2_truth/vec_truth_neg->Pt()*1000000.0;
474 double delta_truth_pos = (vec_truth_pos->P() - vec_pos->P())/vec_truth_pos->P();
475 double delta_truth_neg = (vec_truth_neg->P() - vec_neg->P())/vec_truth_neg->P();
488 double zpt_truth = ((*vec_truth_pos) + (*vec_truth_neg)).
Pt();
490 h_DELTA->Fill(delta_M2/(vec_pos->Pt() - vec_neg->Pt())*1000000);
499 delete vec_truth_pos;
500 delete vec_truth_neg;
548 h_pt->Fill(vec_pos->Pt()/1000.0);
549 h_pt->Fill(vec_neg->Pt()/1000.0);
550 h_pt_pos->Fill(vec_pos->Pt()/1000.0);
551 h_pt_neg->Fill(vec_neg->Pt()/1000.0);
553 h_mass->Fill(((*vec_pos)+(*vec_neg)).M()/1000.0);
576 double temp_pt_pos = muon_pos->Pt();
577 double temp_pt_neg = muon_neg->Pt();
580 double correction = h_corrections->GetBinContent(h_corrections->FindBin(muon_pos->Eta(), muon_pos->Phi()));
583 double pt_true = muon_pos->Pt();
586 muon_pos->SetXYZM(muon_pos->Px()/(1+
correction*pt_true/1000000.0),
587 muon_pos->Py()/(1+
correction*pt_true/1000000.0),
588 muon_pos->Pz()/(1+
correction*pt_true/1000000.0),
591 cout <<
" ** correctMomentum ** mu+ (eta,phi) (" << muon_pos->Eta() <<
", " << muon_pos->Phi() <<
") "
592 <<
" bin = " << h_corrections->FindBin(muon_pos->Eta(), muon_pos->Phi())
599 muon_pos->SetXYZM(muon_pos->Px()*(1-
correction),
606 correction = h_corrections->GetBinContent(h_corrections->FindBin(muon_neg->Eta(), muon_neg->Phi()));
609 double pt_true = muon_neg->Pt();
612 muon_neg->SetXYZM(muon_neg->Px()/(1-
correction*pt_true/1000000.0),
613 muon_neg->Py()/(1-
correction*pt_true/1000000.0),
614 muon_neg->Pz()/(1-
correction*pt_true/1000000.0),
617 cout <<
" ** correctMomentum ** mu- (eta,phi) (" << muon_neg->Eta() <<
", " << muon_neg->Phi() <<
") "
618 <<
" bin = " << h_corrections->FindBin(muon_neg->Eta(), muon_neg->Phi())
625 muon_neg->SetXYZM(muon_neg->Px()*(1-
correction),
632 cout <<
" ** correctMomentum ** mu+ Pt: " << temp_pt_pos <<
" --> " << muon_pos->Pt() <<
" Delta = " << 100*(muon_pos->Pt()-temp_pt_pos)/temp_pt_pos <<
" %"<< endl
633 <<
" mu- Pt: " << temp_pt_neg <<
" --> " << muon_neg->Pt() <<
" Delta = " << 100*(muon_neg->Pt()-temp_pt_neg)/temp_pt_neg <<
" %"<< endl;
644 double z_mass = 91187.6 +
m_shift;
645 double mass = ((*v_pos) + (*v_neg)).M();
646 double delta_M2 = (
mass*
mass - z_mass*z_mass)/(z_mass*z_mass);
650 hist->Fill(v_pos->Eta(), v_pos->Phi(), +1*
m_factor*delta_M2/v_pos->Pt()*1000000.0);
651 hist->Fill(v_neg->Eta(), v_neg->Phi(), -1*
m_factor*delta_M2/v_neg->Pt()*1000000.0);
654 hist->Fill(v_pos->Eta(), v_pos->Phi(), +1*delta_M2/2);
655 hist->Fill(v_neg->Eta(), v_neg->Phi(), +1*delta_M2/2);
663 double z_mass = 91187.6 +
m_shift;
664 double mass = ((*v_pos) + (*v_neg)).M();
665 double delta_M2 = (
mass*
mass - z_mass*z_mass)/(z_mass*z_mass);
672 hist->Fill(v_pos->Eta(), +1*delta_M2/2);
673 hist->Fill(v_neg->Eta(), +1*delta_M2/2);
680 double z_mass = 91187.6 +
m_shift;
681 double mass = ((*v_pos) + (*v_neg)).M();
682 double delta_M2 = (
mass*
mass - z_mass*z_mass)/(z_mass*z_mass);
689 hist->Fill(+1*delta_M2/2);
690 hist->Fill(+1*delta_M2/2);
697 h_QoverPt->Fill((*v_pos).Phi(), 1000/(*v_pos).Pt());
698 h_QoverPt->Fill((*v_neg).Phi(), -1000/(*v_neg).Pt());
700 h_QoverPt3D->Fill((*v_pos).Eta(), (*v_pos).Phi(), 1000/(*v_pos).Pt());
701 h_QoverPt3D->Fill((*v_neg).Eta(), (*v_neg).Phi(), -1000/(*v_neg).Pt());
707 double correction = h_corrections->GetBinContent(h_corrections->FindBin(muon_pos->Eta(), muon_pos->Phi()));
712 correction = h_corrections->GetBinContent(h_corrections->FindBin(muon_neg->Eta(), muon_neg->Phi()));
720 double deltazd0 = -(zd0_muon_p - zd0_muon_n);
721 hist->Fill(v_pos->Eta(),v_pos->Phi(), deltazd0);
722 hist->Fill(v_neg->Eta(),v_neg->Phi(), -1.*deltazd0);
727 double deltazd0 = (zd0_muon_p - zd0_muon_n);
728 if (
pn == +1)
hist->Fill(deltazd0);
729 if (
pn == -1)
hist->Fill(-1.*deltazd0);
731 hist->Fill(-1.*deltazd0);
732 hist->Fill(deltazd0);
745 if (
m_isMC && iteration == 1) {
782 m_file.mkdir(Form(
"Iteration%i",iteration));
783 m_file.cd(Form(
"Iteration%i",iteration));
865 cout<<
"ProfileZwithIterativeGaussFit(): No histogram supplied!"<<endl;
872 int num_bins_x =
hist->GetXaxis()->GetNbins();
873 int num_bins_y =
hist->GetYaxis()->GetNbins();
875 double num_not_converged = 0;
878 double max_sigma = 0;
879 double min_sigma = 0;
886 for (
int i = 1;
i < num_bins_x+(num_bins==1);
i+=num_bins) {
888 for (
int j = 1; j < num_bins_y+(num_bins==1); j+=num_bins) {
891 int index_y = j/num_bins;
893 current_proj =
hist->ProjectionZ(Form(
"%s_GaussProjection_%i_%i",
hist->GetName(),
index, index_y),
i,
i+num_bins-1,j,j+num_bins-1);
894 current_proj->SetTitle(Form(
"%s - Bin %i x %i",
hist->GetName(),
index,index_y));
896 double current_mu,current_err_mu, current_sigma, current_err_sigma;
898 if(current_proj->GetEntries() < minEntries) {
899 if (
m_PrintLevel >= 1) cout <<
" ** profileZwithIterativeGaussFit ** fitting " <<
hist->GetName() <<
" bin (" <<
index <<
", " << index_y <<
") "
900 <<
" Not enough entries " << current_proj->GetEntries() <<
" < " << minEntries << endl;
905 current_err_sigma = 1;
907 if (fDebug) std::cout<<
"WARNING: Only "<<current_proj->GetEntries()<<
" entries in bin "<<
index<<
","<<index_y<<
" in histogram " <<
hist->GetName()<< std::endl;
911 if (
m_PrintLevel >= 1) cout <<
" ** profileZwithIterativeGaussFit ** fitting " <<
hist->GetName() <<
" bin (" <<
index <<
", " << index_y <<
") " << endl;
913 IterativeGaussFit(current_proj, current_mu, current_err_mu, current_sigma, current_err_sigma);
915 if (current_sigma > max_sigma || max_sigma == 0) max_sigma = current_sigma;
916 if (current_sigma < min_sigma || min_sigma == 0) min_sigma = current_sigma;
917 if (current_mu > max_mu || max_mu == 0) max_mu = current_mu;
918 if (current_mu < min_mu || min_mu == 0) min_mu = current_mu;
922 float x_coord = (
hist->GetXaxis()->GetBinLowEdge(
i) +
hist->GetXaxis()->GetBinUpEdge(
i+num_bins-1))/2;
923 float y_coord = (
hist->GetYaxis()->GetBinLowEdge(j) +
hist->GetYaxis()->GetBinUpEdge(j+num_bins-1))/2;
925 if (sigma_graph) sigma_graph->Fill(x_coord,y_coord,current_sigma);
926 if (mu_graph) mu_graph->Fill(x_coord,y_coord,current_mu);
929 if (sigma_err_graph) sigma_err_graph->Fill(x_coord,y_coord,current_err_sigma);
930 if (mu_err_graph) mu_err_graph->Fill(x_coord,y_coord,current_err_mu);
938 mu_graph->GetXaxis()->SetTitle(
hist->GetXaxis()->GetTitle());
939 mu_graph->GetYaxis()->SetTitle(
hist->GetYaxis()->GetTitle());
940 mu_graph->GetYaxis()->SetTitleOffset(1);
941 mu_graph->GetZaxis()->SetTitle(
hist->GetZaxis()->GetTitle());
942 mu_graph->GetZaxis()->SetTitleOffset(1.2);
943 mu_graph->SetTitle(
"" );
949 sigma_graph->GetXaxis()->SetTitle(
hist->GetXaxis()->GetTitle());
950 sigma_graph->GetYaxis()->SetTitle(
hist->GetYaxis()->GetTitle());
951 sigma_graph->GetYaxis()->SetTitleOffset(1);
952 sigma_graph->GetZaxis()->SetTitle(
hist->GetZaxis()->GetTitle());
953 sigma_graph->GetZaxis()->SetTitleOffset(1.2);
954 sigma_graph->SetTitle(
"" );
960 mu_err_graph->GetXaxis()->SetTitle(
hist->GetXaxis()->GetTitle());
961 mu_err_graph->GetYaxis()->SetTitle(
hist->GetYaxis()->GetTitle());
962 mu_err_graph->GetYaxis()->SetTitleOffset(1);
963 mu_err_graph->GetZaxis()->SetTitle(Form(
"Error of fit #mu: %s",
hist->GetZaxis()->GetTitle()));
964 mu_err_graph->GetZaxis()->SetTitleOffset(1.2);
965 mu_err_graph->SetTitle(
hist->GetTitle());
970 if (sigma_err_graph) {
971 sigma_err_graph->GetXaxis()->SetTitle(
hist->GetXaxis()->GetTitle());
972 sigma_err_graph->GetYaxis()->SetTitle(
hist->GetYaxis()->GetTitle());
973 sigma_err_graph->GetYaxis()->SetTitleOffset(1);
974 sigma_err_graph->GetZaxis()->SetTitle(Form(
"Error of fit #sigma: %s",
hist->GetZaxis()->GetTitle()));
975 sigma_err_graph->GetZaxis()->SetTitleOffset(1.2);
976 sigma_err_graph->SetTitle(
hist->GetTitle());
982 if (num_not_converged || num_skipped) std::cout<<
"Fit Results for histogram: "<<
hist->GetName()<<std::endl;
983 if (num_not_converged) std::cout<<
"Non Convergent Bin Fraction: "<<num_not_converged<<
" / " <<num_bins_x*num_bins_y - num_skipped<<std::endl;
984 if (num_skipped) std::cout<<
"Number skipped bins: "<<num_skipped<<
" / " <<num_bins_x*num_bins_y<<std::endl;
994 std::cout <<
"Error in ProfileYwithIterativeGaussFit(): Histogram not found" <<std::endl;
999 std::cout <<
"Error in ProfileYwithIterativeGaussFit(): Invalid number of bins to integrate over." <<std::endl;
1003 const int minEntries = 50;
1004 const int fDebug = 0;
1006 int num_bins_x =
hist->GetXaxis()->GetNbins();
1008 if (mu_graph) mu_graph->Rebin(num_bins);
1009 if (sigma_graph) sigma_graph->Rebin(num_bins);
1011 double* errs_mu =
new double[num_bins_x/num_bins + 2];
1012 double* errs_sigma =
new double[num_bins_x/num_bins + 2];
1015 errs_mu[num_bins_x/num_bins + 1] = 0;
1018 errs_sigma[num_bins_x/num_bins + 1] = 0;
1020 double min_sigma = 0;
1021 double max_sigma = 0;
1025 int num_skipped = 0;
1029 for (
int i = 1;
i < (num_bins_x + (num_bins == 1));
i+=num_bins) {
1032 if (num_bins == 1)
index--;
1034 current_proj =
hist->ProjectionY(Form(
"%s_projection_%i",
hist->GetName(),
index),
i,
i+num_bins-1);
1036 double mu, mu_err,
sigma, sigma_err;
1038 if(current_proj->GetEntries() < minEntries) {
1044 if ( fDebug ) std::cout<<
"WARNING: Not enough entries in bin "<<
index<<std::endl;
1049 if (
sigma > max_sigma || max_sigma == 0) max_sigma =
sigma;
1050 if (
sigma < min_sigma || min_sigma == 0) min_sigma =
sigma;
1051 if (
mu > max_mu || max_mu == 0) max_mu =
mu;
1052 if (
mu < min_mu || min_mu == 0) min_mu =
mu;
1056 double value_x = (
hist->GetXaxis()->GetBinLowEdge(
i) +
hist->GetXaxis()->GetBinUpEdge(
i+num_bins-1))/2;
1063 if (sigma_graph) sigma_graph->Fill(value_x,
sigma);
1064 if (mu_graph) mu_graph->Fill(value_x,
mu);
1066 errs_mu[
index + 1] = mu_err;
1067 errs_sigma[
index + 1] = sigma_err;
1069 delete current_proj;
1073 sigma_graph->SetError(errs_sigma);
1076 sigma_graph->GetYaxis()->SetTitleOffset(1.5);
1077 sigma_graph->GetYaxis()->SetTitle(
hist->GetYaxis()->GetTitle());
1078 sigma_graph->GetXaxis()->SetTitle(
hist->GetXaxis()->GetTitle());
1079 sigma_graph->SetTitle(
"");
1083 mu_graph->SetError(errs_mu);
1086 mu_graph->GetYaxis()->SetTitleOffset(1.5);
1087 mu_graph->GetYaxis()->SetTitle(
hist->GetYaxis()->GetTitle());
1088 mu_graph->GetXaxis()->SetTitle(
hist->GetXaxis()->GetTitle());
1089 mu_graph->SetTitle(
"");
1092 if (fDebug && num_skipped) std::cout<<
" Number of skipped bins: "<<num_skipped<<std::endl;
1103 const int iteration_limit = 20;
1104 const float percent_limit = 0.01;
1105 const float fit_range_multiplier = 1.5;
1106 const int fDebug = 0;
1111 double current_sigma;
1112 double mu_percent_diff;
1113 double sigma_percent_diff;
1116 if (fDebug) std::cout<<
"Error in Anp::IterativeGaussFit(): Histogram to be fit is missing" <<std::endl;
1122 TF1* fit_func =
new TF1(
"fit_func",
"gaus");
1124 int bad_fit =
hist->Fit(fit_func,
"QN");
1126 if (fDebug && bad_fit) std::cout <<
"BAD INITIAL FIT: "<<
hist->GetTitle() << std::endl;
1128 last_mu = fit_func->GetParameter(1);
1129 last_sigma = fit_func->GetParameter(2);
1131 if (bad_fit) last_mu =
hist->GetMean();
1134 if (fabs(last_mu -
hist->GetMean()) > 5*
hist->GetBinWidth(1)) last_mu =
hist->GetMean();
1136 fit_func->SetRange(last_mu-fit_range_multiplier*last_sigma,last_mu+fit_range_multiplier*last_sigma);
1140 while ( iteration < iteration_limit ) {
1144 double FitRangeLower = last_mu-fit_range_multiplier*last_sigma;
1145 double FitRangeUpper = last_mu+fit_range_multiplier*last_sigma;
1148 if ((FitRangeUpper-FitRangeLower)/
hist->GetBinWidth(1) < 4) {
1149 FitRangeLower -=
hist->GetBinWidth(1);
1150 FitRangeUpper +=
hist->GetBinWidth(1);
1153 fit_func->SetRange(FitRangeLower, FitRangeUpper);
1154 if (
m_PrintLevel >= 3) cout <<
" ** IterativeGaussFit ** fit iter # " << iteration
1155 <<
" new fit range: " << FitRangeLower <<
" --> " << FitRangeUpper << endl;
1159 bad_fit =
hist->Fit(fit_func,
"RQN");
1161 if (fDebug && bad_fit) std::cout<<
" ** BAD FIT ** : bin "<<
hist->GetTitle() <<
" iteration "<<iteration<<std::endl;
1163 current_mu = fit_func->GetParameter(1);
1164 current_sigma = fit_func->GetParameter(2);
1168 float average_mu = (last_mu+current_mu)/2;
1169 float average_sigma = (last_sigma+current_sigma)/2;
1171 if (average_mu == 0) {
1172 if ( fDebug ) std::cout<<
" Average mu = 0 in bin "<<
hist->GetTitle() <<std::endl;
1173 average_mu = current_mu;
1176 if (average_sigma == 0) {
1177 if ( fDebug ) std::cout<<
"Average sigma = 0; Fit Problem in "<<
hist->GetTitle() <<
". "<<last_sigma<<
" "<<current_sigma<<std::endl;
1178 average_sigma = current_sigma;
1181 mu_percent_diff = fabs((last_mu-current_mu)/average_mu);
1182 sigma_percent_diff = fabs((last_sigma-current_sigma)/average_sigma);
1184 if ( mu_percent_diff < percent_limit && sigma_percent_diff < percent_limit )
break;
1186 if (iteration != iteration_limit) {
1187 last_mu = current_mu;
1188 last_sigma = current_sigma;
1191 if (fabs(last_mu -
hist->GetMean()) > 5*
hist->GetBinWidth(1)) {
1192 if (
m_PrintLevel >= 3) cout <<
" ** IterativeGaussFit ** fit iter # " << iteration
1193 <<
" ** WARNING ** last_mu looks bad: " << last_mu
1194 <<
" this iter mu: " << fit_func->GetParameter(1)
1195 <<
" proposed mu: " <<
hist->GetMean()
1197 last_mu =
hist->GetMean();
1201 if (iteration == iteration_limit) {
1202 if (fDebug ) std::cout<<
"WARNING: Fit did not converge to < "<<percent_limit*100<<
"% in "<<
hist->GetTitle() <<
" in "<<iteration_limit<<
" iterations. Percent Diffs: "<<mu_percent_diff*100<<
"% (Mu),"<<
" "<<sigma_percent_diff*100<<
"% (Sigma)"<<std::endl;
1207 mu_err = fit_func->GetParError(1);
1208 sigma = current_sigma;
1209 sigma_err = fit_func->GetParError(2);
1211 hist->GetListOfFunctions()->Add(fit_func);
1214 cout <<
" ** IterativeGaussFit ** fit result: histo name " <<
hist->GetName() <<
" title: " <<
hist->GetTitle() << endl
1215 <<
" mu = " <<
mu <<
" +- " << mu_err << endl
1216 <<
" sigma = " <<
sigma <<
" +- " << sigma_err
1225 cout <<
" ** IterativeGaussFit ** Please type RETURN to continue:\n>";
1226 getline(cin,
input);
1341 double RangeUpper =
hist->GetBinContent(
hist->GetMaximumBin());
1342 double RangeLower =
hist->GetBinContent(
hist->GetMinimumBin());
1344 double NewRangeUpper = RangeUpper;
1345 double NewRangeLower = -RangeUpper;
1347 if (RangeUpper < - RangeLower) {
1348 NewRangeUpper = -RangeLower;
1349 NewRangeLower = RangeLower;
1352 NewRangeUpper *= 1.01;
1353 NewRangeLower *= 1.01;
1356 cout <<
" ** SymmetrizeHisto ** old range: " << RangeLower <<
" --> " << RangeUpper << endl;
1357 cout <<
" new range: " << NewRangeLower <<
" --> " << NewRangeUpper << endl;
1360 hist->SetMaximum(NewRangeUpper);
1361 hist->SetMinimum(NewRangeLower);
1369 if (
m_PrintLevel>=3) cout <<
" ** HistogramConditioning ** START ** hist = " <<
hist->GetName() << endl;
1371 double MinEntriesMPB = 15;
1372 Int_t NGroupBins = 2;
1376 Int_t MostPopulatedBin = (
hist->GetMaximumBin());
1377 double EntriesMPB =
hist->GetBinContent(MostPopulatedBin);
1378 if (EntriesMPB < MinEntriesMPB) {
1380 if ((EntriesMPB +
hist->GetBinContent(MostPopulatedBin+1) +
hist->GetBinContent(MostPopulatedBin-1)) > MinEntriesMPB) {
1388 Bool_t DivisorFound =
false;
1389 while (!DivisorFound) {
1390 if (
hist->GetNbinsX() % NGroupBins == 0) {
1391 DivisorFound =
true;
1394 DivisorFound =
false;
1398 Int_t NBinsWas =
hist->GetNbinsX();
1400 if (
m_PrintLevel>=1) cout <<
" ** HistogramConditioning ** histogram had to be rebinned by: " << NGroupBins
1401 <<
" NBins was: " << NBinsWas <<
" and now is: " <<
hist->GetNbinsX() << endl;