//-----------------------------------------------------------------------------
// $Id: pythia_EventDisp.C,v 1.5 2001/09/20 01:45:07 toshi Exp $
//-----------------------------------------------------------------------------
//
//  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 += "ParFiles/";

  // In order to refer to shareable libraries in this simple form,
  // include their paths in a .rootrc file.
  if (strcmp(gSystem->GetName(),"WinNT")) {
    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) >------------------------
  printf("Start Generator Setup!\n");

  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;

  printf("End of Generator Setup!\n");
  //------------< End Generator setup>-------------------

  LCDEvent event;

  //------------< Setup Fast MC >------------------------
  printf("Start FastMC Setup!\n");

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

  printf("End of FastMC Setup!\n");
  //------------< End Fast MC setup >--------------------

  //------------< Setup Event Display >------------------
  printf("Start EventDisplay Setup!\n");

  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.);

  printf("End of EventDisplay Setup!\n");
  //------------< 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;
}
