void histcomp() {
    gROOT->SetBatch(kTRUE);

    // Open file
    TFile *f = new TFile("SimAnasen1.root");

    // Get trees (MAKE SURE names are correct)
    TTree *tree1 = (TTree*)f->Get("tree");
    TTree *tree2 = (TTree*)f->Get("tree2");

    if (!tree1 || !tree2) {
    printf("Error: could not find trees. Check names!\n");
    return;
    }

    // Create output directory (overwrite-safe)
    gSystem->Exec("mkdir -p plots");

    // Get list of branches
    TObjArray *branches = tree1->GetListOfBranches();
    int nBranches = branches->GetEntries();
    //int nBranches = 1;

    // Loop over branches
    for (int i = 0; i < nBranches; i++) {
        TBranch *br = (TBranch*)branches->At(i);
        TString name = br->GetName();

        //printf("Processing branch: %s\n", name.Data());

        // Create histograms (auto-range using Draw first)
        TString h1name = "h1_" + name;
        TString h2name = "h2_" + name;

        // Temporary draw to get range
        double min, max;

        if(name == "T"){
            //Get minimum value of T[0] and use as min  
            min = tree2->GetMinimum("Tb");
            max = tree1->GetMaximum("TB");
        }else{

            tree1->Draw(name, "", "goff");

            min = fmin(tree1->GetMinimum(name),
                    tree2->GetMinimum(name));

            max = fmax(tree1->GetMaximum(name),
                    tree2->GetMaximum(name));
        }

        //if (min == max) continue; // skip constant branches

        // Expand range slightly
        double margin = 0.1 * (max - min);
        min -= margin;
        max += margin;

        TH1D *h1 = new TH1D(h1name, name, 100, min, max);
        TH1D *h2 = new TH1D(h2name, name, 100, min, max);

        // Fill histograms
        if(name == "T"){

            // Fill both array elements into same histogram
            tree1->Draw("Tb>>+" + h1name, "", "goff");
            tree1->Draw("TB>>+" + h1name, "", "goff");

            tree2->Draw("Tb>>+" + h2name, "", "goff");
            tree2->Draw("TB>>+" + h2name, "", "goff");

        }else{

            tree1->Draw(name + ">>" + h1name, "", "goff");
            tree2->Draw(name + ">>" + h2name, "", "goff");

        }

        // Style
        h1->SetLineColor(kRed);
        h1->SetLineWidth(2);

        h2->SetLineColor(kBlue);
        h2->SetLineWidth(2);

        // Normalize (optional but useful)
        //if (h1->GetEntries() > 0) h1->Scale(1.0 / h1->GetEntries());
        //if (h2->GetEntries() > 0) h2->Scale(1.0 / h2->GetEntries());

        // Canvas
        TCanvas *c = new TCanvas("c", name, 900, 600); //arguments are (name, title, width, height)

        c->SetRightMargin(0.18);
        c->Modified(); 
        c->Update();

        h1->SetTitle(name + ";"+name+";Counts");
        h1->Draw("HIST");
        h2->Draw("HIST SAME");

        gPad->Update();

        TPaveStats *st = (TPaveStats*)h1->FindObject("stats");

        st->SetX1NDC(0.85); // New X start (left)
        st->SetY1NDC(0.5); // New Y start (bottom)
        st->SetX2NDC(0.98); // New X end (right)
        st->SetY2NDC(0.8); // New Y end (top)
        st->Draw();
        gPad->Modified();
        gPad->Update();


        // Legend
        TLegend *leg = new TLegend(0.65 + .2,0.75 + .1,0.88 + .1,0.88 + .1);
        leg->AddEntry(h1, "tree1", "l");
        leg->AddEntry(h2, "tree2", "l");
        leg->Draw();
        //to plot both as one histogram in root, can use tree2->Draw("T(0)"); for light particle and tree2->Draw("T(1)") for heavy particle
        // Save plot (overwrite each run)
        TString filename = "plots/" + name + ".png";
        c->SaveAs(filename);
        // Optional: save log plots as well

        if (false) { // set to True to also save log plots
            c->SetLogy(1);
            h1->SetTitle(name + " (log);"+name+";Counts");
            c->SaveAs("plots/" + name + "_logy.png");

            c->SetLogy(0);
            c->SetLogx(1);
            h1->SetTitle(name + " (log);"+name+";Counts");
            c->SaveAs("plots/" + name + "_logx.png");

            // Clean up
            delete c;
            delete h1;
            delete h2;
        }



    }

    // dEb on y, SX3z on x
    TH2D *h2d = new TH2D("h2d", "dEb vs SX3z;SX3z (cm);dEb (MeV)", 500, tree2->GetMinimum("sx3Z"), tree2->GetMaximum("sx3Z"), 500, tree2->GetMinimum("dEb"), tree2->GetMaximum("dEb")); //arguments are (name, title, xbins, xlow, xup, ybins, ylow, yup)
    tree2->Draw("dEb:sx3Z>>h2d", "", "goff"); // arguments are "y:x>>histogram", "selection", "options"
    TCanvas *c2d = new TCanvas("c2d", "dEb vs SX3z", 900, 600);
    h2d->Draw("COLZ");
    c2d->SaveAs("plots/dEb_vs_SX3z.png");

    TH2D *h2z = new TH2D("h2z", "dEb vs z0", 500, tree2->GetMinimum("z0"), tree2->GetMaximum("z0"), 500, tree2->GetMinimum("dEb"), tree2->GetMaximum("dEb"));
    tree2->Draw("dEb:z0>>h2z", "", "goff"); // arguments are "y:x>>histogram", "selection", "options"
    TCanvas *c2z = new TCanvas("c2z", "dEb vs z0", 900, 600);
    h2z->Draw("COLZ");
    c2z->SaveAs("plots/dEb_vs_z0.png");

    TH2D *h2theta = new TH2D("h2theta", "dEb vs reTheta", 500, tree2->GetMinimum("reTheta"), tree2->GetMaximum("reTheta"), 500, tree2->GetMinimum("dEb"), tree2->GetMaximum("dEb"));
    tree2->Draw("dEb:reTheta>>h2theta", "", "goff"); // arguments are "y:x>>histogram", "selection", "options"
    TCanvas *c2theta = new TCanvas("c2theta", "dEb vs reTheta", 900, 600);
    h2theta->Draw("COLZ");
    c2theta->SaveAs("plots/dEb_vs_reTheta.png");

    TH2D *h2phi = new TH2D("h2phi", "dEb vs rePhi", 500, tree2->GetMinimum("rePhi"), tree2->GetMaximum("rePhi"), 500, tree2->GetMinimum("dEb"), tree2->GetMaximum("dEb"));
    tree2->Draw("dEb:rePhi>>h2phi", "", "goff"); // arguments are "y:x>>histogram", "selection", "options"
    TCanvas *c2phi = new TCanvas("c2phi", "dEb vs rePhi", 900, 600);
    h2phi->Draw("COLZ");
    c2phi->SaveAs("plots/dEb_vs_rePhi.png");

    TH2D *h2dE = new TH2D("h2dE", "dEb vs Tb", 500, tree2->GetMinimum("Tb"), tree2->GetMaximum("Tb"), 500, tree2->GetMinimum("dEb"), tree2->GetMaximum("dEb"));
    tree2->Draw("dEb:Tb>>h2dE", "", "goff"); // arguments are "y:x>>histogram", "selection", "options"
    TCanvas *c2dE = new TCanvas("c2dE", "dEb vs Tb", 900, 600);
    h2dE->Draw("COLZ");
    c2dE->SaveAs("plots/dEb_vs_Tb.png");

    printf("Done! Plots saved in ./plots/\n");
}
