modified: scratch/scan_rf_timing.C output images instead of gif

This commit is contained in:
Vignesh Sitaraman 2026-06-05 15:41:32 -04:00
parent d8e4b22614
commit 7891aaebca

View File

@ -1,29 +1,22 @@
#include "TFile.h" #include "TFile.h"
#include "TH1.h" #include "TH1.h"
#include "TCanvas.h" #include "TCanvas.h"
#include "TLine.h"
#include "TSystem.h" #include "TSystem.h"
#include "TROOT.h" #include "TROOT.h"
#include <iostream> #include <iostream>
#include <vector> #include <vector>
void scan_rf_timing(int startRun = 350, int endRun = 370) { void scan_rf_timing(int startRun = 350, int endRun = 370) {
TCanvas *c = new TCanvas("c1", "RF-MCP Timing Animation", 0, 0, 1600, 800); TCanvas *c = new TCanvas("c1", "RF-MCP Timing Images", 0, 0, 1600, 800);
c->Divide(2, 1); c->Divide(2, 1);
// Array of distinct colors to cycle through
int colors[] = {kBlack, kRed, kBlue, kGreen+2, kMagenta, kCyan+1, int colors[] = {kBlack, kRed, kBlue, kGreen+2, kMagenta, kCyan+1,
kOrange+7, kSpring+4, kViolet+2, kAzure+2, kTeal-1, kPink+2}; kOrange+7, kSpring+4, kViolet+2, kAzure+2, kTeal-1, kPink+2};
int nColors = 12; int nColors = 12;
std::vector<TFile*> files; std::vector<TFile*> files;
// Define the output file name
const char* gifFile = "rf_timing_animation.gif";
// Safety check: ROOT appends to GIFs. We must delete any old version
// before starting, otherwise it will just add new frames to the old file!
gSystem->Unlink(gifFile);
int colorIdx = 0; int colorIdx = 0;
for (int i = startRun; i <= endRun; i++) { for (int i = startRun; i <= endRun; i++) {
@ -39,51 +32,56 @@ void scan_rf_timing(int startRun = 350, int endRun = 370) {
int color = colors[colorIdx % nColors]; int color = colors[colorIdx % nColors];
colorIdx++; colorIdx++;
// --- Pad 1: Inner Ring 1 --- // --- Pad 1: Inner Ring ---
c->cd(1); c->cd(1);
c->GetPad(1)->SetGrid(1, 1); c->GetPad(1)->SetGrid(1, 1);
TH1F *h1 = (TH1F*)(f->Get("misc/dt_rf_mcp_qqq_innerring1")); TH1F *h1 = (TH1F*)(f->Get("misc/dt_rf_mcp_qqq_innerring1"));
if (h1) { if (h1) {
h1->SetDirectory(0); h1->SetDirectory(0);
h1->Rebin(2); h1->Rebin(2);
h1->SetTitle(Form("Run %d: Inner Ring 1 Timing", i)); h1->SetTitle(Form("Run %d: Inner Ring Timing", i));
h1->SetLineColor(color); h1->SetLineColor(color);
h1->SetLineWidth(3); h1->SetLineWidth(3);
h1->Draw("hist"); h1->Draw("hist");
c->Update();
TLine *line1 = new TLine(105.189, gPad->GetUymin(), 105.189, gPad->GetUymax());
line1->SetLineColor(kRed);
line1->SetLineStyle(2); // Dashed
line1->SetLineWidth(2);
line1->Draw();
} }
// --- Pad 2: Inner Ring 0 --- // --- Pad 2: Outer Ring ---
c->cd(2); c->cd(2);
c->GetPad(2)->SetGrid(1, 1); c->GetPad(2)->SetGrid(1, 1);
TH1F *h0 = (TH1F*)(f->Get("misc/dt_rf_mcp_qqq_innerring0")); TH1F *h0 = (TH1F*)(f->Get("misc/dt_rf_mcp_qqq_innerring0"));
if (h0) { if (h0) {
h0->SetDirectory(0); h0->SetDirectory(0);
h0->Rebin(2); h0->Rebin(2);
h0->SetTitle(Form("Run %d: Inner Ring 0 Timing", i)); h0->SetTitle(Form("Run %d: Outer Ring Timing", i));
h0->SetLineColor(color); h0->SetLineColor(color);
h0->SetLineWidth(3); h0->SetLineWidth(3);
h0->Draw("hist"); h0->Draw("hist");
c->Update();
TLine *line0 = new TLine(105.189, gPad->GetUymin(), 105.189, gPad->GetUymax());
line0->SetLineColor(kRed);
line0->SetLineStyle(2); // Dashed
line0->SetLineWidth(2);
line0->Draw();
} }
// Update the canvas to reflect the new drawn histograms
c->cd(1); c->Modified(); c->Update(); c->cd(1); c->Modified(); c->Update();
c->cd(2); c->Modified(); c->Update(); c->cd(2); c->Modified(); c->Update();
std::cout << "Adding Run " << i << " to GIF..." << std::endl; TString outName = Form("rf_timing_run%03d.png", i);
c->SaveAs(outName);
// --- ADD FRAME TO GIF --- std::cout << "Saved: " << outName << std::endl;
// The "+50" tells ROOT to append this frame and wait 50 centiseconds (0.5 seconds)
c->Print(Form("%s+50", gifFile));
} }
// --- SEAL THE GIF --- std::cout << "\n=== IMAGE GENERATION COMPLETE ===" << std::endl;
// The "++" tells ROOT we are done adding frames and it should finalize the file
c->Print(Form("%s++", gifFile));
std::cout << "\n=== GIF GENERATION COMPLETE ===" << std::endl;
std::cout << "Saved as: " << gifFile << std::endl;
// Safely clean up memory
for (auto file : files) { for (auto file : files) {
if (file) file->Close(); if (file) file->Close();
} }