#include #include #include #include #include #include #include #include #include using namespace std; #include "tdiff_tgraph.cc" #include "IsOuter.h" TGraphErrors* GetPoints(TH2D *h); //------------------------------------ // Copied from JEventProcessor_bcal_timing.cc typedef struct{ int event; int layer; int sector; int fADC_up; int fADC_dn; float Etot; float geometric_mean; float tup; float tdn; float tup_corrected; float tdn_corrected; }HIT_t; //------------------------------------ //----------------- // tdiff_calibrate_tgraph //----------------- void tdiff_check_tgraph(void) { TFile *f = new TFile("hd_root.root"); f->cd(); TTree *tree = (TTree*)gROOT->FindObject("tree"); HIT_t hit; HIT_t *hitptr = &hit; TBranch *branch = tree->GetBranch("T"); branch->SetAddress(hitptr); // Make histograms to hold tdiff vs. geometric mean // for each layer. TH2D *tdiff_vs_geomn[10+1]; for(int ilayer=1; ilayer<=10 ; ilayer++){ char hname[256]; sprintf(hname, "tdiff_vs_geomn_lay%02d",ilayer); tdiff_vs_geomn[ilayer] = new TH2D(hname, "", 2000, 0.0, 4000.0, 100, -5.0, 5.0); } vector tdiff_correcteds[10+1]; vector geomns[10+1]; int Nentries = branch->GetEntries(); for(int i=1; i<=Nentries; i++){ tree->GetEntry(i); if(hit.layer<1 || hit.layer>10)continue; // bulletproof if(hit.tup_corrected<-100)continue; if(hit.tdn_corrected<-100)continue; float tdiff = hit.tup_corrected - hit.tdn_corrected; float geomn = hit.geometric_mean; float tdiff_corrected = tdiff - BCAL_tdiff(geomn, hit.layer); geomns[hit.layer].push_back(geomn); tdiff_correcteds[hit.layer].push_back(tdiff_corrected); tdiff_vs_geomn[hit.layer]->Fill(geomn, tdiff_corrected); } TCanvas *c1 = new TCanvas("c1"); c1->SetTicks(); c1->SetGrid(); TH2D *axes = new TH2D("axes","", 100, 0.0, 3500.0, 100, -15.0, 15.0); axes->SetStats(0); axes->SetXTitle("fADC"); axes->SetYTitle("t (ns)"); // Open PDF file for plots c1->Print("tdiff_check_tgraph.ps["); // Loop over layers int Nlayers=0; TF1 *sigma_vs_geomn[10+1]; for(int ilayer=1; ilayer<=10 ; ilayer++, Nlayers++){ TGraph *g = new TGraph(geomns[ilayer].size(), &geomns[ilayer][0], &tdiff_correcteds[ilayer][0]); char title[256]; sprintf(title, "#Deltat for layer %d", ilayer); axes->SetTitle(title); TGraphErrors *gres = GetPoints(tdiff_vs_geomn[ilayer]); axes->Draw(); g->Draw("Psame"); gres->Draw("same"); cout<<"Number of points:"<GetN()<Update(); c1->Print("tdiff_check_tgraph.ps"); char fname[256]; sprintf(fname, "tdiff_check_tgraph_layer%02d.png", ilayer); c1->SaveAs(fname); TF1 *fun = gres->GetFunction("fun"); if(fun){ char funname[256]; sprintf(funname, "sigma_vs_geomn_lay%02d", ilayer); sigma_vs_geomn[ilayer] = (TF1*)fun->Clone(funname); }else{ sigma_vs_geomn[ilayer] = NULL; } } // Close PDF file c1->Print("tdiff_check_tgraph.ps]"); cout<<"Plots written to \"tdiff_check_tgraph.ps\". Convert to PDF with:"<GetExpFormula("p")).Data(); while(true){ size_t pos=expr.find("(x^"); if(pos!=string::npos){ expr.replace(pos, 3, "pow(x,"); }else{ break; } } // Set resolution to 100ns for outer detectors, // effictively disabling them if(IsOuter(ilayer))expr = "100.0"; pout<<"\t\t\t"<<"return "<ProjectionX("_px", 1); // Loop over (uneven) bins int Npoints = 0; vector x; vector y; vector yerr; int Nbins = h->GetNbinsX(); for(int start_bin=1; start_bin<=Nbins; ){ // Find limits to give us min_entries entries int end_bin = start_bin; while(h_px->Integral(start_bin, end_bin)Integral(end_bin, Nbins)Integral()>=20){ // Fit to Gaussian h_py->Fit("gaus","0"); //c1->Update(); // Get fit results double sigma = h_py->GetFunction("gaus")->GetParameter(2); double sigma_err = h_py->GetFunction("gaus")->GetParError(2); // Find average fADC value for hits in this bin range double fADC = 0.0; double norm = 0.0; for(int bin=start_bin; bin<=end_bin; bin++){ double weight = h_px->GetBinContent(bin); fADC += h_px->GetBinCenter(bin)*weight; norm += weight; } fADC /= norm; // Add floor term to error obtained from Gaussian fit double epsilon = 0.050; // ns sigma_err = sqrt(sigma_err*sigma_err + epsilon*epsilon); x.push_back(fADC); y.push_back(sigma); yerr.push_back(sigma_err); Npoints++; } // Increment for next iteration start_bin=end_bin+1; } TGraphErrors *g = new TGraphErrors(Npoints, &x[0], &y[0], 0, &yerr[0]); g->SetMarkerColor(kMagenta); g->SetLineColor(kMagenta); // Fit the graph. If there are less than 8 points, just fit to // a flat line. TF1 *fun; if(Npoints<8){ fun = new TF1("fun", "[0]", 0.0, 800.0); }else{ fun = new TF1("fun", "[0] + [1]/(pow(x,[2])+[3])", 0.0, 800.0); fun->SetParameter(0, 0.100); fun->SetParameter(1, 1000.0); fun->SetParameter(2, 1.0); fun->SetParameter(3, -3.0); //fun->SetParLimits(0, 0.0, 3.0); } fun->SetLineColor(kRed); g->Fit(fun); return g; }