#include "TFile.h"
#include "TTree.h"
#include "TCanvas.h"
#include "TH1D.h"
#include "TLegend.h"
#include "TString.h"
#include "TStyle.h"
#include <iostream>

void analyze(const char* filename = "SimAnasen1.root")
{
    gStyle->SetOptStat(1);

    TFile *f = TFile::Open(filename);
    if (!f || f->IsZombie()) {
        std::cerr << "ERROR: cannot open file " << filename << std::endl;
        return;
    }

    TTree *tree = (TTree*)f->Get("tree");
    TTree *tree2 = (TTree*)f->Get("tree2");

    if (!tree) {
        std::cerr << "ERROR: tree not found\n";
        return;
    }

    std::cout << "\n===== BASIC TREE INFO =====\n";
    tree->Print();

    // Event inspection tools
    std::cout << "\n===== FIRST 10 EVENTS =====\n";
    tree->Scan("Tb:TB:anodeID[0]:cathodeID[0]:sx3ID", "", "", 10);

    std::cout << "\n===== SINGLE EVENT EXAMPLE (0) =====\n";
    tree->Show(0);

    // Quick detector gating examples
    std::cout << "\n===== GATED STATS =====\n";
    std::cout << "Events with anodeID[0]==5: "
              << tree->GetEntries("anodeID[0]==5") << std::endl;

    std::cout << "Events with sx3ID>=0: "
              << tree->GetEntries("sx3ID>=0") << std::endl;

    // Tb vs TB comparison histogram

    TCanvas *c1 = new TCanvas("c1","Tb vs TB",800,600);
    //set min and max from tree values
    double min = tree->GetMinimum("Tb");
    double max = tree->GetMaximum("TB");
    min = min - max*0.1;
    max = max * 1.1;
    TH1D *hTb = new TH1D("hTb","Tb and TB;Energy (MeV);Counts",200,min,max); //arguments are name, title (with axis labels), number of bins, x-min, x-max
    TH1D *hTB = new TH1D("hTB","",200,min,max);

    tree->Draw("Tb>>hTb","","goff");
    tree->Draw("TB>>hTB","","goff");

    hTb->SetLineColor(kRed);
    hTB->SetLineColor(kBlue);

    hTb->Draw("HIST");
    hTB->Draw("HIST SAME");

    TLegend *leg = new TLegend(0.65,0.75,0.88,0.88);
    leg->AddEntry(hTb,"Tb (light)","l");
    leg->AddEntry(hTB,"TB (heavy)","l");
    leg->Draw();

    c1->SaveAs("Tb_TB_compare.png");

    // 4. Detector-gated histogram

    TCanvas *c2 = new TCanvas("c2","Anode gated Tb",800,600);
    double min2 = tree->GetMinimum("Tb");
    double max2 = tree->GetMaximum("Tb");
    min2 = min2 - max2*0.1;
    max2 = max2 * 1.1;
    TH1D *hGate = new TH1D("hGate","Tb (anodeID[0]==5);Energy;Counts",200,min2,max2);

    tree->Draw("Tb>>hGate","anodeID[0]==5","goff");

    hGate->SetLineColor(kGreen+2);
    hGate->Draw("HIST");

    c2->SaveAs("Tb_anode5.png");

    // Tb vs TB correlation (with gate)

    TCanvas *c3 = new TCanvas("c3","dEb vs SX3z",800,600);

    tree->Draw("TB:Tb>>h2(200,min,max,200,min,max)","","COLZ"); //arguments are "y:x>>histogram(bins,xmin,xmax,bins,ymin,ymax)", "selection", "options"

    c3->SaveAs("Tb_vs_TB.png");

    // Make gated trees 

    TFile *out = new TFile("gated_output.root","RECREATE");

    TTree *t_anode5 = tree->CopyTree("anodeID[0]==5");
    t_anode5->Write("tree_anode5");

    TTree *t_sx3valid = tree->CopyTree("sx3ID>=0");
    t_sx3valid->Write("tree_sx3valid");

    out->Close();

    std::cout << "\n===== DONE =====\n";
    std::cout << "Saved plots + gated trees in gated_output.root\n";
}