|
1 | | -void plot_clock(TString out_dir, TString suffix) { |
| 1 | +void plot_clock(TString out_dir, TString suffix, Int_t do_fit) { |
| 2 | + |
2 | 3 | TString basename = out_dir + "/clock_" + suffix; |
3 | | - TFile* o = new TFile(basename+".root", "RECREATE"); |
| 4 | + TFile* out_file = new TFile(basename+".root", "RECREATE"); |
| 5 | + |
4 | 6 | TTree* tr = new TTree("tr", "tr"); |
5 | 7 | tr->ReadFile(basename+".dat"); |
6 | | - tr->Write(); |
7 | | - o->Close(); |
| 8 | + Long64_t clock_gated, clock_ungated, timestamp; |
| 9 | + tr->SetBranchAddress("clock_gated", &clock_gated); |
| 10 | + tr->SetBranchAddress("clock_ungated", &clock_ungated); |
| 11 | + tr->SetBranchAddress("timestamp", ×tamp); |
| 12 | + |
| 13 | + Long64_t timestamp_max = 0; |
| 14 | + Long64_t timestamp_min = 1000e9; |
| 15 | + |
| 16 | + TGraph* gr_g = new TGraph(); |
| 17 | + TGraph* gr_u = new TGraph(); |
| 18 | + gr_g->SetName("clock_gated"); |
| 19 | + gr_u->SetName("clock_ungated"); |
| 20 | + gr_g->SetTitle("gated clock vs. timestamp"); |
| 21 | + gr_u->SetTitle("ungated clock vs. timestamp"); |
| 22 | + gr_g->SetMarkerStyle(kFullCircle); |
| 23 | + gr_u->SetMarkerStyle(kFullCircle); |
| 24 | + gr_g->SetMarkerColor(kRed); |
| 25 | + gr_u->SetMarkerColor(kMagenta); |
| 26 | + |
| 27 | + for(Long64_t e = 0; e < tr->GetEntries(); e++) { |
| 28 | + tr->GetEntry(e); |
| 29 | + gr_g->AddPoint(timestamp, clock_gated); |
| 30 | + gr_u->AddPoint(timestamp, clock_ungated); |
| 31 | + timestamp_max = std::max(timestamp, timestamp_max); |
| 32 | + timestamp_min = std::min(timestamp, timestamp_min); |
| 33 | + } |
| 34 | + |
| 35 | + if(do_fit == 1) { |
| 36 | + gr_g->Fit("pol1", "", "", timestamp_min, timestamp_max); |
| 37 | + gr_u->Fit("pol1", "", "", timestamp_min, timestamp_max); |
| 38 | + |
| 39 | + auto fun_g = gr_g->GetFunction("pol1"); |
| 40 | + auto fun_u = gr_u->GetFunction("pol1"); |
| 41 | + auto slp_g = fun_g->GetParameter(1); |
| 42 | + auto slp_u = fun_u->GetParameter(1); |
| 43 | + |
| 44 | + fun_g->SetLineColor(kBlack); |
| 45 | + fun_u->SetLineColor(kBlack); |
| 46 | + |
| 47 | + TCanvas* canv = new TCanvas("canv", "canv", 800, 2*600); |
| 48 | + canv->Divide(1,2); |
| 49 | + canv->GetPad(1)->SetGrid(1,1); |
| 50 | + canv->GetPad(2)->SetGrid(1,1); |
| 51 | + canv->cd(1); |
| 52 | + gr_g->Draw("APE"); |
| 53 | + fun_g->Draw("SAME"); |
| 54 | + canv->cd(2); |
| 55 | + gr_u->Draw("APE"); |
| 56 | + fun_u->Draw("SAME"); |
| 57 | + canv->Write("canv"); |
| 58 | + |
| 59 | + auto slp2freq = [] (auto slp) { |
| 60 | + auto freq_hz = slp / 4e-9; // convert denominator of `slp` from `timestamp` to `duration [s]` |
| 61 | + return freq_hz / 1e6; // convert `Hz` -> `MHz` |
| 62 | + }; |
| 63 | + auto freq_g = slp2freq(slp_g); |
| 64 | + auto freq_u = slp2freq(slp_u); |
| 65 | + |
| 66 | + std::cout << "[clock_freq_result]: " << suffix << " " << freq_g << " " << freq_u << "\n"; |
| 67 | + |
| 68 | + std::cout << "\n"; |
| 69 | + std::cout << "===========================\n"; |
| 70 | + std::cout << "estimated clock frequencies\n"; |
| 71 | + std::cout << "===========================\n"; |
| 72 | + std::cout << " gated: " << freq_g << " MHz\n"; |
| 73 | + std::cout << " ungated: " << freq_u << " MHz\n"; |
| 74 | + std::cout << "===========================\n"; |
| 75 | + std::cout << "\n"; |
| 76 | + } |
| 77 | + |
| 78 | + gr_g->Write("gr_g"); |
| 79 | + gr_u->Write("gr_u"); |
| 80 | + tr->Write("tr"); |
| 81 | + |
| 82 | + out_file->Close(); |
8 | 83 | std::cout << "WROTE " << basename << ".root\n"; |
9 | | - std::cout << "Recommended plot commands:" << std::endl; |
10 | | - std::cout << " tr->Draw(\"clock_ungated:timestamp\")" << std::endl; |
11 | | - std::cout << " tr->Draw(\"clock_gated:timestamp\")" << std::endl; |
12 | 84 | } |
0 commit comments