void eliminationTree::analyzeTree(node *root){
  
  int currLevel = root->currLevel;
  int nodeLevelVecSize = nodeLevelVec.size();
  if (nodeLevelVecSize < currLevel + 1){
    std::vector<node*> currLevelNodeVec;
    nodeLevelVec.push_back(currLevelNodeVec);
  }
  nodeLevelVec[currLevel].push_back(root);

  if (currLevel > (numLevels - 1))
    numLevels = currLevel + 1;

  int numChildren = (root->children).size(); 
  if (numChildren == 0){
    root->isLeaf = true;
    return;
  }
  
  for (int i = 0; i < numChildren; i++){
    node* child = (root->children)[i];
    child->currLevel = currLevel + 1;
    analyzeTree(child);
  }
}
int inefficiency(int nEntAsk=-1, TString dir="/data_CMS/cms/ndaci/ndaci_2011A/Inefficiency/Both/", TString nameChain="Electrons", double cutet=15., bool debug=false)
{

  TChain* myChain = new TChain(nameChain);
  myChain->Add(dir+"/*.root");

  ofstream outlog("logs/log_inefficiency_XavEE_short.log",ios::out);
  int ant = analyzeTree( myChain , nEntAsk, cutet, debug, outlog);

  return ant;
}
void eliminationTree::build_eliminationTree(const int input_numCols,const int input_numBlocks,const std::vector<int> & rangVec,const std::vector<int> & treeVec){
  assert(input_numBlocks > 0);
  numBlocks = input_numBlocks;
  numCols = input_numCols;
  // Check inputs
  assert(treeVec[numBlocks - 1] == -1);
  assert(rangVec[numBlocks] == numCols);
  
  // Create elimination tree 
  numLevels = 1;
  if (numBlocks > 1){
    createTree(rangVec,treeVec);
    analyzeTree(root);
    levelCols.resize(numLevels);
    for (int i = 0; i < numLevels; i++)
      levelCols[i] = countColsAtLevel(i,root);
  }else{
    root = new node;
    root->isLeaf = true;
    root->currLevel = 0;
    root->numCols = numCols;
    root->min_Col = 0;
    root->max_Col = numCols - 1;
    levelCols.push_back(numCols);
    std::vector<node*> rootPtr;
    rootPtr.push_back(root);
    nodeLevelVec.push_back(rootPtr);
  }
  // Sanity check
  int sum = 0;
  for (int i = 0; i < numLevels; i++)
    sum += levelCols[i];
  assert(numCols == sum);
  int nodeLevelVecSize = nodeLevelVec.size();
  assert(nodeLevelVecSize == numLevels);
}
int track2cluster_plot()
{
  gROOT->SetBatch(kTRUE);
  // ------------------------------------------------------------------------//
  //  Uploading Files and Cluster Trees
  // ------------------------------------------------------------------------//
  
  
  TFile *f_1 = new TFile("/sphenix/user/gregtom3/data/Summer2018/track2cluster_studies/e1DIS.root");
  TTree *t_truth_1 = (TTree*)f_1->Get("event_truth");
  TTree *t_cluster_1 = (TTree*)f_1->Get("event_cluster");

  TFile *f_2 = new TFile("/sphenix/user/gregtom3/data/Summer2018/track2cluster_studies/e2DIS.root");
  TTree *t_truth_2 = (TTree*)f_2->Get("event_truth");
  TTree *t_cluster_2 = (TTree*)f_2->Get("event_cluster");

  TFile *f_5 = new TFile("/sphenix/user/gregtom3/data/Summer2018/track2cluster_studies/e5DIS.root");
  TTree *t_truth_5 = (TTree*)f_5->Get("event_truth");
  TTree *t_cluster_5 = (TTree*)f_5->Get("event_cluster");

  TFile *f_10 = new TFile("/sphenix/user/gregtom3/data/Summer2018/track2cluster_studies/e10DIS.root");
  TTree *t_truth_10 = (TTree*)f_10->Get("event_truth");
  TTree *t_cluster_10 = (TTree*)f_10->Get("event_cluster");

  /*TFile *f_20 = new TFile("/sphenix/user/gregtom3/data/Summer2018/track2cluster_studies/e20DIS.root");
  TTree *t_truth_20 = (TTree*)f_20->Get("event_truth");
  TTree *t_cluster_20 = (TTree*)f_20->Get("event_cluster");*/

  // ------------------------------------------------------------------------//
  //  Creating Histograms (Phi, Eta Theta, momentum
  //                      posx, posy, posz, spatial distance,
  // ------------------------------------------------------------------------//
  const int nbins = 200;
  TH2F *h2_phi_base = new TH2F("","",nbins,-3.5,3.5,nbins,-3.5,3.5);
  TH2F *h2_eta_base = new TH2F("","",nbins,-10,10,nbins,-10,10);
  TH2F *h2_theta_base = new TH2F("","",nbins,-3.5,3.5,nbins,-3.5,3.5);
  TH2F *h2_mom_base = new TH2F("","",nbins,0,35,nbins,0,35);
  TH2F *h2_posx_base = new TH2F("","",nbins,-320,320,nbins,-320,320);
  TH2F *h2_posy_base = new TH2F("","",nbins,-320,320,nbins,-320,320);
  TH2F *h2_posz_base = new TH2F("","",nbins,-320,320,nbins,-320,320);
  TH1F *h1_spatial_base = new TH1F("","",nbins,0,30);
  // ------------------------------------------------------------------------//
  //  Creating Histogram Clones
  // ------------------------------------------------------------------------//
  TH2F *h2_phi = (TH2F*)h2_phi_base->Clone();
  TH2F *h2_eta = (TH2F*)h2_eta_base->Clone();
  TH2F *h2_theta = (TH2F*)h2_theta_base->Clone();
  TH2F *h2_mom = (TH2F*)h2_mom_base->Clone();
  TH2F *h2_posx = (TH2F*)h2_posx_base->Clone();
  TH2F *h2_posy = (TH2F*)h2_posy_base->Clone();
  TH2F *h2_posz = (TH2F*)h2_posz_base->Clone();
  TH1F *h1_spatial_cemc = (TH1F*)h1_spatial_base->Clone();
  TH1F *h1_spatial_eemc = (TH1F*)h1_spatial_base->Clone();
  TH1F *h1_spatial_femc = (TH1F*)h1_spatial_base->Clone();
  h1_spatial_cemc->SetLineColor(kRed);
  h1_spatial_eemc->SetLineColor(kBlue);
  h1_spatial_femc->SetLineColor(kGreen);
  // ------------------------------------------------------------------------//
  //  Which momentums would you like to iterate through?
  // ------------------------------------------------------------------------//
  std::vector<int> iter_p;
  iter_p.push_back(1);
  iter_p.push_back(2);
  iter_p.push_back(5);   // Only these momenta are currently available
  iter_p.push_back(10);
  iter_p.push_back(20);    
  // ------------------------------------------------------------------------//
  //  How many events per tree would you like to plot? nEvents < 0 = all evts
  // ------------------------------------------------------------------------//
  const int nEvents = 100000;
  // ------------------------------------------------------------------------//
  //  Analyze each tree
  // ------------------------------------------------------------------------//
  analyzeTree(h2_phi,h2_eta,h2_theta,h2_mom,h2_posx,h2_posy,h2_posz,h1_spatial_cemc,h1_spatial_eemc,h1_spatial_femc,t_truth_1,t_cluster_1,nEvents);
  printHists(h2_phi,h2_eta,h2_theta,h2_mom,h2_posx,h2_posy,h2_posz,h1_spatial_cemc,h1_spatial_eemc,h1_spatial_femc,"1GeV_electron.png");

  analyzeTree(h2_phi,h2_eta,h2_theta,h2_mom,h2_posx,h2_posy,h2_posz,h1_spatial_cemc,h1_spatial_eemc,h1_spatial_femc,t_truth_2,t_cluster_2,nEvents);
  printHists(h2_phi,h2_eta,h2_theta,h2_mom,h2_posx,h2_posy,h2_posz,h1_spatial_cemc,h1_spatial_eemc,h1_spatial_femc,"2GeV_electron.png");

  analyzeTree(h2_phi,h2_eta,h2_theta,h2_mom,h2_posx,h2_posy,h2_posz,h1_spatial_cemc,h1_spatial_eemc,h1_spatial_femc,t_truth_5,t_cluster_5,nEvents);
  printHists(h2_phi,h2_eta,h2_theta,h2_mom,h2_posx,h2_posy,h2_posz,h1_spatial_cemc,h1_spatial_eemc,h1_spatial_femc,"5GeV_electron.png");

  analyzeTree(h2_phi,h2_eta,h2_theta,h2_mom,h2_posx,h2_posy,h2_posz,h1_spatial_cemc,h1_spatial_eemc,h1_spatial_femc,t_truth_10,t_cluster_10,nEvents);
  printHists(h2_phi,h2_eta,h2_theta,h2_mom,h2_posx,h2_posy,h2_posz,h1_spatial_cemc,h1_spatial_eemc,h1_spatial_femc,"10GeV_electron.png");
  return 0;
}