#include <fstream.h>

#include <TROOT.h>
#include <TStopwatch.h>
#include <TObjArray.h>
#include <TVector3.h>

#include "./BFLHandler.h"

TROOT TESTB("TESTB","Testing B Field vectors");

int main(int argc, char** argv)
{ 
   Int_t id = 0;
   Float_t x = 0, y = 0, z = 0;
   TVector3 BVector;
   TObjArray  * allhits = new TObjArray(2000);
   BFLHandler * bfield = new BFLHandler(kNear); 

   Int_t interpmethod = kNN; // see BFLHandler.h  kCN=0, kNN=1, kBL=2
   //   interpmethod = kCN;

   if (argc>1) {
      sscanf(argv[1],"%d",&interpmethod);
      cout << " ... user arg set interpmethod to : " << interpmethod << endl;
   }

   bfield->SetInterpolant(interpmethod);   

   cout << "getting all hits for a nu_mu event" << endl;
   ifstream HITS("NuMuEventHits.dat",ios::in);
   while(!HITS.eof()) {
	HITS >> id >> x >> y >> z;
	allhits->AddAt(new TVector3(10*x,10*y,10*z),id);
   }

   Int_t calls = 0;
   Int_t mxpasses = 10;

   TStopwatch timer;
   timer.Start();
   cout << " pass " << flush;
   for (Int_t npass=0; npass<mxpasses; npass++) {
      cout << npass << "." << flush;
      TIter next(allhits);
      TVector3 * position;
   
      while( (position = (TVector3 *)next())){
	 BVector = bfield->GetBField(*position);
	 bfield->SetLastPolygAsSeed();
         calls++;
      }
   }
   cout << endl;
   timer.Stop();
   

   cout << "TOTAL TIME: " << timer.CpuTime()
        << " for " << calls << " calls using iterp method "
        << interpmethod << endl;
   cout << "     " << 1000.*timer.CpuTime()/(float)calls
        << " ms/call" << endl;

   cout << " delete allhits" << endl;
   delete allhits;    

   cout << " delete bfield" << endl;
   delete bfield;

   return(0);
}

