// @(#)root/pythia8:$Name$:$Id$
// Author: Andreas Morsch   27/10/2007

/*************************************************************************
 * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/


////////////////////////////////////////////////////////////////////////////////
//                                                                            //
// TPythia8                                                                   //
//                                                                            //
// TPythia is an interface class to C++ version of Pythia 8.1                 //
// event generators, written by T.Sjostrand.                                  //
//                                                                            //
// The user is assumed to be familiar with the Pythia package.                //
// This class includes only a basic interface to Pythia8. Because Pythia8 is  //
// also written in C++, its functions/classes can be called directly from a   //
// compiled C++ script.                                                       //
// To call Pythia functions not available in this interface a dictionary must //
// be generated.                                                              //
// see $ROOTSYS/tutorials/pythia/pythia8.C for an example of use from CINT.   //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
/*
*------------------------------------------------------------------------------------*
 |                                                                                    |
 |  *------------------------------------------------------------------------------*  |
 |  |                                                                              |  |
 |  |                                                                              |  |
 |  |   PPP   Y   Y  TTTTT  H   H  III    A      Welcome to the Lund Monte Carlo!  |  |
 |  |   P  P   Y Y     T    H   H   I    A A     This is PYTHIA version 8.100      |  |
 |  |   PPP     Y      T    HHHHH   I   AAAAA    Last date of change: 20 Oct 2007  |  |
 |  |   P       Y      T    H   H   I   A   A                                      |  |
 |  |   P       Y      T    H   H  III  A   A    Now is 27 Oct 2007 at 18:26:53    |  |
 |  |                                                                              |  |
 |  |   Main author: Torbjorn Sjostrand; CERN/PH, CH-1211 Geneva, Switzerland,     |  |
 |  |     and Department of Theoretical Physics, Lund University, Lund, Sweden;    |  |
 |  |     phone: + 41 - 22 - 767 82 27; e-mail: torbjorn@thep.lu.se                |  |
 |  |   Author: Stephen Mrenna; Computing Division, Simulations Group,             |  |
 |  |     Fermi National Accelerator Laboratory, MS 234, Batavia, IL 60510, USA;   |  |
 |  |     phone: + 1 - 630 - 840 - 2556; e-mail: mrenna@fnal.gov                   |  |
 |  |   Author: Peter Skands; CERN/PH, CH-1211 Geneva, Switzerland,                |  |
 |  |     and Theoretical Physics Department,                                      |  |
 |  |     Fermi National Accelerator Laboratory, MS 106, Batavia, IL 60510, USA;   |  |
 |  |     phone: + 41 - 22 - 767 24 59; e-mail: skands@fnal.gov                    |  |
 |  |                                                                              |  |
 |  |   The main program reference is the 'Brief Introduction to PYTHIA 8.1',      |  |
 |  |   T. Sjostrand, S. Mrenna and P. Skands, arXiv:0710.3820                     |  |
 |  |                                                                              |  |
 |  |   The main physics reference is the 'PYTHIA 6.4 Physics and Manual',         |  |
 |  |   T. Sjostrand, S. Mrenna and P. Skands, JHEP05 (2006) 026 [hep-ph/0603175]. |  |
 |  |                                                                              |  |
 |  |   An archive of program versions and documentation is found on the web:      |  |
 |  |   http://www.thep.lu.se/~torbjorn/Pythia.html                                |  |
 |  |                                                                              |  |
 |  |   This program is released under the GNU General Public Licence version 2.   |  |
 |  |   Please respect the MCnet Guidelines for Event Generator Authors and Users. |  |
 |  |                                                                              |  |
 |  |   Disclaimer: this program comes without any guarantees.                     |  |
 |  |   Beware of errors and use common sense when interpreting results.           |  |
 |  |                                                                              |  |
 |  |   Copyright (C) 2007 Torbjorn Sjostrand                                      |  |
 |  |                                                                              |  |
 |  |                                                                              |  |
 |  *------------------------------------------------------------------------------*  |
 |                                                                                    |
 *------------------------------------------------------------------------------------*
*/

#include "TPythia8.h"

