#define myanalysis_cxx
#include "myanalysis.h"
#include <TH2.h>
#include <TStyle.h>
#include <TCanvas.h>
#include <TTree.h>
#include <TFrame.h>
#include <TH1F.h>
#include <TBenchmark.h>
#include <TRandom.h>
#include <TSystem.h>
#include <iostream>

void myanalysis::Loop()
{
//   In a ROOT session, you can do:
//      root> .L myanalysis.C
//      root> myanalysis t
//      root> t.GetEntry(12); // Fill t data members with entry number 12
//      root> t.Show();       // Show values of entry 12
//      root> t.Show(16);     // Read and show values of entry 16
//      root> t.Loop();       // Loop on all entries
//

//     This is the loop skeleton where:
//    jentry is the global entry number in the chain
//    ientry is the entry number in the current Tree
//  Note that the argument to GetEntry must be:
//    jentry for TChain::GetEntry
//    ientry for TTree::GetEntry and TBranch::GetEntry
//
//       To read only selected branches, Insert statements like:
// METHOD1:
//    fChain->SetBranchStatus("*",0);  // disable all branches
//    fChain->SetBranchStatus("branchname",1);  // activate branchname
// METHOD2: replace line
//    fChain->GetEntry(jentry);       //read all branches
//by  b_branchname->GetEntry(ientry); //read only this branch
   if (fChain == 0) return;

   Long64_t nentries = fChain->GetEntriesFast();
   int lasteventID = 0;
   int nPhotoelectrons = 0;
   bool sameEvent_bool = true;

   std::vector<int>   unique_detectorCopy_vec;
   std::vector<int>   hits_detectorCopy_vec;


   TFile * outfile = new TFile("myanalysis_out.root", "RECREATE");
   if ( outfile->IsOpen() ) printf("OUTPUT file opened successfully\n");
   // Create a TTree
   TTree *t = new TTree("tvec","Tree with vectors");
   t->Branch("runID_vec",&runID_vec);
   t->Branch("eventID_vec",&eventID_vec);
   t->Branch("detNumb_vec",&detNumb_vec);
   t->Branch("xPixel_vec",&xPixel_vec);
   t->Branch("yPixel_vec",&yPixel_vec);
   t->Branch("energy_vec",&energy_vec);
   t->Branch("wavelength_vec",&wavelength_vec);
   t->Branch("localtime_vec",&localtime_vec);
   t->Branch("abstime_vec",&abstime_vec);
   t->Branch("length_vec",&length_vec);
   t->Branch("x_vec",&x_vec);
   t->Branch("y_vec",&y_vec);
   t->Branch("z_vec",&z_vec);
   t->Branch("px_vec",&px_vec);
   t->Branch("py_vec",&py_vec);
   t->Branch("pz_vec",&pz_vec);
   t->Branch("vertexX_vec",&vertexX_vec);
   t->Branch("vertexY_vec",&vertexY_vec);
   t->Branch("vertexZ_vec",&vertexZ_vec);
   t->Branch("vertexPx_vec",&vertexPx_vec);
   t->Branch("vertexPy_vec",&vertexPy_vec);
   t->Branch("vertexPz_vec",&vertexPz_vec);
   t->Branch("gpsPosX_vec",&gpsPosX_vec);
   t->Branch("gpsPosY_vec",&gpsPosY_vec);
   t->Branch("gpsPosZ_vec",&gpsPosZ_vec);
   t->Branch("runId_vec",&runId_vec);
   t->Branch("eventId_vec",&eventId_vec);
   t->Branch("trackId_vec",&trackId_vec);
   t->Branch("creatorProcess_vec",&creatorProcess_vec);
   t->Branch("parentId_vec",&parentId_vec);
   t->Branch("lengthInCore_vec",&lengthInCore_vec);
   t->Branch("lengthInInnerCladding_vec",&lengthInInnerCladding_vec);
   t->Branch("lengthInOuterCladding_vec",&lengthInOuterCladding_vec);
   //  t->Branch("detectorCopy_vec",&detectorCopy_vec);
   // t->Branch("subdetectorCopy_vec",&subdetectorCopy_vec);

   
   Long64_t nbytes = 0, nb = 0;
   for (Long64_t jentry=0; jentry<nentries;jentry++) {
      Long64_t ientry = LoadTree(jentry);
      if (ientry < 0) break;
      nb = fChain->GetEntry(jentry);   nbytes += nb;
      // if (Cut(ientry) < 0) continue;

      

      if (ientry==0) {lasteventID = eventID;}
      
      if(eventID == lasteventID) {sameEvent_bool = true;}
      else { sameEvent_bool = false; }


      if (sameEvent_bool == false ){
	lasteventID = eventID;

	
	std::cout << xPixel_vec.size() << " " << uniqCnt << ": ";
	std::cout << std::endl;

	t->Fill();
	runID_vec.clear();		        
	eventID_vec.clear();		        
	detNumb_vec.clear();		        
	xPixel_vec.clear();		        
	yPixel_vec.clear();		        
	energy_vec.clear();		        
	wavelength_vec.clear();		        
	localtime_vec.clear();		        
	abstime_vec.clear();		        
	length_vec.clear();		        
	x_vec.clear();			        
	y_vec.clear();			        
	z_vec.clear();			        
	px_vec.clear();			        
	py_vec.clear();			        
	pz_vec.clear();			        
	vertexX_vec.clear();		        
	vertexY_vec.clear();		        
	vertexZ_vec.clear();		        
	vertexPx_vec.clear();		        
	vertexPy_vec.clear();		        
	vertexPz_vec.clear();		        
	gpsPosX_vec.clear();		        
	gpsPosY_vec.clear();		        
	gpsPosZ_vec.clear();		        
	gpsDirX_vec.clear();		        
	gpsDirY_vec.clear();		        
	gpsDirZ_vec.clear();		        
        runId_vec.clear();		        
        eventId_vec.clear();		        
        trackId_vec.clear();		        
        creatorProcess_vec.clear();	        
        parentId_vec.clear();		        
	lengthInCore_vec.clear();	        
	lengthInInnerCladding_vec.clear();       
	lengthInOuterCladding_vec.clear();       
	//   detectorCopy_vec.clear();	        
	//  subdetectorCopy_vec.clear();             
	
	sameEvent_bool = true;
      }

      if (sameEvent_bool == true){
	runID_vec.push_back(runID);
	eventID_vec.push_back(eventID);
	detNumb_vec.push_back(detNumb);
	xPixel_vec.push_back(xPixel);
	yPixel_vec.push_back(yPixel);
	energy_vec.push_back(energy);
	wavelength_vec.push_back(wavelength);
	localtime_vec.push_back(localtime);
	abstime_vec.push_back(abstime);
	length_vec.push_back(length);
	x_vec.push_back(x);
	y_vec.push_back(y);
	z_vec.push_back(z);
	px_vec.push_back(px);
	py_vec.push_back(py);
	pz_vec.push_back(pz);
	vertexX_vec.push_back(vertexX);
	vertexY_vec.push_back(vertexY);
	vertexZ_vec.push_back(vertexZ);
	vertexPx_vec.push_back(vertexPx);
	vertexPy_vec.push_back(vertexPy);
	vertexPz_vec.push_back(vertexPz);
	gpsPosX_vec.push_back(gpsPosX);
	gpsPosY_vec.push_back(gpsPosY);
	gpsPosZ_vec.push_back(gpsPosZ);
	gpsDirX_vec.push_back(gpsDirX);
	gpsDirY_vec.push_back(gpsDirY);
	gpsDirZ_vec.push_back(gpsDirZ);
	runId_vec.push_back(runId);
	eventId_vec.push_back(eventId);
	trackId_vec.push_back(trackId);
	creatorProcess_vec.push_back(creatorProcess);
	parentId_vec.push_back(parentId);
	lengthInCore_vec.push_back(lengthInCore);
	lengthInInnerCladding_vec.push_back(lengthInInnerCladding);
	lengthInOuterCladding_vec.push_back(lengthInOuterCladding);
	//	detectorCopy_vec.push_back(detectorCopy);
	//   	subdetectorCopy_vec.push_back(subdetectorCopy);
	
      }
      

   
      
   }//end of loop
   outfile->Write();

   delete outfile;  
}
