#include "TTree.h"
#include "TFile.h"
#include <iostream>
#include <fstream>
#include <bitset>

using namespace std;

struct myTree_t {
  int counterPosi;
  int counterElec;
  double eElec;
  double ePosi;
};
 
int main(int argc, char **argv){


  //Setup the input tree
  TFile* inFile=new TFile("psDet.root");
  TTree *inTree = (TTree*)inFile->Get("outTree");
  myTree_t myInputTree;
  inTree->SetBranchAddress("counterPosi",&myInputTree.counterPosi);
  inTree->SetBranchAddress("counterElec",&myInputTree.counterElec);
  inTree->SetBranchAddress("ePosi",&myInputTree.ePosi);
  inTree->SetBranchAddress("eElec",&myInputTree.eElec);

  //Number of training sets in the tree
  int nEvents = inTree->GetEntries();

  ofstream myfile;
  myfile.open ("tmp.csv");

  //Loop over all of the training sets
  for(Int_t i=0;i<nEvents;i++){
    if (i%1000 == 0) cout <<"Event = "<<i<<endl; //Print out every 1000 events
    inTree->GetEntry(i);

    //myfile<<myInputTree.counterPosi<<","<<myInputTree.ePosi*1000;
    myfile<<myInputTree.counterPosi;
    int tmp = myInputTree.ePosi*1000;
    for (int iBit = 0; iBit<24; iBit++){
      int tmpShift = (tmp&(int)pow(2,23-iBit))>>(23-iBit);
      myfile<<","<<std::bitset<1>(tmpShift);
    };
    myfile<<endl;

  }
  myfile.close();
  return 0;
}

