FairPlutoGenerator.cxx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // -------------------------------------------------------------------------
  2. // ----- FairPlutoGenerator source file -----
  3. // ----- Created 13/07/04 by V. Friese / D.Bertini -----
  4. // -------------------------------------------------------------------------
  5. #include "FairPlutoGenerator.h"
  6. #include "FairPrimaryGenerator.h"
  7. #include "TClonesArray.h"
  8. #include "TDatabasePDG.h"
  9. #include "TFile.h"
  10. #include "TLorentzVector.h"
  11. #include "TTree.h"
  12. #include "TVector3.h"
  13. #include "PParticle.h"
  14. #include <iostream>
  15. // ----- Default constructor ------------------------------------------
  16. FairPlutoGenerator::FairPlutoGenerator()
  17. :FairGenerator(),
  18. iEvent(0),
  19. fFileName(NULL),
  20. fInputFile(NULL),
  21. fInputTree(NULL),
  22. fParticles(NULL)
  23. {
  24. /*
  25. iEvent = 0;
  26. fInputFile = NULL;
  27. fInputTree = NULL;
  28. */
  29. }
  30. // ------------------------------------------------------------------------
  31. // ----- Standard constructor -----------------------------------------
  32. FairPlutoGenerator::FairPlutoGenerator(const Char_t* fileName)
  33. :FairGenerator(),
  34. iEvent(0),
  35. fFileName(fileName),
  36. fInputFile(new TFile(fileName)),
  37. fInputTree(NULL),
  38. fParticles(new TClonesArray("PParticle",100))
  39. {
  40. /*
  41. iEvent = 0;
  42. fFileName = fileName;
  43. fInputFile = new TFile(fFileName);
  44. */
  45. fInputTree = (TTree*) fInputFile->Get("data");
  46. // fParticles = new TClonesArray("PParticle",100);
  47. fInputTree->SetBranchAddress("Particles", &fParticles);
  48. }
  49. // ------------------------------------------------------------------------
  50. // ----- Destructor ---------------------------------------------------
  51. FairPlutoGenerator::~FairPlutoGenerator()
  52. {
  53. CloseInput();
  54. }
  55. // ------------------------------------------------------------------------
  56. // ----- Public method ReadEvent --------------------------------------
  57. Bool_t FairPlutoGenerator::ReadEvent(FairPrimaryGenerator* primGen)
  58. {
  59. // Check for input file
  60. if ( ! fInputFile ) {
  61. cout << "-E FairPlutoGenerator: Input file nor open!" << endl;
  62. return kFALSE;
  63. }
  64. // Check for number of events in input file
  65. if ( iEvent > fInputTree->GetEntries() ) {
  66. cout << "-E FairPlutoGenerator: No more events in input file!" << endl;
  67. CloseInput();
  68. return kFALSE;
  69. }
  70. TFile* g=gFile;
  71. fInputFile->cd();
  72. fInputTree->GetEntry(iEvent++);
  73. g->cd();
  74. // Get PDG database
  75. TDatabasePDG* dataBase = TDatabasePDG::Instance();
  76. // Get number of particle in TClonesrray
  77. Int_t nParts = fParticles->GetEntriesFast();
  78. // Loop over particles in TClonesArray
  79. for (Int_t iPart=0; iPart < nParts; iPart++) {
  80. PParticle* part = (PParticle*) fParticles->At(iPart);
  81. Int_t pdgType = dataBase->ConvertGeant3ToPdg( part->ID() );
  82. // Check if particle type is known to database
  83. if ( ! pdgType ) {
  84. cout << "-W FairPlutoGenerator: Unknown type " << part->ID()
  85. << ", skipping particle." << endl;
  86. continue;
  87. }
  88. TLorentzVector mom = part->Vect4();
  89. Double_t px = mom.Px();
  90. Double_t py = mom.Py();
  91. Double_t pz = mom.Pz();
  92. TVector3 vertex = part->getVertex();
  93. Double_t vx = vertex.x();
  94. Double_t vy = vertex.y();
  95. Double_t vz = vertex.z();
  96. // Give track to PrimaryGenerator
  97. primGen->AddTrack(pdgType, px, py, pz, vx, vy, vz);
  98. } // Loop over particle in event
  99. return kTRUE;
  100. }
  101. // ------------------------------------------------------------------------
  102. // ----- Private method CloseInput ------------------------------------
  103. void FairPlutoGenerator::CloseInput()
  104. {
  105. if ( fInputFile ) {
  106. cout << "-I FairPlutoGenerator: Closing input file " << fFileName
  107. << endl;
  108. fInputFile->Close();
  109. delete fInputFile;
  110. }
  111. fInputFile = NULL;
  112. }
  113. // ------------------------------------------------------------------------
  114. ClassImp(FairPlutoGenerator)