#include "TClonesArray.h"
#include "TParticle.h"
#include "TDatabasePDG.h"
#include "TLorentzVector.h"

ClassImp(TPythia8)

TPythia8*  TPythia8::fgInstance = 0;

//___________________________________________________________________________
TPythia8::TPythia8():
    TGenerator("TPythia8", "TPythia8"),
    fPythia(0),
    fNumberOfParticles(0)
{
   // Constructor
   if (fgInstance)
      Fatal("TPythia8", "There's already an instance of TPythia8");

   delete fParticles; // was allocated as TObjArray in TGenerator

   fParticles = new TClonesArray("TParticle",50);
   fPythia    = new Pythia8::Pythia();
}

//___________________________________________________________________________
TPythia8::TPythia8(const char *xmlDir):
    TGenerator("TPythia8", "TPythia8"),
    fPythia(0),
    fNumberOfParticles(0)
{
   // Constructor with an xmlDir (eg "../xmldoc"
   if (fgInstance)
      Fatal("TPythia8", "There's already an instance of TPythia8");

   delete fParticles; // was allocated as TObjArray in TGenerator

   fParticles = new TClonesArray("TParticle",50);
   fPythia    = new Pythia8::Pythia(xmlDir);
}

//___________________________________________________________________________
TPythia8::~TPythia8()
{
   // Destructor
   if (fParticles) {
      fParticles->Delete();
      delete fParticles;
      fParticles = 0;
   }
   delete fPythia;
}

//___________________________________________________________________________
TPythia8* TPythia8::Instance()
{
   // Return an instance of TPythia8
   return fgInstance ? fgInstance : (fgInstance = new TPythia8()) ;
}

//___________________________________________________________________________
Bool_t TPythia8::Initialize(Int_t idAin, Int_t idBin, Double_t ecms)
{
   // Initialization
   AddParticlesToPdgDataBase();
   return fPythia->init(idAin, idBin, ecms);
}

//___________________________________________________________________________
Bool_t TPythia8::Initialize(Int_t idAin, Int_t idBin, Double_t eAin, Double_t eBin)
{
   // Initialization
   AddParticlesToPdgDataBase();
   return fPythia->init(idAin, idBin, eAin, eBin);
}

//___________________________________________________________________________
void TPythia8::GenerateEvent()
{
   // Generate the next event
   fPythia->next();
   fNumberOfParticles  = fPythia->event.size() - 1;
   ImportParticles();
}
//___________________________________________________________________________
Int_t TPythia8::ImportParticles(TClonesArray *particles, Option_t *option)
{
   // Import particles from Pythia stack
   if (particles == 0) return 0;
   TClonesArray &clonesParticles = *particles;
   clonesParticles.Clear();
   Int_t nparts=0;
   Int_t i;
   Int_t ioff = 0;
   fNumberOfParticles  = fPythia->event.size();
   if (fPythia->event[0].id() == 90) {
     ioff = -1;
   }

   if (!strcmp(option,"") || !strcmp(option,"Final")) {
      for (i = 0; i < fNumberOfParticles; i++) {
	if (fPythia->event[i].id() == 90) continue;
         if (fPythia->event[i].isFinal()) {
            new(clonesParticles[nparts]) TParticle(
                fPythia->event[i].id(),
                fPythia->event[i].isFinal(),
                fPythia->event[i].mother1() + ioff,
                fPythia->event[i].mother2() + ioff,
                fPythia->event[i].daughter1() + ioff,
                fPythia->event[i].daughter2() + ioff,
                fPythia->event[i].px(),     // [GeV/c]
                fPythia->event[i].py(),     // [GeV/c]
                fPythia->event[i].pz(),     // [GeV/c]
                fPythia->event[i].e(),      // [GeV]
                fPythia->event[i].xProd(),  // [mm]
                fPythia->event[i].yProd(),  // [mm]
                fPythia->event[i].zProd(),  // [mm]
                fPythia->event[i].tProd()); // [mm/c]
		nparts++;
	    } // final state partice
	} // particle loop
    } else if (!strcmp(option,"All")) {
	for (i = 0; i < fNumberOfParticles; i++) {
	  if (fPythia->event[i].id() == 90) continue;
	    new(clonesParticles[nparts]) TParticle(
		fPythia->event[i].id(),
		fPythia->event[i].isFinal(),
		fPythia->event[i].mother1() + ioff,
		fPythia->event[i].mother2() + ioff,
		fPythia->event[i].daughter1() + ioff,
		fPythia->event[i].daughter2() + ioff,
		fPythia->event[i].px(),       // [GeV/c]
		fPythia->event[i].py(),       // [GeV/c]
		fPythia->event[i].pz(),       // [GeV/c]
		fPythia->event[i].e(),        // [GeV]
		fPythia->event[i].xProd(),    // [mm]
		fPythia->event[i].yProd(),    // [mm]
		fPythia->event[i].zProd(),    // [mm]
		fPythia->event[i].tProd());   // [mm/c]
     	        nparts++;
	} // particle loop
    }
    if(ioff==-1)     fNumberOfParticles--;
    return nparts;
}

