#include <string.h>
#include <iostream>
#include <vector>
#include "TFile.h"
#include "TLorentzVector.h"
#include "TH1.h"
#include "TH2.h"
#include "TF1.h"
#include "TRandom3.h"
#include "TVector3.h"
#include "TTree.h"
#include <fstream>
#include "myTree.h"

using namespace std;
 
//MAIN
int main(int argc, char **argv) {
  char  hId[120];

  TFile *inSino=new TFile("./medDat_all.root"); //Get the file that has histo
  TH2D* hSino=(TH2D*)inSino->Get("hSino"); //Get the histogram 
  int nBinsS     = hSino->GetNbinsX();
  int nBinsTheta = hSino->GetNbinsY();


  TFile *outFile = new TFile("./myOutFile.root","RECREATE");
  //DEFINE OUTPUT TREE
  TTree outTree("outTree","outTree");
  
  //Define the tree structure
  myTree_t myTree;

  //DEFINE OUT BRANCHES
  setBranchesHitToGo(&outTree,&myTree);

  for (int iBinTheta = 0; iBinTheta < nBinsTheta; iBinTheta++) {
    for (int iBinS = 0; iBinS < nBinsS; iBinS++) {
      double intensity = hSino->GetBinContent(iBinS,iBinTheta);
      double sVal = hSino->GetXaxis()->GetBinCenter(iBinS);
      myTree.thetaBin = iBinTheta;
      myTree.sValue = sVal;
      myTree.sliceBin = 0;
      myTree.intensity = intensity;
      outTree.Fill();
    }
  }

  /////////////

  outFile->cd();
  outTree.Write();
  outFile->Close();
  return 0;
}

