#include "TFile.h"
#include "TTree.h"
#include "TGraph.h"
#include "TLegend.h"
#include "TCanvas.h"
#include "TH1D.h"
#include "TObjArray.h"
#include "TBranch.h"
#include <iostream>
#include <fstream>

void aarootscript(int argument = 0) {
    std::cout << "\n\n\n";
    std::cout << "=========================================\n";
    std::cout << "=========   ANASEN Root Script  =========\n";
    std::cout << "=========================================\n";

    TFile *f = new TFile("SimAnasen1.root");
    TTree *tree1 = (TTree*)f->Get("tree");
    if (!tree1) {
        std::cerr << "Error: tree1 not found in the file!" << std::endl;
        return;
    }
    TTree *tree2 = (TTree*)f->Get("tree2");

    TTreeReader reader("tree");
    TTreeReaderValue<double> Tb1(reader, "Tb"); // this line will read the "Tb" branch from the tree and save it as 
    TTreeReaderValue<double> TB1(reader, "TB"); // this line will read the "TB" branch from the tree and save it as Tb1
    //add Tb1 and TB1 together 

    double totalSum = 0;
    while (reader.Next()) {
        totalSum += *Tb1;
    }
    std::cout << "Total Sum: " << totalSum << std::endl;

    TTreeReader reader2("tree2");
    TTreeReaderValue<double> Tb2(reader2, "Tb");
    double totalSum2 = 0;
    int zeroCount = 0;
    while (reader2.Next()) {
        totalSum2 += *Tb2;
        if (*Tb2 == 0) {
            zeroCount++;
        }
    }
    std::cout << "Total Sum: " << totalSum2 << std::endl;

    std::cout << "Difference: " << totalSum - totalSum2 << std::endl;

    std::cout << "Zero Count: " << zeroCount << std::endl;



    //std::cout << "Making histograms..." << std::endl;

    gErrorIgnoreLevel = 2001;
    gROOT->ProcessLine(".x histcomp.C"); 

    std::cout << "=========================================\n";

    if (argument == 1) {
        if (tree1) {
            gROOT->ProcessLine("tree->Print();");
        } else {
            std::cout << "Tree1 not found!" << std::endl;
        }
    }

    if (argument == 2) {
        if (tree2) {
            gROOT->ProcessLine("tree2->Print();");
        } else {
            std::cout << "Tree2 not found!" << std::endl;
        }
    }

    std::cout << "Creating Tb vs dEb plot..." << std::endl;

    // Readers for both trees
    TTreeReader r1(tree1);
    TTreeReader r2(tree2);

    TTreeReaderValue<double> Tb_val(r1, "Tb");
    TTreeReaderValue<double> TB_val(r2, "TB");
    TTreeReaderValue<double> dEb_val(r2, "dEb");

    std::vector<double> x; // Tb (tree1)
    std::vector<double> y; // dEb (tree2)


    // Loop over both trees simultaneously
    while (r1.Next() && r2.Next()) {
        x.push_back(*Tb_val);
        y.push_back(*dEb_val);
    }
    std::cout << "x length: " << x.size() << ", y length: " << y.size() << std::endl;

    std::ofstream outfile("Tb_dEb_data.txt");

    if (!outfile.is_open()) {
        std::cerr << "Error: Could not open output file!" << std::endl;
        return;
    }

    for (size_t i = 0; i < x.size(); i++) {
        outfile << x[i] << " " << y[i] << "\n";
    }

    outfile.close();

    std::cout << "Data written to Tb_dEb_data.txt" << std::endl;
    /*
    // Create graph
    TGraph *gr = new TGraph(x.size(), &x[0], &y[0]);
    gr->SetTitle("Tb (tree1) vs dEb (tree2);Tb;dEb");
    gr->SetMarkerStyle(20);

    // Draw
    TCanvas *c1 = new TCanvas("c1", "Tb vs dEb", 800, 600);
    gr->Draw("AP");
    c1->Update();
    c1->SaveAs("Tb_vs_dEb.png");
    std::cout << "Plot saved as Tb_vs_dEb.pdf" << std::endl;
    std::cout << "\n\n\n";

    delete c1;
    delete gr;*/




}

