// ----------------------------------------------------------------------------
// $Id: root_EventDisp.C,v 1.9 2001/09/20 01:45:26 toshi Exp $
// ----------------------------------------------------------------------------
//
//  Example file to run Event Display interactively with Root.
//  
//  In this file, 1) read generated event (root file)
//                2) through Fast MC
//                3) make event display
//
//                                   Apr. 2001   M. Iwasaki
//
// :: How to run ::
// 
// 1) to call the root, type   
//    
//     root
//
// 2)
//     root [0]  .x root_EventDisp.C
// 
// .. that's it.
//
// For example, if you like to run 3 events, type like
//     root [0]  .x root_EventDisp.C(3)
//

#include <stdlib.h>

int root_EventDisp(int nEvent=5)
{
  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("libLCDPhUtil");

  LCDEvent event;

  // Open a root file (generator output)
  Char_t* inputFile = "test.root";
  LCDreadRootFile source(inputFile,&event);

  // Call detector parameter file
  char* detectorGeomFile = 
    "SDMar01.par"; // SD
  //"LDMar01.par"; // LD
  TString s_geomFile(parfile_dir);
  s_geomFile += detectorGeomFile;
  FILE* m_geomfile =  fopen(s_geomFile.Data(),"r");
  LCDGetParameters gp(m_geomfile);
  fclose(m_geomfile);

  // If you want to change some detector parameter(s) for FastMC 
  // do here
  // For example..
  //gp.SetMagneticField(2.);  // Change Magnetif field
  //gp.SetCoilinHAD();        // Set HAD inside the Coil (This case
                              // you need to change some detector size also..)

  // Call Tracker smearing parameter
  char* trkSmearFile = "lookup_silicon.nobmcon";
  //"lookup_large.nobmcon";
  TString s_smearFile(parfile_dir);
  s_smearFile += trkSmearFile;

  // Initialize FastMC
  LCDFastMC fmc(&gp, s_smearFile.Data(), &event);
  // If you don't want the Cluster merging
  //fmc.SetNoClusterMerge();

  // Setup Event Display
  LCDEventDisplay evdisp(&gp);

  //Select Black-White option, if you like
  //evdisp->SetBlackWhite(); 

  //evdisp.SetNoMUCAL();  // Do not display MUCAL part
  //evdisp.SetNoHADCAL(); // Do not display HADCAL part
  //evdisp.SetNoCOIL();   // Do not display COIL part
  //evdisp.SetTheta(90.);
  //evdisp.SetPhi(30.);

  // Event loop
  Int_t iEvent;
  for (iEvent = 0; iEvent < nEvent; iEvent++) {
    if (!source.GetEvent()) break; // Read an event from the ROOT file.

    fmc.Doit();                    // Through Fast simulation.

    if (iEvent == 0){
      // List Event Header information, for example
      LCDEventHeader* header = (LCDEventHeader*)event.EventHeader();
      printf("\n");
      printf("Process:: %s\n",header->GetProcessName());

      // List beam parameters 
      LCDBeam* beam = (LCDBeam*)event.BEAM();
      printf("Ecm = %f\n",beam->GetEcm());
      printf("electron polarization : %f  positron polarization : %f\n",
	     beam->GetPolElec(), beam->GetPolPosi());
      printf("==> effective polarization for electron : %f \n\n",
	     beam->GetEffectivePol());
    }

    evdisp.Draw(&event);           // Event Display

    Int_t a;
    printf("To get the next event, enter any number (<0 Exit Event loop)\n");
    scanf("%d",&a);
    if (a < 0) break;

  }
  return iEvent;
}