//___________________________________________________________________________
TObjArray* TPythia8::ImportParticles(Option_t* /* option */)
{
   // Import particles from Pythia stack
   fParticles->Clear();
   Int_t ioff = 0;
   Int_t numpart   = fPythia->event.size();
   if (fPythia->event[0].id() == 90) {
     numpart--;
     ioff = -1;
   }


   TClonesArray &a = *((TClonesArray*)fParticles);
   for (Int_t i = 1; i <= numpart; i++) {
      new(a[i]) TParticle(
         fPythia->event[i].id(),
         fPythia->event[i].isFinal(),
         fPythia->event[i].mother1()   + ioff,
         fPythia->event[i].mother2()   + ioff,
         fPythia->event[i].daughter1() + ioff,
         fPythia->event[i].daughter2() + ioff,
         fPythia->event[i].px(),       // [GeV/c]
         fPythia->event[i].py(),       // [GeV/c]
         fPythia->event[i].pz(),       // [GeV/c]
         fPythia->event[i].e(),        // [GeV]
         fPythia->event[i].xProd(),    // [mm]
         fPythia->event[i].yProd(),    // [mm]
         fPythia->event[i].zProd(),    // [mm]
         fPythia->event[i].tProd());   // [mm/c]
   }
   return fParticles;
}

//___________________________________________________________________________
Int_t TPythia8::GetN() const
{
   // Initialization
   return (fPythia->event.size() - 1);
}

//___________________________________________________________________________
void TPythia8::ReadString(const char* string) const
{
   // Configuration
   fPythia->readString(string);
}

//___________________________________________________________________________
void  TPythia8::ReadConfigFile(const char* string) const
{
  // Configuration
  fPythia->readFile(string);
}

//___________________________________________________________________________
void TPythia8::ListAll() const
{
   // Event listing
   fPythia->settings.listAll();
}

//___________________________________________________________________________
void TPythia8::ListChanged() const
{
   // Event listing
   fPythia->settings.listChanged();
}

//___________________________________________________________________________
void TPythia8::Plist(Int_t id) const
{
   // Event listing
   fPythia->particleData.list(id);
}

//___________________________________________________________________________
void TPythia8::PlistAll() const
{
   // Event listing
   fPythia->particleData.listAll();
}

//___________________________________________________________________________
void TPythia8::PlistChanged() const
{
   // Event listing
   fPythia->particleData.listChanged();
}

//___________________________________________________________________________
void TPythia8::PrintStatistics() const
{
   // Print end of run statistics
   fPythia->statistics();
}

//___________________________________________________________________________
void TPythia8::EventListing() const
{
   // Event listing
   fPythia->event.list();
}

