//-----------------------------------------------------------------------------
// $Id: pythia_EventDisp.C,v 1.2 2001/06/19 16:51:59 toshi Exp $
//-----------------------------------------------------------------------------
// $Log: pythia_EventDisp.C,v $
// Revision 1.2  2001/06/19 16:51:59  toshi
// Use gSystem->Getenv() to get Parfiles directory.
//
// Revision 1.1  2001/05/18 23:53:56  masako
// Create a new example file to run pythia and EventDisplay.
//
//
//  Example file to run Event Display interactively with Root.
//  
//  In this file, 1) event generation with Pythia (TPythia6)
//                2) through Fast MC
//                3) make event display
//
//  For example, generate e+e- --> W+W- events
//   
//                               May. 18 2001  M.Iwasaki
//
//   :: How to run ::
// 
//   1) type root
//   2)
//      root [0]  .x pythia_EventDisp.C
//
// For example, if you like to run 20 events, type like
//      root [0]  .x pythia_EventDisp.C(20)
//    

//__________________________________________________________________________

Int_t pythia_EventDisp(Int_t nEvent=10){

  gROOT->Reset();

  TString parfile_dir;
  parfile_dir += gSystem->Getenv("LCDROOT")   ;parfile_dir += "/";
  parfile_dir += gSystem->Getenv("LCDVERSION");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("libPythia6");
  gSystem->Load("libEG");
  gSystem->Load("libEGPythia6");
  gSystem->Load("libLCDEvent");
  gSystem->Load("libLCDRootAppsUtil");
  gSystem->Load("libLCDFastMC");
  gSystem->Load("libLCDPhUtil");
  gSystem->Load("libLCDGenUtil");

  //------------< Setup Generator (TPythia6) >------------------------
  cout<<"Start Generator Setup!"<<endl;

  double ECM  = 500.;
  int    iseed_pyth =  19518503;// pythia initial seed

  TPythia6 pyth6;

  //...t mass.
  pyth6.SetPMAS(6,1,175.);

  // Select e+e- --> W+W- process
  pyth6.SetMSEL(0);
  pyth6.SetMSUB(25,1);

  pyth6.Pyinit("CMS","E-","E+",ECM);
 
  // Change pythia seed  
  pyth6.SetMRPY(1,iseed_pyth); 

  //Pyjet -> McPart converter
  LCDPyj2McPart py2mc;

  cout<<"End of Generator Setup!"<<endl;
  //------------< End Generator setup>-------------------

  LCDEvent event;

  //------------< Setup Fast MC >------------------------
  cout<<"Start FastMC Setup!"<<endl;

  // Call detector parameter file
  char* detectorGeomFile = "Silicon.par"; // Silicon 
  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

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

  // Initialize FastMC
  LCDFastMC fmc(&gp, s_smearFile.Data(), &event);

  cout<<"End of FastMC Setup!"<<endl;
  //------------< End Fast MC setup >--------------------

  //------------< Setup Event Display >------------------
  cout<<"Start EventDisplay Setup!"<<endl;

  LCDEventDisplay evdisp(&gp);

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

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

  cout<<"End of EventDisplay Setup!"<<endl;
  //------------< End Event Display setup >--------------

  Int_t iEvent;
  for (Int_t iEvent=0; iEvent < nEvent ; iEvent++) {
    pyth6.GenerateEvent(); //Event Generation with pythia
    py2mc.MakeMcPart(event.MCparticles()); //Make LCDMcPart 

    fmc.Doit();          //Through FastMC

    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;

  }
  
  pyth6.Pystat(1); // Pythia summary

  return iEvent;
}
