void erfFit(pair<int, double> bump, vector<pair<int, double> > shower, TH1D* energyResoX, TH1D* energyResoY, TH1D* posResoX, TH1D* posResoY) { TF1* erfX = new TF1("erfX", erfPulse, -5, 5, 3); erfX->SetParameters(IDtoX(bump.first), 1., bump.second); TF1* erfY = new TF1("erfY", erfPulse, -5, 5, 3); erfY->SetParameters(IDtoY(bump.first), 1., bump.second); TH2D* histo = new TH2D("histo", "ShowerFit", 11, -5, 5, 11, -5, 5); vector<pair<int, double> >::iterator b; for (b=shower.begin(); b!=shower.end(); b++) { histo->Fill(IDtoX(b->first), IDtoY(b->first), b->second); } TH1D* histoX = histo->ProjectionX(); TH1D* histoY = histo->ProjectionY(); histoX->Fit(erfX, "Q"); energyResoX->Fill(erfX->Integral(-5, 5)-energyGamma); posResoX->Fill(erfX->GetParameter(0)); histoY->Fit(erfY, "Q"); energyResoY->Fill(erfY->Integral(-5, 5)-energyGamma); posResoY->Fill(erfY->GetParameter(0)); ` }
/*--------------------------------------------*/ void FitCosTheta() /*--------------------------------------------*/ { //TH1F *brgauss = new TH1F("breitg","", 131, 0, 130); TFile * file = new TFile("cosTheta_allnadjieh.root","READ"); TH1D * h = (TH1D*)file->Get("cosThetaGen"); //h->Scale(1./h->Integral()); h->Sumw2(); /*TFile * file= new TFile("WPol_SelectedTTBars.root","read"); TH1D* h = (TH1D*)file->Get("costheta_mm/hCosThetaPosLepton_Gen");*/ h->Rebin(10); //h->Scale(1./h->Integral()); TF1 *f = new TF1("f",cosTheta, -1, 1 ,3); Double_t par[3]; par[0] = 1; par[1] = 1; par[2] = h->GetEntries(); f->SetParameters(par); h->Fit(f, "RBO"); h->Fit(f, "RBO"); h->Fit(f, "RBO"); h->Fit(f, "RBO"); h->Fit(f, "RBO"); }
void FindMCToDataScaleFactor() { std::string data_filename = "~/data/out/v92/total.root"; TFile* data_file = new TFile(data_filename.c_str(), "READ"); std::string data_plotname = "TME_Al50_EvdE/all_particles/SiL_EvdE"; TH2F* hEvdE_data = (TH2F*) data_file->Get(data_plotname.c_str()); hEvdE_data->SetDirectory(0); data_file->Close(); std::string MC_filename = "plots.root"; TFile* MC_file = new TFile(MC_filename.c_str(), "READ"); std::string MC_plotname = "hAll_EvdE_SiL"; TH2F* hEvdE_MC = (TH2F*) MC_file->Get(MC_plotname.c_str()); hEvdE_MC->SetDirectory(0); MC_file->Close(); double energy_slice = 3000; int energy_slice_bin = hEvdE_data->GetXaxis()->FindBin(energy_slice); TH1D* hDataProjection = hEvdE_data->ProjectionY("_py", energy_slice_bin, energy_slice_bin); energy_slice_bin = hEvdE_MC->GetXaxis()->FindBin(energy_slice); TH1D* hMCProjection = hEvdE_MC->ProjectionY("_py", energy_slice_bin, energy_slice_bin); TFitResultPtr data_fit_result = hDataProjection->Fit("gaus", "S"); TFitResultPtr MC_fit_result = hMCProjection->Fit("gaus", "S"); hDataProjection->SetLineColor(kBlack); hDataProjection->GetXaxis()->SetRangeUser(0, energy_slice); hDataProjection->Draw(); hMCProjection->SetLineColor(kRed); hMCProjection->GetXaxis()->SetRangeUser(0, energy_slice); hMCProjection->Draw("SAME"); //EvdE_data->Draw("COLZ"); double data_mean = data_fit_result->Parameter(1); double MC_mean = MC_fit_result->Parameter(1); double scale_factor = data_mean / MC_mean; std::cout << "data / MC = " << data_mean << " / " << MC_mean << " = " << scale_factor << std::endl; // Go through the MC projection and scale each bin by the scale factor int n_bins = hMCProjection->GetNbinsX(); double min_x = hMCProjection->GetXaxis()->GetXmin(); double max_x = hMCProjection->GetXaxis()->GetXmax(); TH1F* hMCProjection_scaled = new TH1F("hMCProjection_scaled", "", n_bins,min_x,max_x); for (int i_bin = 1; i_bin <= n_bins; ++i_bin) { double old_energy = hMCProjection->GetBinLowEdge(i_bin); double new_energy = old_energy * scale_factor; double old_bin_content = hMCProjection->GetBinContent(i_bin); hMCProjection_scaled->Fill(new_energy, old_bin_content); } hMCProjection_scaled->Draw(); hDataProjection->Draw("SAME"); }
TH2D* GetJetCorrFunc2D_ZYAM(int itrg, int jass) { TH2D* hcorr = (TH2D*)GetRawCorrFunc2D_ratio(itrg,jass); TH2D* hcorr_clone = (TH2D*)hcorr->Clone(Form("corr_clone_itrg%d_jass%d",itrg,jass)); hcorr_clone->Reset(); for(int ietabin=1;ietabin<=hcorr->GetNbinsX();ietabin++) { TH1D* hcorrphi = (TH1D*)hcorr->ProjectionY(Form("corrphi_%d",ietabin),ietabin,ietabin,"e"); // hcorrphi->SetAxisRange(0.35,1.57,"X"); double histminX = hcorrphi->GetBinCenter(hcorrphi->GetMinimumBin()); double histminY = hcorrphi->GetBinContent(hcorrphi->GetMinimumBin()); fitfunc = new TF1(Form("fitfunc_%d",ietabin),"[0]+[1]*(x-[2])*(x-[2])",0.6,1.2); fitfunc->SetParameters(histminY,1,histminX); fitfunc->SetParLimits(1,0,0.10000); // fitfunc->SetParLimits(2,0.35,1.57); for(int ifit=0;ifit<3;ifit++) hcorrphi->Fit(Form("fitfunc_%d",ietabin),"RNO"); float level = fitfunc->GetParameter(0); for(int iphibin=1;iphibin<=hcorr->GetNbinsY();iphibin++) hcorr_clone->SetBinContent(ietabin,iphibin,hcorr->GetBinContent(ietabin,iphibin)-level); } float max = hcorr_clone->GetBinContent(hcorr_clone->GetMaximumBin()); hcorr_clone->SetAxisRange(ymin,max*1.3,"Z"); return hcorr_clone; }
void fitSlices(TH2* hCorr, TF1* func){ int nBins = hCorr->GetNbinsX(); TH1D* hMean = new TH1D(Form("%s_1",hCorr->GetName()),"",nBins,hCorr->GetXaxis()->GetXmin(),hCorr->GetXaxis()->GetXmax()); TH1D* hSigma = new TH1D(Form("%s_2",hCorr->GetName()),"",nBins,hCorr->GetXaxis()->GetXmin(),hCorr->GetXaxis()->GetXmax()); for(int i = 1; i < nBins+1; ++i){ int bin = nBins - i; TH1D* h = hCorr->ProjectionY(Form("%s_bin%d",hCorr->GetName(),bin),i,i); func->SetParameter(0,h->GetMaximum()); func->SetParameter(1,h->GetMean()); func->SetParameter(2,h->GetRMS()); if(useFits) h->Fit(func); hMean->SetBinContent(i,func->GetParameter(1)); hMean->SetBinError(i,func->GetParError(1)); hSigma->SetBinContent(i,func->GetParameter(2)); hSigma->SetBinError(i,func->GetParError(2)); if(onlySaveTable){ h->Delete(); } } }
//------------------------------------------------------------------------ void fitSlices(TH2* hCorr, TF1* func){ int nBins = hCorr->GetNbinsX(); TH1D* hMean = new TH1D(Form("%s_1",hCorr->GetName()),"",nBins,hCorr->GetXaxis()->GetXmin(),hCorr->GetXaxis()->GetXmax()); TH1D* hSigma = new TH1D(Form("%s_2",hCorr->GetName()),"",nBins,hCorr->GetXaxis()->GetXmin(),hCorr->GetXaxis()->GetXmax()); for(int i = 1; i < nBins+1; i++){ int bin = nBins - i; TH1D* h = hCorr->ProjectionY(Form("%s_bin%d",hCorr->GetName(),bin),i,i); func->SetParameter(0,h->GetMaximum()); func->SetParameter(1,h->GetMean()); func->SetParameter(2,h->GetRMS()); h->Fit(func,"Q"); /*hMean->SetBinContent(i,func->GetParameter(1)); hMean->SetBinError(i,func->GetParError(1)); hSigma->SetBinContent(i,func->GetParameter(2)); hSigma->SetBinError(i,func->GetParError(2));*/ hMean->SetBinContent(i,h->GetMean()); hMean->SetBinError(i,func->GetParError(1)); //errors are not use later for the actual table hSigma->SetBinContent(i,h->GetRMS()); hSigma->SetBinError(i,func->GetParError(2)); } }
void tofQAMaker( string file, int xRange = 10 ){ TFile * f = new TFile( file.c_str(), "READ" ); TH1D * tpc = (TH1D*)f->Get( "zvertex" ); TH1D * vpd = (TH1D*)f->Get( "zvertexVPD" ); TH1D * delta = (TH1D*)f->Get( "zvertexDelta" ); Reporter rp( "vertexQA.pdf" ); TF1* gaus = new TF1( "g", "gaus" ); gaus->SetRange( -xRange, xRange ); rp.newPage(); tpc->Draw(); rp.savePage(); rp.newPage(); vpd->Draw(); rp.savePage(); rp.newPage(); delta->GetXaxis()->SetRangeUser( -25, 25 ); gStyle->SetOptFit( 111 ); delta->Draw(); delta->Fit( gaus, "R" ); rp.savePage(); }
TH2D* GetJetCorrFunc2D_doublegaussian(int itrg, int jass) { TH2D* hcorr = (TH2D*)GetRawCorrFunc2D_ratio(itrg,jass); TH2D* hcorr_clone = (TH2D*)hcorr->Clone(Form("corr_clone_itrg%d_jass%d",itrg,jass)); hcorr_clone->Reset(); for(int ietabin=1;ietabin<=hcorr->GetNbinsX();ietabin++) { TH1D* hcorrphi = (TH1D*)hcorr->ProjectionY(Form("corrphi_%d",ietabin),ietabin,ietabin,"e"); float min = hcorrphi->GetMinimum(); hcorrphi->SetAxisRange(-1,1,"X"); float nearmax = hcorrphi->GetMaximum(); hcorrphi->SetAxisRange(PI-1,PI+1,"X"); float awaymax = hcorrphi->GetMaximum(); TF1* fitfunc = new TF1("fitfunc",doubleGaussian,-PI/2.,3.*PI/2.,5); fitfunc->SetParameters(min,nearmax-min,0.3,awaymax-min,0.5); fitfunc->SetParLimits(0,0,100000); fitfunc->SetParLimits(1,0,100000); fitfunc->SetParLimits(2,0,100000); fitfunc->SetParLimits(3,0,100000); fitfunc->SetParLimits(4,0,100000); for(int ifit=0;ifit<3;ifit++) hcorrphi->Fit(Form("fitfunc_%d",ietabin),"RNO"); float level = fitfunc->GetParameter(0); for(int iphibin=1;iphibin<=hcorr->GetNbinsY();iphibin++) hcorr_clone->SetBinContent(ietabin,iphibin,hcorr->GetBinContent(ietabin,iphibin)-level); delete fitfunc; } float max = hcorr_clone->GetBinContent(hcorr_clone->GetMaximumBin()); hcorr_clone->SetAxisRange(ymin,max*1.3,"Z"); return hcorr_clone; }
void Draw_KL_Test(){ TChain* ch = new TChain("Tree"); TChain* ch1 = new TChain("Tree"); TH1D* his = new TH1D("Klong6g","Klong6g",20,450,550); TH1D* his1 = new TH1D("Klong4g","Klong4g",20,450,550); TH1D* his2 = new TH1D("Klong4gAll","Klong4gAll",60,250,550); for( int i = 0; i< 68; i++){ ch->Add(Form("klongRootFile/kl%d.root" ,4162+i)); ch1->Add(Form("klongRootFile/ks%d.root",4162+i)); } ch->Project(his->GetName() ,"KlongMass[0]","CutCondition==0"); ch1->Project(his1->GetName(),"KlongMass[0]","CutCondition==0"); ch1->Project(his2->GetName(),"KlongMass[0]","CutCondition==0"); TF1* func = new TF1("func","gaus(0)+expo(3)",0,550); func->SetParameter(1,498); func->SetParameter(2,5); TF1* func2 = new TF1("func2","gaus(0)",0,550); func2->SetParameter(1,498); func2->SetParameter(2,5); TCanvas* can = new TCanvas("can","",1200,600); can->Divide(2,1); can->cd(1); his2->Fit(func->GetName(),"","",450,550); his2->Draw(); TF1* func1 = new TF1("Test","gaus",450,550); func1->SetParameter(0,func->GetParameter(0)); func1->SetParameter(1,func->GetParameter(1)); func1->SetParameter(2,func->GetParameter(2)); can->cd(2); his1->SetLineColor(2); his->Draw(); his->Fit(func2->GetName(),"","",450,550); func->Draw("same"); his1->Draw("same"); std::cout<< func2->GetParameter(0) << " " << func->GetParameter(0) << " " << func->GetParameter(0)/func2->GetParameter(0)<< std::endl; std::cout<< func2->Integral(450,550) << " " << func1->Integral(450,550) << " " << func1->Integral(450,550)/func2->Integral(450,550) << std::endl; //ch->Draw("KlongPt[0]:KlongMass[0]>>(400,200,600,50,0,20)","(CutCondition&(1|2|4|8))==0","colz"); gPad->SetLogz(); TText* text = new TText(0.5,0.5,""); TText* text1 = new TText(0.5,0.5,""); text->DrawTextNDC(0.5,0.5,Form("Integral:%2.3lf",func1->Integral(450,550))); text1->DrawTextNDC(0.5,0.6,Form("Integral:%2.3lf",func2->Integral(450,550))); }
void pixelHistos(const char *filenames = "/export/data1/dambach/hardware/ntpls/gradeC/*.root", const char *cuts = "defects == 0") { Init(); // do not forget to load the Utilities.C TH1D *gainH = new TH1D("gainH", "Gain", 1000, 0., 10.); TH1D *pedH = new TH1D("pedH", "Pedestal", 1000, -1000., 1000.); TH1D *noiseH = new TH1D("noiseH", "Noise", 1100, -100., 1000.); TH1D *tthrH = new TH1D("tthrH", "Trimmed Threshold", 100, 0., 100.); TChain *c = new TChain("mod"); c->Add(filenames); canvas->Clear(); canvas->Divide(2,2); canvas->cd(1); InitPad("log"); gainH->GetXaxis()->SetTitle("ADC units/DAC units"); gainH->GetYaxis()->SetTitle("# pixels"); gainH->GetXaxis()->SetTitleSize(0.055); gainH->GetYaxis()->SetTitleSize(0.055); c->Draw("gain>>gainH", cuts); TBox *box = new TBox(); canvas->cd(2); InitPad("log"); pedH->GetXaxis()->SetTitle("DAC units"); pedH->GetYaxis()->SetTitle("# pixels"); pedH->GetXaxis()->SetTitleSize(0.055); pedH->GetYaxis()->SetTitleSize(0.055); c->Draw("ped>>pedH", cuts); canvas->cd(3); InitPad("log"); noiseH->GetXaxis()->SetTitle("e^{-}"); noiseH->GetYaxis()->SetTitle("# pixels"); noiseH->GetYaxis()->SetRangeUser(0.1,400000); noiseH->GetXaxis()->SetTitleSize(0.055); noiseH->GetYaxis()->SetTitleSize(0.055); c->Draw("noise>>noiseH", cuts); noiseH->Fit("gaus"); // box->SetFillStyle(3003); // box->SetFillColor(kBlue); // box->DrawBox(0.1,0.,500.,400000.); // box->SetFillColor(kRed); // box->DrawBox(500.,0.,1000.,400000.); canvas->cd(4); tthrH->GetXaxis()->SetTitle("DAC units"); tthrH->GetYaxis()->SetTitle("# pixels"); tthrH->GetXaxis()->SetTitleSize(0.055); tthrH->GetYaxis()->SetTitleSize(0.055); InitPad("log"); c->Draw("tthr>>tthrH", cuts); }
void DynamicExec() { // Example of function called when a mouse event occurs in a pad. // When moving the mouse in the canvas, a second canvas shows the // projection along X of the bin corresponding to the Y position // of the mouse. The resulting histogram is fitted with a gaussian. // A "dynamic" line shows the current bin position in Y. // This more elaborated example can be used as a starting point // to develop more powerful interactive applications exploiting CINT // as a development engine. // // Author: Rene Brun TObject *select = gPad->GetSelected(); if(!select) return; if (!select->InheritsFrom("TH2")) {gPad->SetUniqueID(0); return;} TH2 *h = (TH2*)select; gPad->GetCanvas()->FeedbackMode(kTRUE); //erase old position and draw a line at current position int pyold = gPad->GetUniqueID(); int px = gPad->GetEventX(); int py = gPad->GetEventY(); float uxmin = gPad->GetUxmin(); float uxmax = gPad->GetUxmax(); int pxmin = gPad->XtoAbsPixel(uxmin); int pxmax = gPad->XtoAbsPixel(uxmax); if(pyold) gVirtualX->DrawLine(pxmin,pyold,pxmax,pyold); gVirtualX->DrawLine(pxmin,py,pxmax,py); gPad->SetUniqueID(py); Float_t upy = gPad->AbsPixeltoY(py); Float_t y = gPad->PadtoY(upy); //create or set the new canvas c2 TVirtualPad *padsav = gPad; TCanvas *c2 = (TCanvas*)gROOT->GetListOfCanvases()->FindObject("c2"); if(c2) delete c2->GetPrimitive("Projection"); else c2 = new TCanvas("c2","Projection Canvas",710,10,700,500); c2->SetGrid(); c2->cd(); //draw slice corresponding to mouse position Int_t biny = h->GetYaxis()->FindBin(y); TH1D *hp = h->ProjectionX("",biny,biny); hp->SetFillColor(38); char title[80]; sprintf(title,"Projection of biny=%d",biny); hp->SetName("Projection"); hp->SetTitle(title); hp->Fit("gaus","ql"); hp->GetFunction("gaus")->SetLineColor(kRed); hp->GetFunction("gaus")->SetLineWidth(6); c2->Update(); padsav->cd(); }
void ReFit(int lo, int hi) { TF1* floo = (TF1*) gROOT->FindObject("floo"); if (floo == 0) return; TH1D* hCe = (TH1D*) gROOT->FindObject("hCe"); if (hCe == 0) return; hCe->GetXaxis()->SetRangeUser(lo,hi); double max = hCe->GetMaximum(); int binmax = hCe->GetMaximumBin(); double xmax = hCe->GetXaxis()->GetBinCenter(binmax); floo->SetParameters(max,xmax,10,max*0.1,50,20,5); hCe->Fit(floo,"Q","",lo,hi); }
void EfficiencyFit(TString inputdata="ROOTfiles/MCstudiesPP_Fine.root", TString label="PP_Fine"){ gStyle->SetTextSize(0.05); gStyle->SetTextFont(42); gStyle->SetPadRightMargin(0.043); gStyle->SetPadLeftMargin(0.18); gStyle->SetPadTopMargin(0.1); gStyle->SetPadBottomMargin(0.145); gStyle->SetTitleX(.0f); TFile* inf = new TFile(inputdata.Data()); TH1D* hEff = (TH1D*)inf->Get("hEff"); TCanvas* c= new TCanvas(Form("c"),"",600,600); hEff->Draw(); TF1 *myfit = new TF1("myfit","[0]+[1]*x+x*x*[2]+x*x*x*[3]+x*x*x*x*[4]", 7, 50); //TF1 *myfit = new TF1("myfit","[0]+[1]*x+x*x*[2]+x*x*x*[3]", 7, 50); //TF1 *myfit = new TF1("myfit","[0]+[1]*x+x*x*[2]", 7, 50); hEff->Fit("myfit","L q","",7,50); hEff->Fit("myfit","L q","",7,50); hEff->Fit("myfit","L q","",7,50); hEff->Fit("myfit","L m","",7,50); double par0=myfit->GetParameter(0); double par1=myfit->GetParameter(1); double par2=myfit->GetParameter(2); double par3=myfit->GetParameter(3); double par4=myfit->GetParameter(4); TString myEffWeight=Form("(%e+%e*Bpt+Bpt*Bpt*%e+Bpt*Bpt*Bpt*%e+Bpt*Bpt*Bpt*Bpt*%e)",par0,par1,par2,par3,par4); std::cout<<myEffWeight<<std::endl; c->SaveAs(Form("plotEffFit/%s.png", label.Data())); TFile *fout=new TFile(Form("plotEffFit/%s.root", label.Data()),"recreate"); fout->cd(); hEff->Write(); myfit->Write(); fout->Close(); }
void RRootHistRead_v1(){ TFile *inputFile = new TFile("pPb_MBSpectra_Combine_-1_1.root"); cout << "Is the file Open " << inputFile->IsOpen() << endl; //Copy the histogram TString hName = "Spectra_NtrkOffline0_inf"; TH1D *h = (TH1D*)inputFile->Get(hName); cout << "Number of bins in the histogram: " << h->GetSize() << "\n"; h->Draw(); double nhBins = h->GetSize(); for (int i=0; i < nhBins; i++) cout << h->GetBinWidth(i) << ", "; cout << endl; // Fit function double nParam = 2, maxValue = 5, minValue = 0; TF1 *fitFn = new TF1("fitFn", fitFunction, minValue, maxValue, nParam); // Set parameters for Fit fitFn->SetParameter(0, 0.008); // Get parameters from Fit double paramFromFit[2]; fitFn->GetParameters(paramFromFit); // Generate data from fit double nBins = 27, pT[27], spectraFromFit[27]; // From the Xi for (int i=0; i < nBins; i++){ spectraFromFit[i] = paramFromFit[0]*exp(-i) + paramFromFit[1]; cout << spectraFromFit[i] << ", "; pT[i] = i; } // Using Integral for the Fit h->Fit(fitFn, "I"); TGraph *g = new TGraph(nBins, pT, spectraFromFit); g->SetMarkerStyle(7); g->SetMarkerSize(1.5); g->SetMarkerColor(kGreen); g->Draw("ALP"); h->Draw("same"); }
TF1* GetFitFunc_ZYAM_MC(TH1D* h) { TH1D* hcorrphi = (TH1D*)h->Clone(h->GetName()); double histminY = hcorrphi->GetBinContent(10); double histminX = 1.0; //hcorrphi->SetAxisRange(-0.01,1.2,"X"); TF1* fitfunc = new TF1(Form("fitfunc_%s",h->GetName()),"[0]+[1]*(x-[2])*(x-[2])",0.8,2.8); //std 0.15-1.8 fitfunc->SetParameters(histminY,0.0002,histminX); fitfunc->SetParLimits(1,0,1000); fitfunc->SetParLimits(2,0.05,1000); for(int ifit=0;ifit<3;ifit++) hcorrphi->Fit(Form("fitfunc_%s",h->GetName()),"RNO"); return fitfunc; }
TF1* GetFitFunc_ZYAM_AllPhysics(TH1D* h) { TH1D* hcorrphi = (TH1D*)h->Clone(h->GetName()); double histminY = hcorrphi->GetBinContent(10); double histminX = 1.0; hcorrphi->SetAxisRange(-0.01,1.5,"X"); TF1* fitfunc = new TF1(Form("fitfunc_%s",h->GetName()),"[0]+[1]*(x-[2])*(x-[2])+[3]*(x-[2])*(x-[2])*(x-[2])",0.5,1.65); //std 0.6 1.55 vs pT ; 0.6 1.8 vs eta fitfunc->SetParameters(histminY,0.0002,histminX,0.0001); fitfunc->SetParLimits(1,0,1000); fitfunc->SetParLimits(2,0.5,1000); // fitfunc->SetParLimits(3,0,1000); for(int ifit=0;ifit<3;ifit++) hcorrphi->Fit(Form("fitfunc_%s",h->GetName()),"RNO"); return fitfunc; }
void exec2() { if (!gPad) { Error("exec2", "gPad is null, you are not supposed to run this macro"); return; } TObject *select = gPad->GetSelected(); if(!select) return; if (!select->InheritsFrom(TH2::Class())) {gPad->SetUniqueID(0); return;} gPad->GetCanvas()->FeedbackMode(kTRUE); //erase old position and draw a line at current position int pyold = gPad->GetUniqueID(); int px = gPad->GetEventX(); int py = gPad->GetEventY(); float uxmin = gPad->GetUxmin(); float uxmax = gPad->GetUxmax(); int pxmin = gPad->XtoAbsPixel(uxmin); int pxmax = gPad->XtoAbsPixel(uxmax); if(pyold) gVirtualX->DrawLine(pxmin,pyold,pxmax,pyold); gVirtualX->DrawLine(pxmin,py,pxmax,py); gPad->SetUniqueID(py); Float_t upy = gPad->AbsPixeltoY(py); Float_t y = gPad->PadtoY(upy); //create or set the new canvas c2 TVirtualPad *padsav = gPad; TCanvas *c2 = (TCanvas*)gROOT->GetListOfCanvases()->FindObject("c2"); if(c2) delete c2->GetPrimitive("Projection"); else c2 = new TCanvas("c2"); c2->cd(); //draw slice corresponding to mouse position TH2 *h = (TH2*)select; Int_t biny = h->GetYaxis()->FindBin(y); TH1D *hp = h->ProjectionX("",biny,biny); char title[80]; sprintf(title,"Projection of biny=%d",biny); hp->SetName("Projection"); hp->SetTitle(title); hp->Fit("gaus","ql"); c2->Update(); padsav->cd(); }
void Draw(){ gSystem->Load("../AnalysisLib/lib/libAnalysisLib.so"); IDHandler* handler = new IDHandler(); CsIImage* image = new CsIImage(handler); TH1D* hisResidual = new TH1D("his","",80,-0.2,0.2); std::ifstream ifs("CalibrationData/OldCalibrationData/CalibrationFactor_8.dat"); if( !ifs.is_open() ) return; Double_t gainList[2716]= {1}; Double_t gainListSpec[2716]={1}; while( !ifs.eof() ){ int ID; double gain; ifs >> ID >> gain; std::cout << ID << " : " << gain << std::endl; gainList[ID] = gain; } std::ifstream ifs1("Data/calibConstKe3.dat"); while( !ifs1.eof()){ int ID; double gain; ifs1 >> ID >> gain; gainListSpec[ID] = gain; } TGraph*gr = new TGraph(); for( int i = 0; i<2716; i++){ image->Fill(i,gainList[i]); gr->SetPoint(gr->GetN(), gainList[i],gainListSpec[i]); if( !gainList[i]==0){ hisResidual->Fill((gainList[i]-gainListSpec[i])/gainList[i]); } } TCanvas* can = new TCanvas("can","",800,800); //gr->Draw("AP"); //image->DrawWithRange("colz",0.8,1.2); hisResidual->Fit("gaus","","",-0.03,0.06); hisResidual->Draw(); //image->Draw(); }
TH1D * smartGausProfileXSQRTN (TH2F * strip, double width){ TProfile * stripProfile = strip->ProfileX () ; // (from FitSlices of TH2.h) double xmin = stripProfile->GetXaxis ()->GetXmin () ; double xmax = stripProfile->GetXaxis ()->GetXmax () ; int profileBins = stripProfile->GetNbinsX () ; std::string name = strip->GetName () ; name += "_smartGaus_X" ; TH1D * prof = new TH1D(name.c_str (),strip->GetTitle (),profileBins,xmin,xmax) ; int cut = 0 ; // minimum number of entries per fitted bin int nbins = strip->GetXaxis ()->GetNbins () ; int binmin = 1 ; int ngroup = 1 ; // bins per step int binmax = nbins ; // loop over the strip bins for (int bin=binmin ; bin<=binmax ; bin += ngroup) { TH1D *hpy = strip->ProjectionY ("_temp",bin,bin+ngroup-1,"e") ; if (hpy == 0) continue ; int nentries = Int_t (hpy->GetEntries ()) ; if (nentries == 0 || nentries < cut) {delete hpy ; continue ;} Int_t biny = bin + ngroup/2 ; TF1 * gaussian = new TF1 ("gaussian","gaus", hpy->GetMean () - width * hpy->GetRMS (), hpy->GetMean () + width * hpy->GetRMS ()) ; gaussian->SetParameter (1,hpy->GetMean ()) ; gaussian->SetParameter (2,hpy->GetRMS ()) ; hpy->Fit ("gaussian","RQL") ; // hpy->GetXaxis ()->SetRangeUser ( hpy->GetMean () - width * hpy->GetRMS (), hpy->GetMean () + width * hpy->GetRMS ()) ; prof->Fill (strip->GetXaxis ()->GetBinCenter (biny), gaussian->GetParameter (1)) ; prof->SetBinError (biny,gaussian->GetParameter (2) / sqrt(hpy->GetEntries())) ; // prof->SetBinError (biny,gaussian->GetParError (1)) ; delete gaussian ; delete hpy ; } // loop over the bins delete stripProfile ; return prof ; }
TH1D* GetJetCorrFunc1D_ZYA1(int itrg, int jass) { TH1D* hcorrphi = (TH1D*)GetRawCorrFunc1D_ratio(itrg,jass); TH1D* hcorrphi_clone = (TH1D*)hcorrphi->Clone(Form("corrphi_clone_itrg%d_jass%d",itrg,jass)); hcorrphi->SetAxisRange(0.5,2.0,"X"); double histminY = hcorrphi->GetMinimum(); TF1* fitfunc = new TF1("fitfunc","[0]+[1]*x",0.8,1.2); fitfunc->SetParameters(histminY,0); fitfunc->FixParameter(1,0); for(int ifit=0;ifit<3;ifit++) hcorrphi->Fit("fitfunc","RNO"); float level = fitfunc->GetParameter(0); for(int ibin=1;ibin<=hcorrphi_clone->GetNbinsX();ibin++) hcorrphi_clone->SetBinContent(ibin,hcorrphi_clone->GetBinContent(ibin)-level); float max = hcorrphi_clone->GetBinContent(hcorrphi_clone->GetMaximumBin()); hcorrphi_clone->SetAxisRange(ymin,max*1.3,"Y"); hcorrphi_clone->SetAxisRange(-PI/2.,3.*PI/2.,"X"); delete fitfunc; return hcorrphi_clone; }
TH1D* GetJetCorrFunc1D_doublegaussian(int itrg, int jass) { TH1D* hcorrphi = (TH1D*)GetRawCorrFunc1D_ratio(itrg,jass); TF1* fitfunc = new TF1("fitfunc",doubleGaussian,-PI/2.,3.*PI/2.,5); fitfunc->SetParameters(hcorrphi->GetMinimum(),hcorrphi->GetMaximum()-hcorrphi->GetMinimum(),0.3,hcorrphi->GetMaximum(hcorrphi->GetMaximum())-hcorrphi->GetMinimum(),0.5); fitfunc->SetParLimits(0,0,100000); fitfunc->SetParLimits(1,0,100000); fitfunc->SetParLimits(2,0,100000); fitfunc->SetParLimits(3,0,100000); fitfunc->SetParLimits(4,0,100000); for(int ifit=0;ifit<3;ifit++) hcorrphi->Fit("fitfunc","RNO"); TH1D* hcorrphi_clone = (TH1D*)hcorrphi->Clone(Form("corrphi_clone_itrg%d_jass%d",itrg,jass)); float level = fitfunc->GetParameter(0); for(int ibin=1;ibin<=hcorrphi_clone->GetNbinsX();ibin++) hcorrphi_clone->SetBinContent(ibin,hcorrphi_clone->GetBinContent(ibin)-level); float max = hcorrphi_clone->GetBinContent(hcorrphi_clone->GetMaximumBin()); hcorrphi_clone->SetAxisRange(ymin,max*1.3,"Y"); delete fitfunc; return hcorrphi_clone; }
double GetV2(int itrg, int jass) { TH2D* hsignal = (TH2D*) gInputfile->Get(Form("%ssignal_trg%d_ass%d",subdir.Data(),itrg,jass)); TH1D* hsignalphi = (TH1D*)hsignal->ProjectionY(Form("signalphi_trg%d_ass%d",itrg,jass),-1,hsignal->GetXaxis()->FindBin(-1.0),"e"); hsignalphi->Scale(1.0/hsignalphi->Integral()); TH2D* hmulttrg = (TH2D*) gInputfile->Get(Form("%smult_trg_%d",subdir.Data(),itrg)); TH1D* hmulttrg_proj = (TH1D*)hmulttrg->ProjectionY(Form("mult_trg_proj_%d",itrg),-1,-1); float multtrg_mean = hmulttrg_proj->GetMean(); cout<<multtrg_mean<<endl; TF1* fitfunc = new TF1("fitfunc","1+2*[0]*TMath::Cos(2*x)",-PI/2.,3.*PI/2.); fitfunc->SetParameter(0,0.01); fitfunc->SetParLimits(0,0.0,1.0); hsignalphi->Fit(fitfunc,"RNO"); float V2=fitfunc->GetParameter(0)*(multtrg_mean-1); // float V2 = 0; // for(int i=1;i<=hsignalphi->GetNbinsX();i++) // V2 = V2 + TMath::Cos(2*hsignalphi->GetBinCenter(i))*hsignalphi->GetBinContent(i); //V2 = V2 / hsignalphi->Integral(); //* (multtrg_mean-1); return sqrt(V2); }
TF1* GetFitFunc_ZYAM_pp(TH1D* h) { TH1D* hcorrphi = (TH1D*)h->Clone(h->GetName()); // double histminY = hcorrphi->GetBinContent(hcorrphi->GetMinimumBin()); // double histminX = hcorrphi->GetBinCenter(hcorrphi->GetMinimumBin()); double histminY = hcorrphi->GetBinContent(hcorrphi->FindBin(1.2)); double histminX = hcorrphi->GetBinCenter(hcorrphi->FindBin(1.2)); //hcorrphi->SetAxisRange(-0.01,1.2,"X"); TF1* fitfunc = new TF1(Form("fitfunc_%s",h->GetName()),"[0]+[1]*(x-[2])*(x-[2])+[3]*abs((x-[2])*(x-[2])*(x-[2]))",0.5,2.5); //std 0.6 1.55 vs pT ; 0.6 1.8 vs eta fitfunc->SetParameters(histminY,0.002,histminX,0.0); fitfunc->SetParLimits(1,0,1000); fitfunc->SetParLimits(2,0.2,2.0); // fitfunc->SetParLimits(3,0,100); fitfunc->FixParameter(3,0); for(int ifit=0;ifit<3;ifit++) hcorrphi->Fit(Form("fitfunc_%s",h->GetName()),"NO","",histminX-0.6,histminX+0.6); fitfunc->SetName(Form("fitfunc_%s_%.3f",h->GetName(),fitfunc->GetParameter(0))); return fitfunc; }
void makeOptHist(Int_t nBins, Double_t* cut, Double_t pt, TString iso, TString id, TString opt) { TCanvas* c = new TCanvas(); TString isoTitle, idTitle; TString histName = iso + "Iso_" + id + "_" + opt; TH1D* h = new TH1D(histName, "", nBins, pt, 30.0); if (iso == "track") isoTitle = "track"; else if (iso == "ecal") isoTitle = "ECAL"; else if (iso == "hcal") isoTitle = "HCAL"; else if (iso == "comb") isoTitle = "comb"; if (id == "elec") idTitle = "Electrons"; else if (id == "muon") idTitle = "Muons"; h->Sumw2(); for (Int_t i = 1; i <= nBins; i++) { h->SetBinContent(i, cut[i - 1]); h->SetBinError (i, 1.0); } h->Fit("pol1", "e"); h->GetXaxis()->SetTitle("reco p_{T}"); h->GetYaxis()->SetTitle(isoTitle + " abs iso " + opt + " cut"); h->GetYaxis()->SetRangeUser(0.0, 30.0); h->GetYaxis()->SetTitleOffset(1.0); h->SetTitle(idTitle); h->Draw(); c->SaveAs("./optIsoCut/" + histName + ".png"); h->Write(); }
void FitHist(Double_t * d, char * path) { TH1D* hist = GetHist(path); if (hist == NULL ) { d[0]=-1; d[1]=-1; d[2]=-1; return; } TF1 *func = new TF1 ("fit",DoubleErrf,hist->GetXaxis()->GetXmin(),hist->GetXaxis()->GetXmax(),3); func->SetParameters(0.,0.1,hist->GetEntries()); func->SetParNames ("Mean_value","Sigma","N"); hist->Fit("fit","Q"); //Double_t d[2]; d[0]=func->GetParameter(0); d[1]=func->GetParameter(1); d[2]=func->GetParameter(2); //return d; //cout<<endl<<"for "<<path<<endl<<"mean = "<<func->GetParameter(0)<<endl<<"sigma = "<<func->GetParameter(1)<<endl; }
void sinFittingTest(){ double PI = 3.14159265359; int bins = 16; TH1D* hSin = new TH1D("hSin","hSin",bins,-PI,PI); TF1* func = new TF1("func","[0]*sin(x)",-PI*.5,PI*.5); double amp = 1.5; for (int i=0; i<bins; i++) { double binCenter = hSin->GetBinCenter(i+1); double val = amp*sin(binCenter); hSin->SetBinContent(i+1, val); } hSin->Draw(); hSin->Fit("func", "R"); }
int main(int argc, char *argv[]) { int binCount = 100; if (argc > 2) { std::cout << "too many argument. exit." << std::endl; std::exit(1); } else if (argc == 2) { binCount = std::atoi(argv[1]); --argc; } TApplication app("app", &argc, argv); gStyle->SetOptStat(0); TH1D *hist = new TH1D("hist", "ns vs count", binCount, minNanoSec, maxNanoSec); char filename[256]; GetFileName(filename, "../data/muon_lifetime/muon_lifetime.dat"); std::ifstream ifs(filename); for (int i = 0; !ifs.eof(); ++i) { Double_t data; ifs >> data; if (data) { NanoSecWithError ns; ConvertTdcChannelToNanoSec(i%8, data, &ns); hist->Fill(ns.time); } } hist->Draw(); TF1 *fit = new TF1("fit", "[0] * exp(- x / [1]) + [2]", 1000, 20000); fit->SetParameters(200, 2100, 40); hist->Fit(fit, "R+"); std::cout << "chi^2/ndf: "; std::cout << fit->GetChisquare() / fit->GetNDF() << std::endl; app.Run(); return 0; }
void fitpapvar(const char* infilename, const char* histname) { TFile* infile = new TFile(Form("%s",infilename),"read"); TH1D* hcf = (TH1D*)infile->Get(histname); hcf->GetXaxis()->SetRangeUser(0,2); TF1 *fc2 = new TF1("fc2","[3]+[2]*TMath::Gaus(x,[0],[1])",0.4,1); fc2->SetParameters(1.5,0.3,-0.2,1); // TF1 *fc2 = new TF1("fc2","[1]+[0]*x*x",0,1); // fc2->SetParameters(-0.01,1.0); // TF1 *fc2 = new TF1("fc2","[2]+[1]*x*x*x*x*x",0,1); // fc2->SetParameters(-0.01,0.01,1.0); // TF1 *fc2 = new TF1("fc2","[3]+[2]*x*x+[1]*x*x*x+[0]*x*x*x*x",0.,1); // fc2->SetParameters(-0.01,0.01,1.0,1.0); hcf->Fit("fc2","r","",0.3,1); TH1D* hnew = new TH1D("hnew","hnew",hcf->GetNbinsX(),0,1); for(int i=1;i<=hcf->GetNbinsX();++i){ // cout << i << endl; // cout << hcf->GetBinContent(i)/fc2->Eval(2.*i/hcf->GetNbinsX()) << endl; // cout << hcf->GetBinContent(i) << endl; // cout << fc2->Eval(2.*i/hcf->GetNbinsX()) << endl << endl; hnew->SetBinContent(i, hcf->GetBinContent(i)/fc2->Eval(1.*i/hcf->GetNbinsX())); hnew->SetBinError(i, hcf->GetBinError(i)); } hnew->Draw("same"); fc2->Draw("same"); hnew->SetName(Form("divp4%s",histname)); TFile* ofile = new TFile(Form("divp4%s",infilename),"update"); hnew->Write(); hcf->Write(); ofile->Close(); }
TH2D* GetJetCorrFunc2D_ZYA1(int itrg, int jass) { TH2D* hcorr = (TH2D*)GetRawCorrFunc2D_ratio(itrg,jass); TH2D* hcorr_clone = (TH2D*)hcorr->Clone(Form("corr_clone_itrg%d_jass%d",itrg,jass)); hcorr_clone->Reset(); for(int ietabin=1;ietabin<=hcorr->GetNbinsX();ietabin++) { TH1D* hcorrphi = (TH1D*)hcorr->ProjectionY(Form("corrphi_%d",ietabin),ietabin,ietabin,"e"); hcorrphi->SetAxisRange(0.5,2.0,"X"); double histminY = hcorrphi->GetMinimum(); fitfunc = new TF1("fitfunc","[0]+[1]*x",0.8,1.2); fitfunc->SetParameters(histminY,0); fitfunc->FixParameter(1,0); for(int ifit=0;ifit<3;ifit++) hcorrphi->Fit(Form("fitfunc_%d",ietabin),"RNO"); float level = fitfunc->GetParameter(0); for(int iphibin=1;iphibin<=hcorrphi->GetNbinsY();iphibin++) hcorr_clone->SetBinContent(ietabin,iphibin,hcorr->GetBinContent(ietabin,iphibin)-level); delete fitfunc; } float max = hcorr_clone->GetBinContent(hcorr_clone->GetMaximumBin()); hcorr_clone->SetAxisRange(ymin,max*1.3,"Z"); return hcorr_clone; }
int RateVsVar(unsigned k=1, unsigned l=0){ TCanvas c1; std::string filename = Form("output/DoubleJet%u.root",k); std::string histname; std::string outname; std::string Xname; double xmin; double xmax; double step; if(true){ histname = "hMuVary"; outname = "RateOut/Mu.pdf"; Xname = "L1 muon p_{T} cut [GeV]"; xmin = 8; xmax = 18; step = 1; } if(true){ histname = "leadJetVary"; outname = "RateOut/JetPt.pdf"; Xname = "L1 lead jet p_{T} cut [GeV]"; xmin = 97; xmax = 120; step = 1; } if(false){ histname = "hsubleadJetVary"; outname = "RateOut/Jet2Pt.pdf"; Xname = "L1 sub-lead jet p_{T} cut [GeV]"; xmin = 30; xmax = 60; step = 1; } if(false){ histname = "hMjjVary"; outname = "RateOut/Mjj.pdf"; Xname = "L1 M_{jj} cut [GeV]"; xmin = 620; xmax = 750; step = 10; } TFile *f0 = new TFile(filename.c_str()); TH2D *h1 = (TH2D*)f0->Get(histname.c_str()); TH1D *hout = new TH1D("hout", "hout",20,0,2); TH1D *hdenum = new TH1D("hdenum", "hdenum",20,0,2); hout->Sumw2(); TH1D *h_rate = new TH1D("h_rate","h_rate",(int)(xmax-xmin)/step,xmin,xmax); h_rate->SetFillStyle(0); h_rate->GetXaxis()->SetTitle(Xname.c_str()); h_rate->GetYaxis()->SetTitle("L1 Rate @ 1.5e34cm^{-2}s^{-1} [kHz]"); for(unsigned i=(unsigned)xmin; i<=xmax; i+=(unsigned)step){ cout << i << endl; for(unsigned j=1; j<=(unsigned)h1->GetNbinsX(); ++j){ hout->SetBinContent(j,h1->Integral(j,j,h1->GetYaxis()->FindBin(i),h1->GetNbinsY()+1)); double error=0; h1->IntegralAndError(j,j,h1->GetYaxis()->FindBin(i),h1->GetNbinsY()+1,error); hout->SetBinError(j,error); hdenum->SetBinContent(j,h1->GetBinContent(j,1)); } hout->Divide(hdenum); //hout->Draw(); double rate1pt2=0; double rate1pt5=0; //hout->GetXaxis()->SetRangeUser(0.4,1.6); if(l==0){ TF1 *f1 = new TF1("f1","pol1",0.4,1.6); hout->Fit("f1","R"); rate1pt2 = f1->GetParameter(0) + f1->GetParameter(1)*1.2; rate1pt5 = f1->GetParameter(0) + f1->GetParameter(1)*1.5; } if(l==1){ TF1 *f1 = new TF1("f1","pol2",0.4,1.6); hout->Fit("f1","R"); rate1pt2 = f1->GetParameter(0) + f1->GetParameter(1)*1.2 + f1->GetParameter(2)*1.2*1.2; rate1pt5 = f1->GetParameter(0) + f1->GetParameter(1)*1.5 + f1->GetParameter(2)*1.5*1.5; } h_rate->SetBinContent(h_rate->GetXaxis()->FindBin(i),rate1pt5); //std::cout << "Rate @ 1.2e34: " << rate1pt2 << std::endl; //std::cout << "Rate @ 1.5e34: " << rate1pt5 << std::endl; } h_rate->Draw(); c1.Print(outname.c_str()); c1.Print(outname.c_str()); bool Proceed = true; while(Proceed){ std::string temp; cin >> temp; if(temp == "y") Proceed = false; } return k; }