// Run Fast MC interactively with Root
// Sample program using EventShape       May/12/2000 M. Iwasaki
//
//
// :: How to run ::
// 
// 1) to call the root, type   
//    
//     root
//
// 2)
//     root [0]  .x Thrust.C
// 
// .. that's it.
//
// For example, if you like to run 300 events, type like
//     root [0]  .x Thrust.C(300)
//

#include <stdlib.h>

int Thrust(int nEvent=100)
{
  gROOT->Reset();

  // In order to refer to shareable libraries in this simple form,
  // include their paths in a .rootrc file.
  gSystem->Load("libLCDEvent.so");
  gSystem->Load("libLCDRootAppsUtil.so");
  gSystem->Load("libLCDFastMC.so");
  gSystem->Load("libLCDPhUtil.so");

  // Input STDHEP file
  char* stdFile = "test.dat";

  // Detector parameter file
  char* geomFile = 
    "/afs/slac/g/nld/RootApps/V2.1/ParFiles/Small.par";

  // Tracker smearing parameter file
  char* smearFile = 
    "/afs/slac/g/nld/RootApps/V2.1/ParFiles/lookup_small.nobmcon";

  LCDreadStdFile *source = new LCDreadStdFile(stdFile);
  LCDFastMC* fmc = new LCDFastMC(source, geomFile, smearFile, 0);
  LCDEvent*  event = 0;

  // Define histograms
  TH1F *THRUST = new TH1F("THRUST","Thrust", 50,0.,1.);
  TH1F *MAJOR  = new TH1F("MAJOR ","Major ", 50,0.,1.);
  TH1F *MINOR  = new TH1F("MINOR ","Minor ", 50,0.,1.);
  TH1F *THRUSTDIRZ = new TH1F("THRUSTDIRZ","Thrust Direction Z", 50,-1.,1.);

  // Set up Thrust Finder
  LCDEventShape* eshape = new LCDEventShape();

  TObjArray* veclist = new TObjArray();

  // Event loop
  for (int iEvent = 0; iEvent < nEvent; iEvent++) {

    event = new LCDEvent();
    event->Create();
    Int_t ierr = fmc->FetchEvent(event); // Through FastMC
    if (!ierr) break;

    fmc->Cleanup();
    fmc->Doit(event);


    // Look at tracks. Can access them by the [] operator.
    int nTracks = event->Tracks()->GetEntries();
    
    // Event cut # track >= 2
    if (nTracks < 2) continue;

    veclist -> Delete(); // Clean up veclist

    // Track loop    
    for (Int_t ntrk=0; ntrk<nTracks; ntrk++) {
	LCDTrack* trk = (LCDTrack*)event->Tracks()->At(ntrk);
	
	Double_t* tP3 = trk->GetMomentum();
	TVector3* vec3 = new TVector3(tP3);

	veclist->Add(vec3);  // Make particle 3-vector list
    }
    
    // Do Thrust Finding
    eshape->SetPartList(veclist); // Input particle 3-vector list
    
    Double_t thrust = eshape->GetThrust();
    Double_t major  = eshape->GetMajor();
    Double_t minor  = eshape->GetMinor();
    
    THRUST->Fill(thrust);
    MAJOR ->Fill(major);
    MINOR ->Fill(minor);
    THRUSTDIRZ->Fill(eshape->GetThrustAxis()->Z());
    

    event->Clean(); delete event;
  }

  veclist->Delete(); delete veclist;    

  THRUST    ->SetFillColor(4);
  MAJOR     ->SetFillColor(4);
  MINOR     ->SetFillColor(4);
  THRUSTDIRZ->SetFillColor(4);

  TCanvas *c1 = new TCanvas("c1", "c1", 700,900);
  c1->Divide(2,2);
  c1->cd(1); THRUST->Draw("B");
  c1->cd(2); MAJOR->Draw("B");
  c1->cd(3); MINOR->Draw("B");
  c1->cd(4); THRUSTDIRZ->Draw("B");

  return 1;
}
