void makeMuonTimingPlot (std::string fname) { TFile file(fname.c_str()); TDirectoryFile *dir = (TDirectoryFile*)file.Get("Muons"); TList *hlist = dir->GetListOfKeys(); TH1F *h1; bool foundHist = false; for (auto hist : *hlist) { TString name = hist->GetName(); if (!name.Contains("hMuTimeP")) continue; TH1F *h0 = (TH1F*)dir->Get(name.Data()); if (!foundHist) { h1 = (TH1F*)h0->Clone(); h1->Sumw2(); foundHist = true; } else { h1->Add(h0); } } TH1F *h2 = new TH1F("h2", "h2", 200, -100, 100); h2->GetXaxis()->SetTitle("muon time (ns)"); h2->GetYaxis()->SetTitle("Fraction of Muons/ns"); h2->GetYaxis()->SetTitleOffset(1.1); h2->GetXaxis()->SetTitleOffset(0.8); h2->SetTitle("CSC Muon Time"); h2->SetTitleFont(42); h2->SetTitleSize(0.052); h2->Sumw2(); h2->SetLineColor(kCyan+3); h2->SetFillColor(kCyan+3); for (int ibin = 1; ibin <= 200; ibin++) { h2->SetBinContent(ibin, h1->GetBinContent(ibin)); h2->SetBinError(ibin, h1->GetBinError(ibin)); if (ibin == 200) h2->SetEntries(h1->GetEntries()); } TCanvas c1("c1", "c1", 600, 400); gStyle->SetOptStat(""); double rms = h2->GetRMS(); double avg = h2->GetMean(); TLatex cms(0.17, 0.83, "CMS"); cms.SetNDC(); cms.SetTextFont(61); cms.SetTextSize(0.06); TLatex prelim(0.17, 0.81, "Preliminary"); prelim.SetNDC(); prelim.SetTextAlign(13); prelim.SetTextFont(52); prelim.SetTextSize(0.0456); TLatex data(0.17, 0.76, "Data 2016"); data.SetNDC(); data.SetTextAlign(13); data.SetTextFont(52); data.SetTextSize(0.0456); TLatex lumi(0.9, 0.93, "4.0 fb^{-1} (13 TeV)"); lumi.SetNDC(); lumi.SetTextAlign(31); lumi.SetTextFont(42); lumi.SetTextSize(0.052); TLatex mean(0.7, 0.81, Form("Mean %2.1f", avg)); mean.SetNDC(); mean.SetTextAlign(11); mean.SetTextFont(61); mean.SetTextSize(0.06); TLatex stdev(0.7, 0.76, Form("RMS %2.1f", rms)); stdev.SetNDC(); stdev.SetTextAlign(11); stdev.SetTextFont(61); stdev.SetTextSize(0.06); h2->GetXaxis()->SetRangeUser(-6*rms,6*rms); TH1F* h2norm = (TH1F*)h2->DrawNormalized("hist"); gPad->Update(); cms.Draw(); prelim.Draw(); data.Draw(); lumi.Draw(); mean.Draw(); stdev.Draw(); TPaveText *title = (TPaveText*)gPad->GetPrimitive("title"); title->SetBorderSize(0); title->SetFillColor(0); title->SetFillStyle(0); title->SetX1NDC(0.13); title->SetY1NDC(0.88); title->SetX2NDC(0.9); title->SetY2NDC(0.98); title->SetTextFont(42); title->SetTextSize(0.052); title->SetTextAlign(11); c1.Print("plots/muon_time_all.pdf"); c1.Print("plots/muon_time_all.png"); c1.Print("plots/muon_time_all.root"); }
/* root -l 'sigmapeak.C+(0.025)' // Processing sigmapeak.C+(0.025)... background only: bkg_nsigma_0_99 = -0.352803 sig_nsigma_0_59 = -0.608621 sig_nsigma_0_99 = 2.1472 sig_nsigma_60_99 = 4.14042 sig_nsigma_70_90 = 5.57689 sig_nsigma_75_85 = 8.056 */ void sigmapeak(Double_t signal_area=0.025) { Double_t bkg_mean = 0; Double_t bkg_sigma = 0.001; Double_t x[100]; Double_t y[100]; Int_t np = 100; TRandom3 rand; for (int i=0; i<np; ++i) { x[i] = i; y[i] = rand.Gaus(bkg_mean,bkg_sigma); } TGraph* gr = new TGraph(np,x,y); gr->SetNameTitle("gr",Form("#sigma = %0.1f",bkg_sigma)); gr->SetMarkerStyle(7); gr->SetMarkerColor(46); new TCanvas(); gr->Draw("ap"); Double_t bkg_nsigma_0_99 = Nsigma(gr->GetY(), 0,99, bkg_sigma); cout<< "background only: bkg_nsigma_0_99 = " << bkg_nsigma_0_99 <<endl; // add signal Double_t signal_mean = 80; Double_t signal_sigma = 3; for (int i=0; i<gr->GetN(); ++i) { Double_t xx = (gr->GetX()[i] - signal_mean)/signal_sigma; Double_t arg = 0.5*xx*xx; Double_t exp = arg < 50? TMath::Exp(-arg): 0; Double_t signal = signal_area/(TMath::Sqrt(TMath::TwoPi())*signal_sigma) * exp; gr->SetPoint(i, gr->GetX()[i], gr->GetY()[i] + signal); } gr->SetTitle(Form("#sigma_{bkg} = %0.3f signal: area = %0.3f mean = %0.0f sigma = %0.1f", bkg_sigma,signal_area,signal_mean,signal_sigma)); gr->Draw("apl"); gr->Fit("gaus", "R", "", signal_mean - 5*signal_sigma, signal_mean+5*signal_sigma); Double_t fit_area = 2.5 * gr->GetFunction("gaus")->GetParameter("Constant") * gr->GetFunction("gaus")->GetParameter("Sigma"); cout<< "Area under fitted gaussian = " << fit_area <<endl; // titmax(); gPad->Modified(); // to create box (NB: the pad was not drawn yet at this point!) gPad->Update(); TPaveText* tit = (TPaveText*)gPad->GetPrimitive("title"); tit->SetX1NDC(0.); tit->SetX2NDC(1.); tit->SetY1NDC(0.9); tit->SetY2NDC(1.); gPad->Modified(); // to update the pad gPad->Update(); Double_t sig_nsigma_0_59 = Nsigma(gr->GetY(), 0,59, bkg_sigma); cout<< "sig_nsigma_0_59 = " << sig_nsigma_0_59 <<endl; Double_t sig_nsigma_0_99 = Nsigma(gr->GetY(), 0,99, bkg_sigma); cout<< "sig_nsigma_0_99 = " << sig_nsigma_0_99 <<endl; Double_t sig_nsigma_60_99 = Nsigma(gr->GetY(), 60,99, bkg_sigma); cout<< "sig_nsigma_60_99 = " << sig_nsigma_60_99 <<endl; Double_t sig_nsigma_70_90 = Nsigma(gr->GetY(), 70,90, bkg_sigma); cout<< "sig_nsigma_70_90 = " << sig_nsigma_70_90 <<endl; Double_t sig_nsigma_75_85 = Nsigma(gr->GetY(), 75,85, bkg_sigma); cout<< "sig_nsigma_75_85 = " << sig_nsigma_75_85 <<endl; Double_t ys5[100]; smooth5(np, gr->GetY(), ys5); TGraph* gr5 = new TGraph(np, x, ys5); gr5->SetNameTitle("gr5","smoothed on 5 points"); gr5->SetMarkerStyle(7); gr5->SetMarkerColor(2); gr5->SetLineColor(2); new TCanvas; gr5->Draw("apl"); Double_t ys7[100]; smooth7(np, gr->GetY(), ys7); TGraph* gr7 = new TGraph(np, x, ys7); gr7->SetNameTitle("gr7","smoothed on 7 points"); gr7->SetMarkerStyle(7); gr7->SetMarkerColor(8); gr7->SetLineColor(8); new TCanvas; gr7->Draw("apl"); Double_t ys7a[100]; smooth7a(np, gr->GetY(), ys7a); TGraph* gr7a = new TGraph(np, x, ys7a); gr7a->SetNameTitle("gr7a","smoothed on 7a points"); gr7a->SetMarkerStyle(7); gr7a->SetMarkerColor(3); gr7a->SetLineColor(3); new TCanvas; gr7a->Draw("apl"); Double_t ys5g[100]; smooth5g(np, gr->GetY(), ys5g); TGraph* gr5g = new TGraph(np, x, ys5g); gr5g->SetNameTitle("gr5g","smoothed on 5g points"); gr5g->SetMarkerStyle(7); gr5g->SetMarkerColor(4); gr5g->SetLineColor(4); new TCanvas; gr5g->Draw("apl"); Double_t ys5a[100]; smooth5a(np, gr->GetY(), ys5a); TGraph* gr5a = new TGraph(np, x, ys5a); gr5a->SetNameTitle("gr5a","smoothed on 5a points"); gr5a->SetMarkerStyle(7); gr5a->SetMarkerColor(6); gr5a->SetLineColor(6); new TCanvas; gr5a->Draw("apl"); }
void AnalyzeWaveforms(char *WaveformsFile = "Waveforms.root", const int nAddedChannels = 5) { //try to access waveforms file and in case of failure return if(gSystem->AccessPathName(WaveformsFile,kFileExists)) { cout << "Error: file " << WaveformsFile << " does not exsist. Run .x WaveformsFileMaker.C to create it" << endl; return; } // gStyle->SetOptFit(111); // gStyle->SetStatFormat("1.3E"); // gStyle->SetFitFormat("1.3E"); // fetch the list of trees contained in the waveforms file // for every tree generate a waveform graph TFile *f = TFile::Open(WaveformsFile); TList *listOfKeys = f->GetListOfKeys(); Int_t numberOfKeys = listOfKeys->GetEntries(); TList *listOfGraphs = new TList(); // if the waveform file name begins with the string "comparator" it goes in this list TList *listOfCompWaves = new TList(); // if the waveform file name begins with the string "sum output" it goes in this list TList *listOfAdderWaves = new TList(); for(Int_t i = 0; i < numberOfKeys; i++) { TString *keyName = new TString(listOfKeys->At(i)->GetName()); TTree *tree = (TTree*)f->Get(keyName->Data()); Float_t x = 0; Float_t y = 0; tree->SetBranchAddress("x",&x); tree->SetBranchAddress("y",&y); Int_t nentries = tree->GetEntries(); TString *gName = new TString(keyName->Data()); gName->Append(" graph"); TGraphErrors *gWave = new TGraphErrors(nentries); gWave->SetName(gName->Data()); gWave->SetTitle(gName->Data()); gWave->GetXaxis()->SetTitle("Time"); gWave->GetYaxis()->SetTitle("Voltage"); // gWave->SetBit(TH1::kCanRebin); for (Int_t j = 0; j < nentries; j++) { tree->GetEntry(j); gWave->SetPoint(j,x,y); } listOfGraphs->Add(gWave); if(keyName->BeginsWith("comparator")) listOfCompWaves->Add(gWave); if(keyName->BeginsWith("sum output")) listOfAdderWaves->Add(gWave); /* TString *cName = new TString(keyName->Data()); cName->Append(" canvas"); TCanvas *cy = new TCanvas(cName->Data(),cName->Data(),800,600); gWave->Draw("AL"); */ } cout << listOfAdderWaves->GetEntries() << endl; // analysis for waves with no delay // global variables Double_t xMin,xMax,yStart,yEnd; Int_t graphPoints; Double_t step; // comparator outputs waves sum TGraphErrors *gFirstCompWave = (TGraphErrors *)listOfCompWaves->First(); graphPoints = gFirstCompWave->GetN(); gFirstCompWave->GetPoint(0,xMin,yStart); gFirstCompWave->GetPoint(graphPoints - 1,xMax,yEnd); step = (xMax - xMin)/graphPoints; cout << gFirstCompWave->GetName() << endl; cout << "xMin = " << xMin << " xMax = " << xMax << " graphPoints = " << graphPoints << endl; TGraphErrors *gCompSum = new TGraphErrors(graphPoints); gCompSum->SetLineColor(kBlue); gCompSum->SetLineWidth(2); gCompSum->SetName("Comparator Outputs Sum"); gCompSum->SetTitle("Comparator Outputs Sum"); Int_t nCompWaves = listOfCompWaves->GetEntries(); Float_t gx,gy = 0; // Alpha coefficiens are now written "hard coded" here Float_t alphaArray[3] = {0.199,0.201,0.197}; // Deleays coming from the multiplexer are written "hard coded" here Float_t muxDelayArray[3] = {0,77.14E-12,192.01E-12}; for(Int_t i = 0; i < graphPoints; i++) { for(Int_t j = 0; j < nCompWaves; j++) { TGraphErrors *gCompWave = (TGraphErrors *)listOfCompWaves->At(j); gy += (gCompWave->Eval(xMin + i*step + muxDelayArray[j]))*alphaArray[j]; } gCompSum->SetPoint(i,xMin + i*step,gy); gy = 0; } // note that there is a manual correction on the x axis for the comparator waves sum to compare them better in the multigraph Double_t *xArray = gCompSum->GetX(); for(Int_t i = 0; i < graphPoints; i++) { xArray[i] += 5.6E-9; } // TCanvas *cCompSum = new TCanvas("cCompSum","Comparator Outputs Sum",800,600); // gCompSum->Draw("AL"); // adder outputs waves sum // THE ANALYSIS FOR THE ADDER OUTPUT WITH NOT DELAY AND 3 CHANNELS ADDED IS DEPRECATED. // THERE WAS A MISTAKE IN THE DATA TAKING // I'LL KEEP THE CODE HERE FOR FURTHER REFERENCE /* TGraphErrors *gFirstAdderWave = (TGraphErrors *)listOfAdderWaves->First(); graphPoints = gFirstAdderWave->GetN(); gFirstAdderWave->GetPoint(0,xMin,yStart); gFirstAdderWave->GetPoint(graphPoints - 1,xMax,yEnd); step = (xMax - xMin)/graphPoints; cout << gFirstAdderWave->GetName() << endl; cout << "xMin : " << xMin << " xMax : " << xMax << endl; TGraphErrors *gAdderSum = new TGraphErrors(graphPoints); gAdderSum->SetLineWidth(2); // gAdderSum->SetLineColor(kGreen); gAdderSum->SetLineStyle(9); gAdderSum->SetName("Sum of the adder outputs with no delay"); gAdderSum->SetTitle("Sum of the adder outputs with no delay"); Int_t nAdderWaves = listOfAdderWaves->GetEntries(); gy = 0; for(Int_t i = 0; i < graphPoints; i++) { for(Int_t j = 0; j < nAdderWaves; j++) { TGraphErrors *gAdderWave = (TGraphErrors *)listOfAdderWaves->At(j); gy += gAdderWave->Eval(xMin + i*step); } gAdderSum->SetPoint(i,xMin + i*step,gy); gy = 0; } // TCanvas *cAdderSum = new TCanvas("cAdderSum","Sum of the adder outputs",800,600); // gAdderSum->Draw("AL"); TGraphErrors *g3ChannelsSumNoDelay = listOfGraphs->FindObject("sum_output_3_channels_(3) graph"); g3ChannelsSumNoDelay->SetLineColor(kRed); g3ChannelsSumNoDelay->SetLineWidth(2); // note that there is a manual correction on the x axis for the adder output to compare it better in the multigraph Double_t *xArray = g3ChannelsSumNoDelay->GetX(); graphPoints = g3ChannelsSumNoDelay->GetN(); for(Int_t i = 0; i < graphPoints; i++) { xArray[i] -= 600E-12; } // comparison among the computed adder output ,the real one and the sum of the comparator outputs with alpha coefficients correction TMultiGraph *mgSumNoDelay = new TMultiGraph(); mgSumNoDelay->Add(g3ChannelsSumNoDelay); mgSumNoDelay->Add(gAdderSum); mgSumNoDelay->Add(gCompSum); mgSumNoDelay->SetTitle("Collection of graphs for 3 channels sum with no delay"); TCanvas *cmgSumNoDelay = new TCanvas("cmgSumNoDelay", "Collection of graphs for 3 channels sum with no delay", 1200,800); cmgSumNoDelay->Update(); mgSumNoDelay->Draw("AL"); legend = new TLegend(0.6,0.78,0.99,0.99); legend->AddEntry(g3ChannelsSumNoDelay, "Actual sum done by the adder", "lp"); legend->AddEntry(gAdderSum, "Sum of the adder outputs", "lp"); legend->AddEntry(gCompSum, "Sum of the comparator outputs","lp"); legend->Draw(); mgSumNoDelay->GetXaxis()->SetTitle("Seconds"); mgSumNoDelay->GetYaxis()->SetTitle("Volts"); TPaveText *title = (TPaveText*)cmgSumNoDelay->GetPrimitive("title"); title->SetX1NDC(0.009); title->SetY1NDC(0.94); title->SetX2NDC(0.56); title->SetY2NDC(0.99); cmgSumNoDelay->Modified(); */ // analysis for delayed adder outputs Int_t nAdderWaves = listOfAdderWaves->GetEntries(); TGraphErrors *g3ChannelsSumDelay = listOfGraphs->FindObject("sum_output_3_channels_(1-1-1) graph"); g3ChannelsSumDelay->SetLineColor(kRed); g3ChannelsSumDelay->SetLineWidth(2); // Delay coming from cables are written "hard coded" here Float_t cablesDelayArray[3] = {0,1.45E-9,3.21E-9}; graphPoints = g3ChannelsSumDelay->GetN(); //g3ChannelsSumDelay->GetPoint(0,xMin,yStart); //g3ChannelsSumDelay->GetPoint(graphPoints - 1,xMax,yEnd); xMin = 6E-9; xMax = 16E-9; step = (xMax - xMin)/graphPoints; cout << "Sum of the adder outputs with delay" << endl; cout << "xMin : " << xMin << " xMax : " << xMax << endl; TGraphErrors *gAdderSumDelay = new TGraphErrors(graphPoints); gAdderSumDelay->SetLineColor(kBlue); gAdderSumDelay->SetLineWidth(2); gAdderSumDelay->SetName("Sum of the adder outputs with delay"); gAdderSumDelay->SetTitle("Sum of the adder outputs with delay"); gy = 0; // note that there is a manual correction on the delay of 500E-12 to compare the adder output better in the multigraph for(Int_t i = 0; i < graphPoints; i++) { for(Int_t j = 0; j < nAdderWaves; j++) { TGraphErrors *gAdderWave = (TGraphErrors *)listOfAdderWaves->At(j); gy += gAdderWave->Eval(xMin + i*step + cablesDelayArray[j] - 500E-12); } gAdderSumDelay->SetPoint(i,xMin + i*step,gy); gy = 0; } // TCanvas *cSumSumDelay = new TCanvas("cSumSumDelay","Sum of the sum outputs with delay",800,600); // gAdderSumDelay->Draw("APEL"); TMultiGraph *mg3ChannelsSumDelay = new TMultiGraph(); mg3ChannelsSumDelay->Add(gAdderSumDelay); mg3ChannelsSumDelay->Add(g3ChannelsSumDelay); mg3ChannelsSumDelay->SetTitle("Collection of graphs for 3 channels sum with delay"); TCanvas *cmg3ChannelsSumDelay = new TCanvas("cmg3ChannelsSumDelay", "Collection of graphs for 3 channels sum with delay", 1200, 800); cmg3ChannelsSumDelay->Update(); legend = new TLegend(0.6,0.78,0.99,0.99); legend->AddEntry(g3ChannelsSumDelay, "Actual sum done by the adder", "lp"); legend->AddEntry(gAdderSumDelay, "Sum of the delayed adder outputs", "lp"); mg3ChannelsSumDelay->Draw("AL"); legend->Draw(); mg3ChannelsSumDelay->GetXaxis()->SetTitle("Seconds"); mg3ChannelsSumDelay->GetYaxis()->SetTitle("Volts"); TPaveText *title = (TPaveText*)cmg3ChannelsSumDelay->GetPrimitive("title"); title->SetX1NDC(0.009); title->SetY1NDC(0.94); title->SetX2NDC(0.56); title->SetY2NDC(0.99); cmg3ChannelsSumDelay->Modified(); /* // delay test just to try a different method TMultiGraph *mgDelayTest = new TMultiGraph(); Double_t gxTest,gyTest; TGraphErrors *gDelayTest0 = new TGraphErrors(((TGraphErrors *)listOfAdderWaves->At(0))->GetN()); for(Int_t j = 0; j < gDelayTest0->GetN(); j++) { ((TGraphErrors *)listOfAdderWaves->At(0))->GetPoint(j,gxTest,gyTest); gDelayTest0->SetPoint(j,gxTest - cablesDelayArray[0],gyTest); } mgDelayTest->Add(gDelayTest0); TGraphErrors *gDelayTest1 = new TGraphErrors(((TGraphErrors *)listOfAdderWaves->At(1))->GetN()); for(Int_t j = 0; j < gDelayTest1->GetN(); j++) { ((TGraphErrors *)listOfAdderWaves->At(1))->GetPoint(j,gxTest,gyTest); gDelayTest1->SetPoint(j,gxTest - cablesDelayArray[1],gyTest); } mgDelayTest->Add(gDelayTest1); TGraphErrors *gDelayTest2 = new TGraphErrors(((TGraphErrors *)listOfAdderWaves->At(2))->GetN()); for(Int_t j = 0; j < gDelayTest2->GetN(); j++) { ((TGraphErrors *)listOfAdderWaves->At(2))->GetPoint(j,gxTest,gyTest); gDelayTest2->SetPoint(j,gxTest - cablesDelayArray[2],gyTest); } mgDelayTest->Add(gDelayTest2); // TCanvas *cmgDelayTest = new TCanvas("cmgDelayTest", "cmgDelayTest", 800,600); // mgDelayTest->Draw("APEL"); graphPoints = 12000; xMin = 2E-9; xMax = 22E-9; step = (xMax - xMin)/graphPoints; cout << "xMin : " << xMin << " xMax : " << xMax << endl; Double_t delayTest0xMin,delayTest0xMax,delayTest1xMin,delayTest1xMax,delayTest2xMin,delayTest2xMax; gDelayTest0->GetPoint(0,delayTest0xMin,gyTest); gDelayTest0->GetPoint(gDelayTest0->GetN() - 1,delayTest0xMax,gyTest); gDelayTest1->GetPoint(0,delayTest1xMin,gyTest); gDelayTest1->GetPoint(gDelayTest1->GetN() - 1,delayTest1xMax,gyTest); gDelayTest2->GetPoint(0,delayTest2xMin,gyTest); gDelayTest2->GetPoint(gDelayTest2->GetN() - 1,delayTest2xMax,gyTest); cout << delayTest0xMin << endl; TGraphErrors *gAdderSumDelayV2 = new TGraphErrors(graphPoints); for(Int_t i = 0; i < graphPoints; i++) { gyTest = gDelayTest0->Eval(xMin + i*step); gyTest += gDelayTest1->Eval(xMin + i*step); gyTest += gDelayTest2->Eval(xMin + i*step); gAdderSumDelayV2->SetPoint(i,xMin + i*step+ 500E-12,gyTest); } gAdderSumDelayV2->SetLineColor(kGreen); gAdderSumDelayV2->SetLineWidth(2); mg3ChannelsSumDelay->Add(gAdderSumDelayV2); c3ChannelsSumDelay->Modified(); TCanvas *cAdderSumDelayV2 = new TCanvas("cAdderSumDelayV2","cAdderSumDelayV2",800,600); gAdderSumDelayV2->Draw("APL"); */ // List of waves representing the adder output with 5,4,3,2,1 channels in input TGraphErrors *gAdder5Channels = listOfGraphs->FindObject("adder output 5 channels v2 graph"); TGraphErrors *gAdder4Channels = listOfGraphs->FindObject("adder output 4 channels v2 graph"); TGraphErrors *gAdder3Channels = listOfGraphs->FindObject("adder output 3 channels v2 graph"); TGraphErrors *gAdder2Channels = listOfGraphs->FindObject("adder output 2 channels v2 graph"); TGraphErrors *gAdder1Channel = listOfGraphs->FindObject("adder output 1 channel v2 graph"); TGraphErrors *gAdderSingleCh0 = listOfGraphs->FindObject("single sum output ch 0 input 500 mV DT 100 mV graph"); TGraphErrors *gAdderSingleCh1 = listOfGraphs->FindObject("single sum output ch 1 input 500 mV DT 100 mV graph"); TGraphErrors *gAdderSingleCh4 = listOfGraphs->FindObject("single sum output ch 4 input 500 mV DT 100 mV graph"); TGraphErrors *gAdderSingleCh5 = listOfGraphs->FindObject("single sum output ch 5 input 500 mV DT 100 mV graph"); TGraphErrors *gAdderSingleCh7 = listOfGraphs->FindObject("single sum output ch 7 input 500 mV DT 100 mV graph"); TMultiGraph *mgAdder54321Channels = new TMultiGraph(); mgAdder54321Channels->SetTitle("Collection of graphs for different numbers of added channels"); mgAdder54321Channels->Add(gAdder5Channels); mgAdder54321Channels->Add(gAdder4Channels); mgAdder54321Channels->Add(gAdder3Channels); mgAdder54321Channels->Add(gAdder2Channels); mgAdder54321Channels->Add(gAdder1Channel); TCanvas *cmgAdder54321Channels = new TCanvas("cmgAdder54321Channels", "Collection of graphs for different numbers of added channels",1200,800); mgAdder54321Channels->Draw("AL"); cmgAdder54321Channels->Update(); mgAdder54321Channels->GetXaxis()->SetTitle("seconds"); mgAdder54321Channels->GetYaxis()->SetTitle("Volts"); cmgAdder54321Channels->Modified(); // analysis of the linearity of the adder Double_t addedVMax[nAddedChannels]; addedVMax[0] = TMath::MaxElement(gAdder1Channel->GetN(), gAdder1Channel->GetY()); addedVMax[1] = TMath::MaxElement(gAdder2Channels->GetN(), gAdder2Channels->GetY()); addedVMax[2] = TMath::MaxElement(gAdder3Channels->GetN(), gAdder3Channels->GetY()); addedVMax[3] = TMath::MaxElement(gAdder4Channels->GetN(), gAdder4Channels->GetY()); addedVMax[4] = TMath::MaxElement(gAdder5Channels->GetN(), gAdder5Channels->GetY()); Double_t singleVMax[nAddedChannels]; singleVMax[0] = TMath::MaxElement(gAdderSingleCh0->GetN(), gAdderSingleCh0->GetY()); singleVMax[1] = TMath::MaxElement(gAdderSingleCh1->GetN(), gAdderSingleCh1->GetY()); singleVMax[2] = TMath::MaxElement(gAdderSingleCh4->GetN(), gAdderSingleCh4->GetY()); singleVMax[3] = TMath::MaxElement(gAdderSingleCh5->GetN(), gAdderSingleCh5->GetY()); singleVMax[4] = TMath::MaxElement(gAdderSingleCh7->GetN(), gAdderSingleCh7->GetY()); vector<double> expectedVMax(nAddedChannels); Double_t previousVmax = 0; for(Int_t i = 0; i < expectedVMax.size(); i++) { expectedVMax[i] = singleVMax[i] + previousVmax; previousVmax = expectedVMax[i]; cout << "singleVMax[" <<i <<"] = " << singleVMax[i] << endl; cout << "addedVMax[" <<i <<"] = " << addedVMax[i] << endl; cout << "expectedVMax[" <<i <<"] = " << expectedVMax[i] << endl; } TGraph *gAdderLinearity = new TGraph(expectedVMax.size(),&expectedVMax[0],addedVMax); gAdderLinearity->SetTitle("Linearity of the adder"); gAdderLinearity->GetXaxis()->SetTitle("Expected amplitude value (V)"); gAdderLinearity->GetYaxis()->SetTitle("Actual amplitude value (V)"); gAdderLinearity->GetYaxis()->SetTitleOffset(1.3); TCanvas *cgAdderLinearity = new TCanvas("cgAdderLinearity","Linearity of the adder",800,600); cgAdderLinearity->SetGrid(); cgAdderLinearity->Update(); gAdderLinearity->SetMarkerStyle(20); gAdderLinearity->SetMarkerSize(0.8); //gAdderLinearity->Fit("pol1","Q+","",expectedVMax[0],expectedVMax[2]); TF1 *line = new TF1("line","x",expectedVMax.back(),expectedVMax.front()); gAdderLinearity->Draw("APEL"); line->Draw("SAME"); //gAdderLinearity->GetFunction("pol1")->SetRange(expectedVMax.back(),expectedVMax.front());; cgAdderLinearity->SetLeftMargin(0.13); cgAdderLinearity->Modified(); f->Close(); }
void ptBestFit(float BIN_SIZE=5.0,bool BLIND=false,TString MASS,TString NAME) { gROOT->ProcessLine(".x ../../common/styleCMSTDR.C"); gSystem->Load("libHiggsAnalysisCombinedLimit.so"); gROOT->ForceStyle(); gStyle->SetOptStat(0); gStyle->SetOptTitle(0); gROOT->SetBatch(1); gStyle->SetPadRightMargin(0.04); gStyle->SetPadLeftMargin(0.16); gStyle->SetPadTopMargin(0.06); gStyle->SetPadBottomMargin(0.10); gStyle->SetTitleFont(42,"XY"); gStyle->SetTitleSize(0.0475,"XY"); gStyle->SetTitleOffset(0.9,"X"); gStyle->SetTitleOffset(1.5,"Y"); gStyle->SetLabelSize(0.0375,"XY"); RooMsgService::instance().setSilentMode(kTRUE); for(int i=0;i<2;i++) { RooMsgService::instance().setStreamStatus(i,kFALSE); } float XMIN = 80; float XMAX = 200; TFile *f1 = TFile::Open("datacards/datacard_m"+MASS+"_"+NAME+".root"); TFile *f2 = TFile::Open("combine/mlfit.vbfHbb_"+NAME+"_mH"+MASS+".root"); TFile *f3 = TFile::Open("root/sig_shapes_workspace_B80-200.root"); TFile *f4 = TFile::Open("root/data_shapes_workspace_"+NAME+".root"); RooWorkspace *w = (RooWorkspace*)f1->Get("w"); //w->Print(); RooAbsPdf *bkg_model = (RooAbsPdf*)w->pdf("model_s"); RooFitResult *res_s = (RooFitResult*)f2->Get("fit_s"); RooFitResult *res_b = (RooFitResult*)f2->Get("fit_b"); RooRealVar *rFit = dynamic_cast<RooRealVar *>(res_s->floatParsFinal()).find("r"); RooDataSet *data = (RooDataSet*)w->data("data_obs"); int nparS=0,nparB=0; cout << res_s->floatParsFinal().getSize() << endl; cout << res_b->floatParsFinal().getSize() << endl; nparS = res_s->floatParsFinal().getSize(); nparB = res_b->floatParsFinal().getSize(); float chi2sumS = 0.; float chi2sumB = 0.; int nparsum = 0; // if (BLIND) { // res_b->Print(); // } // else { // res_s->Print(); // } w->allVars().assignValueOnly(res_s->floatParsFinal()); // w->Print(); // w->allVars()->Print(); RooWorkspace *wSig = (RooWorkspace*)f3->Get("w"); RooWorkspace *wDat = (RooWorkspace*)f4->Get("w"); const RooSimultaneous *sim = dynamic_cast<const RooSimultaneous *> (bkg_model); const RooAbsCategoryLValue &cat = (RooAbsCategoryLValue &) sim->indexCat(); TList *datasets = data->split(cat,true); TIter next(datasets); //int count = 0; for(RooAbsData *ds = (RooAbsData*)next();ds != 0; ds = (RooAbsData*)next()) { //if (count > 0) return 0; //count++; RooAbsPdf *pdfi = sim->getPdf(ds->GetName()); RooArgSet *obs = (RooArgSet*)pdfi->getObservables(ds); RooRealVar *x = dynamic_cast<RooRealVar *>(obs->first()); RooRealVar *yield_vbf = (RooRealVar*)wSig->var("yield_signalVBF_mass"+MASS+"_"+TString(ds->GetName())); RooRealVar *yield_gf = (RooRealVar*)wSig->var("yield_signalGF_mass"+MASS+"_"+TString(ds->GetName())); TString ds_name(ds->GetName()); //----- get the QCD normalization ----------- RooRealVar *qcd_norm_final = dynamic_cast<RooRealVar *>(res_s->floatParsFinal()).find("CMS_vbfbb_qcd_norm_"+ds_name); RooRealVar *qcd_yield = (RooRealVar*)wDat->var("yield_data_"+ds_name); float Nqcd = exp(log(1.5)*qcd_norm_final->getVal())*qcd_yield->getVal(); float eNqcd = log(1.5)*qcd_norm_final->getError()*Nqcd; cout<<"QCD normalization = "<<Nqcd<<" +/- "<<eNqcd<<endl; TH1 *hCoarse = (TH1*)ds->createHistogram("coarseHisto_"+ds_name,*x); float norm = hCoarse->Integral(); int rebin = BIN_SIZE/hCoarse->GetBinWidth(1); hCoarse->Rebin(rebin); float MIN_VAL = TMath::Max(0.9*hCoarse->GetBinContent(hCoarse->GetMinimumBin()),1.0); float MAX_VAL = 1.3*hCoarse->GetBinContent(hCoarse->GetMaximumBin()); RooDataHist ds_coarse("ds_coarse_"+ds_name,"ds_coarse_"+ds_name,*x,hCoarse); TH1F *hBlind = (TH1F*)hCoarse->Clone("blindHisto_"+ds_name); for(int i=0;i<hBlind->GetNbinsX();i++) { double x0 = hBlind->GetBinCenter(i+1); if (x0 > 100 && x0 < 150) { hBlind->SetBinContent(i+1,0); hBlind->SetBinError(i+1,0); } } RooDataHist ds_blind("ds_blind_"+ds_name,"ds_blind_"+ds_name,*x,hBlind); RooHist *hresid,*hresid0; RooPlot *frame1 = x->frame(); RooPlot *frame2 = x->frame(); if (BLIND) { //cout << "Blind case: " << ds_coarse.GetName() << endl; ds_coarse.plotOn(frame1,LineColor(0),MarkerColor(0)); pdfi->plotOn(frame1,Components("shapeBkg_qcd_"+ds_name+",shapeBkg_top_"+ds_name+",shapeBkg_zjets_"+ds_name),VisualizeError(*res_s,1,kTRUE),FillColor(0),MoveToBack()); pdfi->plotOn(frame1,Components("shapeBkg_qcd_"+ds_name+",shapeBkg_top_"+ds_name+",shapeBkg_zjets_"+ds_name),LineWidth(2),LineStyle(3)); ds_blind.plotOn(frame1); hresid = frame1->residHist(); frame2->addPlotable(hresid,"pE1"); } else { //cout << "Non-blind case: " << ds_coarse.GetName() << endl; ds_coarse.plotOn(frame1); pdfi->plotOn(frame1); //cout << pdfi->getParameters(ds_coarse)->selectByAttrib("Constant",kFALSE)->getSize() << endl; cout<<"chi2/ndof (bkg+sig) = "<<frame1->chiSquare()<<endl; cout << ds_coarse.numEntries() << endl; chi2sumS += frame1->chiSquare()*ds_coarse.numEntries(); nparsum += ds_coarse.numEntries(); //hresid0 = frame1->residHist(); //pdfi->plotOn(frame1,VisualizeError(*res_s,1,kTRUE),FillColor(0),MoveToBack()); pdfi->plotOn(frame1,Components("shapeBkg_qcd_"+ds_name),LineWidth(2),LineStyle(5),LineColor(kGreen+2)); pdfi->plotOn(frame1,Components("shapeBkg_qcd_"+ds_name+",shapeBkg_top_"+ds_name+",shapeBkg_zjets_"+ds_name),LineWidth(2),LineStyle(2),LineColor(kBlack)); cout<<"chi2/ndof (bkg) = "<<frame1->chiSquare()<<endl; chi2sumB += frame1->chiSquare()*ds_coarse.numEntries(); pdfi->plotOn(frame1,Components("shapeBkg_qcd_"+ds_name+",shapeBkg_top_"+ds_name+",shapeBkg_zjets_"+ds_name),LineWidth(2),LineStyle(2),LineColor(kBlack),VisualizeError(*res_s,1,kTRUE),FillColor(0),MoveToBack()); hresid = frame1->residHist(); frame2->addPlotable(hresid,"pE1"); float yield_sig = rFit->getValV()*(yield_vbf->getValV()+yield_gf->getValV()); RooAbsPdf *signal_pdf = (RooAbsPdf*)w->pdf("shapeSig_qqH_"+ds_name); signal_pdf->plotOn(frame2,LineWidth(2),LineColor(kRed),Normalization(yield_sig,RooAbsReal::NumEvent),MoveToBack()); } // hresid0->Print(); // hresid->Print(); // double x2,y2; // for (int i=0; i<3; ++i) { // hresid0->GetPoint(i,x2,y2); // cout << "BKG+SIG\t" << x2 << "\t" << y2 << endl; // hresid->GetPoint(i,x2,y2); // cout << "BKG\t" << x2 << "\t" << y2 << endl; // ds_coarse.get(i); // cout << ds_coarse.weightError(RooAbsData::SumW2) << endl; // cout << endl; // } TCanvas* canFit = new TCanvas("Higgs_fit_"+ds_name,"Higgs_fit_"+ds_name,900,750); canFit->cd(1)->SetBottomMargin(0.4); frame1->SetMinimum(MIN_VAL); frame1->SetMaximum(MAX_VAL); frame1->GetYaxis()->SetNdivisions(510); frame1->GetXaxis()->SetTitleSize(0); frame1->GetXaxis()->SetLabelSize(0); frame1->GetYaxis()->SetTitle(TString::Format("Events / %1.1f GeV",BIN_SIZE)); frame1->Draw(); gPad->Update(); TList *list = (TList*)gPad->GetListOfPrimitives(); //list->Print(); TH1F *hUncH = new TH1F("hUncH"+ds_name,"hUncH"+ds_name,(XMAX-XMIN)/BIN_SIZE,XMIN,XMAX); TH1F *hUncL = new TH1F("hUncL"+ds_name,"hUncL"+ds_name,(XMAX-XMIN)/BIN_SIZE,XMIN,XMAX); TH1F *hUnc2H = new TH1F("hUnc2H"+ds_name,"hUnc2H"+ds_name,(XMAX-XMIN)/BIN_SIZE,XMIN,XMAX); TH1F *hUnc2L = new TH1F("hUnc2L"+ds_name,"hUnc2L"+ds_name,(XMAX-XMIN)/BIN_SIZE,XMIN,XMAX); TH1F *hUncC = new TH1F("hUncC"+ds_name,"hUncC"+ds_name,(XMAX-XMIN)/BIN_SIZE,XMIN,XMAX); RooCurve *errorBand,*gFit,*gQCDFit,*gBkgFit; //list->Print(); if (BLIND) { errorBand = (RooCurve*)list->FindObject("pdf_bin"+ds_name+"_Norm[mbbReg_"+ds_name+"]_errorband_Comp[shapeBkg_qcd_"+ds_name+",shapeBkg_top_"+ds_name+",shapeBkg_zjets_"+ds_name+"]"); gFit = (RooCurve*)list->FindObject("pdf_bin"+ds_name+"_Norm[mbbReg_"+ds_name+"]"+"_Comp[shapeBkg_qcd_"+ds_name+",shapeBkg_top_"+ds_name+",shapeBkg_zjets_"+ds_name+"]"); } else { //errorBand = (RooCurve*)list->FindObject("pdf_bin"+ds_name+"_Norm[mbbReg_"+ds_name+"]_errorband"); errorBand = (RooCurve*)list->FindObject("pdf_bin"+ds_name+"_Norm[mbbReg_"+ds_name+"]_errorband_Comp[shapeBkg_qcd_"+ds_name+",shapeBkg_top_"+ds_name+",shapeBkg_zjets_"+ds_name+"]"); gFit = (RooCurve*)list->FindObject("pdf_bin"+ds_name+"_Norm[mbbReg_"+ds_name+"]"); } gQCDFit = (RooCurve*)list->FindObject("pdf_bin"+ds_name+"_Norm[mbbReg_"+ds_name+"]"+"_Comp[shapeBkg_qcd_"+ds_name+"]"); gBkgFit = (RooCurve*)list->FindObject("pdf_bin"+ds_name+"_Norm[mbbReg_"+ds_name+"]"+"_Comp[shapeBkg_qcd_"+ds_name+",shapeBkg_top_"+ds_name+",shapeBkg_zjets_"+ds_name+"]"); for(int i=0;i<hUncH->GetNbinsX();i++) { double x0 = hUncH->GetBinCenter(i+1); double e1 = fabs(errorBand->Eval(x0)-gBkgFit->Eval(x0)); //double e1 = fabs(errorBand->Eval(x0)-gFit->Eval(x0)); double e2 = eNqcd/hUncH->GetNbinsX(); hUncH->SetBinContent(i+1,sqrt(pow(e2,2)+pow(e1,2))); hUnc2H->SetBinContent(i+1,2*sqrt(pow(e2,2)+pow(e1,2))); hUncL->SetBinContent(i+1,-sqrt(pow(e2,2)+pow(e1,2))); hUnc2L->SetBinContent(i+1,-2*sqrt(pow(e2,2)+pow(e1,2))); hUncC->SetBinContent(i+1,0.); } TPad* pad = new TPad("pad", "pad", 0., 0., 1., 1.); pad->SetTopMargin(0.63); pad->SetFillColor(0); pad->SetFillStyle(0); pad->Draw(); pad->cd(0); hUnc2H->GetXaxis()->SetTitle("m_{bb} (GeV)"); hUnc2H->GetYaxis()->SetTitle("Data - Bkg"); //hUnc2H->GetYaxis()->SetTitle("Data - Fit"); double YMAX = 1.1*frame2->GetMaximum(); double YMIN = -1.1*frame2->GetMaximum(); hUnc2H->GetYaxis()->SetRangeUser(YMIN,YMAX); hUnc2H->GetYaxis()->SetNdivisions(507); // hUnc2H->GetXaxis()->SetTitleOffset(0.9); // hUnc2H->GetYaxis()->SetTitleOffset(1.0); hUnc2H->GetYaxis()->SetTickLength(0.0); // hUnc2H->GetYaxis()->SetTitleSize(0.05); // hUnc2H->GetYaxis()->SetLabelSize(0.04); hUnc2H->GetYaxis()->CenterTitle(kTRUE); hUnc2H->SetFillColor(kGreen); hUnc2L->SetFillColor(kGreen); hUncH->SetFillColor(kYellow); hUncL->SetFillColor(kYellow); hUncC->SetLineColor(kBlack); hUncC->SetLineStyle(7); hUnc2H->Draw("HIST"); hUnc2L->Draw("same HIST"); hUncH->Draw("same HIST"); hUncL->Draw("same HIST"); hUncC->Draw("same HIST"); frame2->GetYaxis()->SetTickLength(0.03/0.4); frame2->Draw("same"); TList *list1 = (TList*)gPad->GetListOfPrimitives(); //list1->Print(); RooCurve *gSigFit = (RooCurve*)list1->FindObject("shapeSig_qqH_"+ds_name+"_Norm[mbbReg_"+ds_name+"]"); TLegend *leg = new TLegend(0.70,0.61,0.94,1.-gStyle->GetPadTopMargin()-0.01); leg->SetTextFont(42); leg->SetFillStyle(-1); //leg->SetHeader(ds_name+" (m_{H}="+MASS+")"); leg->SetHeader(TString::Format("Category %d",atoi(ds_name(3,1).Data())+1)); leg->AddEntry(hBlind,"Data","P"); if (!BLIND) { leg->AddEntry(gSigFit,"Fitted signal","L"); } TLine *gEmpty = new TLine(0.0,0.0,0.0,0.0); gEmpty->SetLineWidth(0); TLegendEntry *l1 = leg->AddEntry(gEmpty,"(m_{H} = "+MASS+" GeV)",""); l1->SetTextSize(0.038*0.97*0.85); leg->AddEntry(gFit,"Bkg. + signal","L"); leg->AddEntry(gBkgFit,"Bkg.","L"); leg->AddEntry(gQCDFit,"QCD","L"); leg->AddEntry(hUnc2H,"2#sigma bkg. unc.","F"); leg->AddEntry(hUncH,"1#sigma bkg. unc.","F"); leg->SetFillColor(0); leg->SetBorderSize(0); leg->SetTextFont(42); leg->SetTextSize(0.038*0.98); leg->Draw(); leg->SetY1(leg->GetY2()-leg->GetNRows()*0.045*0.96); TPaveText *paveCMS = new TPaveText(gStyle->GetPadLeftMargin()+0.02,0.7,gStyle->GetPadLeftMargin()+0.15,1.-gStyle->GetPadTopMargin()-0.01,"NDC"); paveCMS->SetTextFont(62); paveCMS->SetTextSize(gStyle->GetPadTopMargin()*3./4.); paveCMS->SetBorderSize(0); paveCMS->SetFillStyle(-1); paveCMS->SetTextAlign(12); paveCMS->AddText("CMS"); paveCMS->Draw(); gPad->Update(); paveCMS->SetY1NDC(paveCMS->GetY2NDC()-paveCMS->GetListOfLines()->GetSize()*gStyle->GetPadTopMargin()); TPaveText *paveLumi = new TPaveText(0.5,1.-gStyle->GetPadTopMargin(),0.98,1.00,"NDC"); paveLumi->SetTextFont(42); paveLumi->SetTextSize(gStyle->GetPadTopMargin()*3./4.); paveLumi->SetBorderSize(0); paveLumi->SetFillStyle(-1); paveLumi->SetTextAlign(32); paveLumi->AddText(TString::Format("%.1f fb^{-1} (8TeV)",(atoi(ds_name(3,1).Data())<4 ? 19.8 : 18.3)).Data());//+ 18.2 ; paveLumi->Draw(); TString path="."; //TString path="BiasV10_limit_BRN5p4_dX0p1_B80-200_CAT0-6/output/"; system(TString::Format("[ ! -d %s/plot ] && mkdir %s/plot",path.Data(),path.Data()).Data()); system(TString::Format("[ ! -d %s/plot/fits ] && mkdir %s/plot/fits",path.Data(),path.Data()).Data()); canFit->SaveAs(TString::Format("%s/plot/fits/Fit_mH%s_%s.pdf",path.Data(),MASS.Data(),ds_name.Data()).Data()); canFit->SaveAs(TString::Format("%s/plot/fits/Fit_mH%s_%s.png",path.Data(),MASS.Data(),ds_name.Data()).Data()); canFit->SaveAs(TString::Format("%s/plot/fits/Fit_mH%s_%s.eps",path.Data(),MASS.Data(),ds_name.Data()).Data()); TText *l = (TText*)paveCMS->AddText("Preliminary"); l->SetTextFont(52); paveCMS->Draw(); gPad->Update(); paveCMS->SetY1NDC(paveCMS->GetY2NDC()-paveCMS->GetListOfLines()->GetSize()*gStyle->GetPadTopMargin()); canFit->SaveAs(TString::Format("%s/plot/fits/Fit_mH%s_%s_prelim.pdf",path.Data(),MASS.Data(),ds_name.Data()).Data()); canFit->SaveAs(TString::Format("%s/plot/fits/Fit_mH%s_%s_prelim.png",path.Data(),MASS.Data(),ds_name.Data()).Data()); canFit->SaveAs(TString::Format("%s/plot/fits/Fit_mH%s_%s_prelim.eps",path.Data(),MASS.Data(),ds_name.Data()).Data()); delete ds; } cout << "chi2sumS: " << chi2sumS << endl; cout << "chi2sumB: " << chi2sumB << endl; cout << "nparS: " << nparS << endl; cout << "nparB: " << nparB << endl; cout << "nbinsum: " << nparsum << endl; cout << "chi2sumS/(nbinsum - nparS): " << chi2sumS / (float)(nparsum - nparS) << endl; cout << "chi2sumB/(nbinsum - nparB): " << chi2sumB / (float)(nparsum - nparB) << endl; delete datasets; }