//___________________________________________________________________________
void TPythia8::AddParticlesToPdgDataBase()
{
   // Add some pythia specific particle code to the data base

   TDatabasePDG *pdgDB = TDatabasePDG::Instance();
   pdgDB->AddParticle("string","string", 0, kTRUE,
                      0, 0, "QCD string", 90);
   pdgDB->AddParticle("rho_diff0", "rho_diff0", 0, kTRUE,
                      0, 0, "QCD diffr. state", 9900110);
   pdgDB->AddParticle("pi_diffr+", "pi_diffr+", 0, kTRUE,
                      0, 1, "QCD diffr. state", 9900210);
   pdgDB->AddParticle("omega_di", "omega_di", 0, kTRUE,
                      0, 0, "QCD diffr. state", 9900220);
   pdgDB->AddParticle("phi_diff","phi_diff", 0, kTRUE,
                      0, 0, "QCD diffr. state", 9900330);
   pdgDB->AddParticle("J/psi_di", "J/psi_di", 0, kTRUE,
                      0, 0, "QCD diffr. state", 9900440);
   pdgDB->AddParticle("n_diffr0","n_diffr0",0,kTRUE,
                      0, 0, "QCD diffr. state", 9902110);
   pdgDB->AddParticle("p_diffr+","p_diffr+", 0, kTRUE,
                      0, 1, "QCD diffr. state", 9902210);
}

 TPythia8.cxx:1
 TPythia8.cxx:2
 TPythia8.cxx:3
 TPythia8.cxx:4
 TPythia8.cxx:5
 TPythia8.cxx:6
 TPythia8.cxx:7
 TPythia8.cxx:8
 TPythia8.cxx:9
 TPythia8.cxx:10
 TPythia8.cxx:11
 TPythia8.cxx:12
 TPythia8.cxx:13
 TPythia8.cxx:14
 TPythia8.cxx:15
 TPythia8.cxx:16
 TPythia8.cxx:17
 TPythia8.cxx:18
 TPythia8.cxx:19
 TPythia8.cxx:20
 TPythia8.cxx:21
 TPythia8.cxx:22
 TPythia8.cxx:23
 TPythia8.cxx:24
 TPythia8.cxx:25
 TPythia8.cxx:26
 TPythia8.cxx:27
 TPythia8.cxx:28
 TPythia8.cxx:29
 TPythia8.cxx:30
 TPythia8.cxx:31
 TPythia8.cxx:32
 TPythia8.cxx:33
 TPythia8.cxx:34
 TPythia8.cxx:35
 TPythia8.cxx:36
 TPythia8.cxx:37
 TPythia8.cxx:38
 TPythia8.cxx:39
 TPythia8.cxx:40
 TPythia8.cxx:41
 TPythia8.cxx:42
 TPythia8.cxx:43
 TPythia8.cxx:44
 TPythia8.cxx:45
 TPythia8.cxx:46
 TPythia8.cxx:47
 TPythia8.cxx:48
 TPythia8.cxx:49
 TPythia8.cxx:50
 TPythia8.cxx:51
 TPythia8.cxx:52
 TPythia8.cxx:53
 TPythia8.cxx:54
 TPythia8.cxx:55
 TPythia8.cxx:56
 TPythia8.cxx:57
 TPythia8.cxx:58
 TPythia8.cxx:59
 TPythia8.cxx:60
 TPythia8.cxx:61
 TPythia8.cxx:62
 TPythia8.cxx:63
 TPythia8.cxx:64
 TPythia8.cxx:65
 TPythia8.cxx:66
 TPythia8.cxx:67
 TPythia8.cxx:68
 TPythia8.cxx:69
 TPythia8.cxx:70
 TPythia8.cxx:71
 TPythia8.cxx:72
 TPythia8.cxx:73
 TPythia8.cxx:74
 TPythia8.cxx:75
 TPythia8.cxx:76
 TPythia8.cxx:77
 TPythia8.cxx:78
 TPythia8.cxx:79
 TPythia8.cxx:80
 TPythia8.cxx:81
 TPythia8.cxx:82
 TPythia8.cxx:83
 TPythia8.cxx:84
 TPythia8.cxx:85
 TPythia8.cxx:86
 TPythia8.cxx:87
 TPythia8.cxx:88
 TPythia8.cxx:89
 TPythia8.cxx:90
 TPythia8.cxx:91
 TPythia8.cxx:92
 TPythia8.cxx:93
 TPythia8.cxx:94
 TPythia8.cxx:95
 TPythia8.cxx:96
 TPythia8.cxx:97
 TPythia8.cxx:98
 TPythia8.cxx:99
 TPythia8.cxx:100
 TPythia8.cxx:101
 TPythia8.cxx:102
 TPythia8.cxx:103
 TPythia8.cxx:104
 TPythia8.cxx:105
 TPythia8.cxx:106
 TPythia8.cxx:107
 TPythia8.cxx:108
 TPythia8.cxx:109
 TPythia8.cxx:110
 TPythia8.cxx:111
 TPythia8.cxx:112
 TPythia8.cxx:113
 TPythia8.cxx:114
 TPythia8.cxx:115
 TPythia8.cxx:116
 TPythia8.cxx:117
 TPythia8.cxx:118
 TPythia8.cxx:119
 TPythia8.cxx:120
 TPythia8.cxx:121
 TPythia8.cxx:122
 TPythia8.cxx:123
 TPythia8.cxx:124
 TPythia8.cxx:125
 TPythia8.cxx:126
 TPythia8.cxx:127
 TPythia8.cxx:128
 TPythia8.cxx:129
 TPythia8.cxx:130
 TPythia8.cxx:131
 TPythia8.cxx:132
 TPythia8.cxx:133
 TPythia8.cxx:134
 TPythia8.cxx:135
 TPythia8.cxx:136
 TPythia8.cxx:137
 TPythia8.cxx:138
 TPythia8.cxx:139
 TPythia8.cxx:140
 TPythia8.cxx:141
 TPythia8.cxx:142
 TPythia8.cxx:143
 TPythia8.cxx:144
 TPythia8.cxx:145
 TPythia8.cxx:146
 TPythia8.cxx:147
 TPythia8.cxx:148
 TPythia8.cxx:149
 TPythia8.cxx:150
 TPythia8.cxx:151
 TPythia8.cxx:152
 TPythia8.cxx:153
 TPythia8.cxx:154
 TPythia8.cxx:155
 TPythia8.cxx:156
 TPythia8.cxx:157
 TPythia8.cxx:158
 TPythia8.cxx:159
 TPythia8.cxx:160
 TPythia8.cxx:161
 TPythia8.cxx:162
 TPythia8.cxx:163
 TPythia8.cxx:164
 TPythia8.cxx:165
 TPythia8.cxx:166
 TPythia8.cxx:167
 TPythia8.cxx:168
 TPythia8.cxx:169
 TPythia8.cxx:170
 TPythia8.cxx:171
 TPythia8.cxx:172
 TPythia8.cxx:173
 TPythia8.cxx:174
 TPythia8.cxx:175
 TPythia8.cxx:176
 TPythia8.cxx:177
 TPythia8.cxx:178
 TPythia8.cxx:179
 TPythia8.cxx:180
 TPythia8.cxx:181
 TPythia8.cxx:182
 TPythia8.cxx:183
 TPythia8.cxx:184
 TPythia8.cxx:185
 TPythia8.cxx:186
 TPythia8.cxx:187
 TPythia8.cxx:188
 TPythia8.cxx:189
 TPythia8.cxx:190
 TPythia8.cxx:191
 TPythia8.cxx:192
 TPythia8.cxx:193
 TPythia8.cxx:194
 TPythia8.cxx:195
 TPythia8.cxx:196
 TPythia8.cxx:197
 TPythia8.cxx:198
 TPythia8.cxx:199
 TPythia8.cxx:200
 TPythia8.cxx:201
 TPythia8.cxx:202
 TPythia8.cxx:203
 TPythia8.cxx:204
 TPythia8.cxx:205
 TPythia8.cxx:206
 TPythia8.cxx:207
 TPythia8.cxx:208
 TPythia8.cxx:209
 TPythia8.cxx:210
 TPythia8.cxx:211
 TPythia8.cxx:212
 TPythia8.cxx:213
 TPythia8.cxx:214
 TPythia8.cxx:215
 TPythia8.cxx:216
 TPythia8.cxx:217
 TPythia8.cxx:218
 TPythia8.cxx:219
 TPythia8.cxx:220
 TPythia8.cxx:221
 TPythia8.cxx:222
 TPythia8.cxx:223
 TPythia8.cxx:224
 TPythia8.cxx:225
 TPythia8.cxx:226
 TPythia8.cxx:227
 TPythia8.cxx:228
 TPythia8.cxx:229
 TPythia8.cxx:230
 TPythia8.cxx:231
 TPythia8.cxx:232
 TPythia8.cxx:233
 TPythia8.cxx:234
 TPythia8.cxx:235
 TPythia8.cxx:236
 TPythia8.cxx:237
 TPythia8.cxx:238
 TPythia8.cxx:239
 TPythia8.cxx:240
 TPythia8.cxx:241
 TPythia8.cxx:242
 TPythia8.cxx:243
 TPythia8.cxx:244
 TPythia8.cxx:245
 TPythia8.cxx:246
 TPythia8.cxx:247
 TPythia8.cxx:248
 TPythia8.cxx:249
 TPythia8.cxx:250
 TPythia8.cxx:251
 TPythia8.cxx:252
 TPythia8.cxx:253
 TPythia8.cxx:254
 TPythia8.cxx:255
 TPythia8.cxx:256
 TPythia8.cxx:257
 TPythia8.cxx:258
 TPythia8.cxx:259
 TPythia8.cxx:260
 TPythia8.cxx:261
 TPythia8.cxx:262
 TPythia8.cxx:263
 TPythia8.cxx:264
 TPythia8.cxx:265
 TPythia8.cxx:266
 TPythia8.cxx:267
 TPythia8.cxx:268
 TPythia8.cxx:269
 TPythia8.cxx:270
 TPythia8.cxx:271
 TPythia8.cxx:272
 TPythia8.cxx:273
 TPythia8.cxx:274
 TPythia8.cxx:275
 TPythia8.cxx:276
 TPythia8.cxx:277
 TPythia8.cxx:278
 TPythia8.cxx:279
 TPythia8.cxx:280
 TPythia8.cxx:281
 TPythia8.cxx:282
 TPythia8.cxx:283
 TPythia8.cxx:284
 TPythia8.cxx:285
 TPythia8.cxx:286
 TPythia8.cxx:287
 TPythia8.cxx:288
 TPythia8.cxx:289
 TPythia8.cxx:290
 TPythia8.cxx:291
 TPythia8.cxx:292
 TPythia8.cxx:293
 TPythia8.cxx:294
 TPythia8.cxx:295
 TPythia8.cxx:296
 TPythia8.cxx:297
 TPythia8.cxx:298
 TPythia8.cxx:299
 TPythia8.cxx:300
 TPythia8.cxx:301
 TPythia8.cxx:302
 TPythia8.cxx:303
 TPythia8.cxx:304
 TPythia8.cxx:305
 TPythia8.cxx:306
 TPythia8.cxx:307
 TPythia8.cxx:308
 TPythia8.cxx:309
 TPythia8.cxx:310
 TPythia8.cxx:311
 TPythia8.cxx:312
 TPythia8.cxx:313
 TPythia8.cxx:314
 TPythia8.cxx:315
 TPythia8.cxx:316
 TPythia8.cxx:317
 TPythia8.cxx:318
 TPythia8.cxx:319
 TPythia8.cxx:320
 TPythia8.cxx:321
 TPythia8.cxx:322
 TPythia8.cxx:323
 TPythia8.cxx:324
 TPythia8.cxx:325
 TPythia8.cxx:326
 TPythia8.cxx:327
 TPythia8.cxx:328
 TPythia8.cxx:329
 TPythia8.cxx:330
 TPythia8.cxx:331
 TPythia8.cxx:332
 TPythia8.cxx:333
 TPythia8.cxx:334
 TPythia8.cxx:335
 TPythia8.cxx:336
 TPythia8.cxx:337
 TPythia8.cxx:338
 TPythia8.cxx:339
 TPythia8.cxx:340
 TPythia8.cxx:341
 TPythia8.cxx:342
 TPythia8.cxx:343
 TPythia8.cxx:344
 TPythia8.cxx:345
 TPythia8.cxx:346
 TPythia8.cxx:347
 TPythia8.cxx:348
 TPythia8.cxx:349
 TPythia8.cxx:350