// ----------------------------------------------------------------------------
// $Id: sio_GoFullRecon.C,v 1.7 2002/01/17 23:34:31 masako Exp $
// ----------------------------------------------------------------------------
//
// Read the GISMO output data (SIO format), then do FullRecon
//
// Sample program                      04/26/2001 Toshi
//
// :: How to run ::
// 
// 1) to call the root, type   
//    
//     root
//
// 2)
//     root [0]  .x sio_GoFullReconMC.C
// 
// .. that's it.
//
// For example, if you like to run 300 events, type like
//     root [0]  .x sio_GoFullReconMC.C(300)
//

Int_t sio_GoFullRecon(Int_t nEvent=1000) {
  gROOT->Reset();

  TString parfile_dir;
  parfile_dir += gSystem->Getenv("LCDROOT")   ;parfile_dir += "/";
  parfile_dir += "ParFiles/";

  // In order to refer to shareable libraries in this simple form,
  // include their paths in a .rootrc file.
  gSystem->Load("libLCDEvent");
  gSystem->Load("libLCDRootAppsUtil");
  gSystem->Load("libLCDFastMC");
  gSystem->Load("libLCDFullRecon");
  gSystem->Load("libLCDPhUtil");
  if (strcmp(gSystem->GetName(),"WinNT")) {
    gSystem->Load("libSIO2root");
  }
  gSystem->Load("libLCDSIOUtil");

  LCDEvent event;

  // Input sio file (GISMO output)
  Char_t* sioFile  = "test.sio";
  LCDreadSIOFile source(sioFile,&event);

  // Must use the same detector name as Full simulation
  Char_t* geomFile = 
    "SDMar01.par"; // SD
  //"LDMar01.par"; // LD
  TString s_geomFile(parfile_dir);
  s_geomFile += geomFile;
  FILE* m_geomfile =  fopen(s_geomFile.Data(),"r");
  LCDGetParameters gp(m_geomfile);
  fclose(m_geomfile);

  Char_t* smearFile = 
    "lookup_silicon.nobmcon";
  //"lookup_large.nobmcon";
  TString s_smearFile(parfile_dir);
  s_smearFile += smearFile;

  // Setup FullRecon
  LCDFullRecon *frec=
  //new LCDFullRecon(&gp, s_smearFile.Data(), &event);    // For GEANT4
    new LCDFullRecon(&gp, s_smearFile.Data(), &event, 1); // For GISMO

  // Define a histogram or two
  TH1F *ClsMult = new TH1F("ClsMult","Cluster Multiplicity",50, 0.0, 200.0);
  TH1F *ClsE    = new TH1F("ClsE","Cluster Energy",50, 0.0, 50.0);
  TH1F *TrkMult = new TH1F("TrkMult","Track Multiplicity",50, 0.0, 100.0);
  TH1F *TrkE    = new TH1F("TrkE","Track Energy",50, 0.0, 50.0);

  // Event loop
  Int_t iEvent;
  for (iEvent = 0; iEvent < nEvent; iEvent++) {

    if (!source.GetEvent()) break; // Get an event from the input

    frec->Doit();                   // Do FullRecon

    LCDBeam* beam = event.BEAM();
    Double_t Ecm = beam->GetEcm();
    TVector3 IP  = beam->GetPos();
    if (iEvent == 0) printf(" Ecm = %f \n", Ecm);
    //printf(" beam position = %f %f %f \n",
    //	  IP.X(), IP.Y(), IP.Z());    

    // Look at Clusters. Can access them by the [] operator.
    int nClusters = event.ClusterLst()->GetEntries();
    ClsMult->Fill(nClusters);

    for (Int_t iClus = 0; iClus < nClusters; iClus++) {
      LCDCluster* cls = (LCDCluster*)event.ClusterLst()->At(iClus);

      if (cls) {
	Double_t     eClus     = cls->GetEnergy();	
	// Histogram energies
	ClsE->Fill(eClus);
      }

    }
    

    // Look at tracks. Can access them by the [] operator.
    int nTracks = event.Tracks()->GetEntries();
    TrkMult->Fill(nTracks);

    // Track loop
    for (Int_t ntrk=0; ntrk<nTracks; ntrk++) {
      LCDTrack* trk = (LCDTrack*)event.Tracks()->At(ntrk);
      
      if(trk) {
	Double_t* tP3 = trk->GetMomentum();
	Double_t  E_trk = 0.1396 * 0.1396; // Assume pion mass
	for (int i=0; i<3; i++) { E_trk += (*(tP3+i))*(*(tP3+i)); }
	E_trk = sqrt(E_trk);

	// Histogram energies
	TrkE->Fill(E_trk);
      }

    }

    event.Clear();

  }

  ClsMult->SetFillColor(4);
  ClsE->SetFillColor(4);
  TrkMult->SetFillColor(4);
  TrkE->SetFillColor(4);

  TCanvas *c1 = new TCanvas("c1", "c1", 600, 600);
  c1->Divide(2,2);
  c1->cd(1); ClsMult->Draw("B");
  c1->cd(2); ClsE->Draw("B");
  c1->cd(3); TrkMult->Draw("B");
  c1->cd(4); TrkE->Draw("B");

  return iEvent;
}
