Newer
Older
TB_Chris / TbIO / src / .svn / text-base / TbCombatBuilder.cpp.svn-base
  1. #include <algorithm>
  2.  
  3. // Boost
  4. #include <boost/filesystem.hpp>
  5.  
  6. // Gaudi
  7. #include "GaudiKernel/IEventProcessor.h"
  8.  
  9. // Tb/TbKernel
  10. #include "TbKernel/TbFunctors.h"
  11.  
  12. // Local
  13. #include "TbCombatBuilder.h"
  14.  
  15. DECLARE_ALGORITHM_FACTORY(TbCombatBuilder)
  16.  
  17. namespace fs = boost::filesystem;
  18.  
  19. //=============================================================================
  20. // Standard constructor
  21. //=============================================================================
  22. TbCombatBuilder::TbCombatBuilder(const std::string& name,
  23. ISvcLocator* pSvcLocator)
  24. : TbAlgorithm(name, pSvcLocator),
  25. m_nPixels(256),
  26. m_nEvents(0),
  27. m_neof(true) {
  28.  
  29. declareProperty("CombatInputFile0", m_fileName0);
  30. declareProperty("CombatInputFile1", m_fileName1 = "DefaultFileName1.dat");
  31. declareProperty("HitLocation", m_hitLocation = LHCb::TbHitLocation::Default);
  32. declareProperty("MinPlanesWithHits", m_nMinPlanesWithHits = 1); // TODO
  33. declareProperty("SkipEvents", m_skipEvents = 0); // TODO
  34. declareProperty("PrintFreq", m_printFreq = 100);
  35. declareProperty("ReadoutFormat", m_readoutFormat);
  36. declareProperty("FakeTime", m_fakeTime = 1);
  37. declareProperty("NumberArms", m_nArms);
  38. }
  39.  
  40. //=============================================================================
  41. // Destructor
  42. //=============================================================================
  43. TbCombatBuilder::~TbCombatBuilder() {}
  44.  
  45. //=============================================================================
  46. // Initialisation
  47. //=============================================================================
  48. StatusCode TbCombatBuilder::initialize() {
  49.  
  50. // Initialise the base class.
  51. StatusCode sc = TbAlgorithm::initialize();
  52. if (sc.isFailure()) return sc;
  53.  
  54. // Prepare the hit containers.
  55. m_hits.resize(m_nPlanes, NULL);
  56.  
  57. // Open the hit files.
  58. m_dataStream0.open(m_fileName0.c_str());
  59. if (!m_dataStream0.is_open()) {
  60. error() << "Cannot open file name: " << m_fileName0 << endmsg;
  61. return StatusCode::FAILURE;
  62. }
  63.  
  64. if (m_nArms == 2 && m_readoutFormat=="Pixelman") {
  65. m_dataStream1.open(m_fileName1.c_str());
  66. if (!m_dataStream1.is_open()) {
  67. error() << "Cannot open file name: " << m_fileName1 << endmsg;
  68. return StatusCode::FAILURE;
  69. }
  70. }
  71.  
  72. return StatusCode::SUCCESS;
  73. }
  74.  
  75. //=============================================================================
  76. // Main execution
  77. //=============================================================================
  78. StatusCode TbCombatBuilder::execute() {
  79. // Printing.
  80. if (m_nEvents % m_printFreq == 0)
  81. info() << "Loading event: " << m_nEvents << endmsg;
  82.  
  83. // Create containers for hits.
  84. for (unsigned int i = 0; i < m_nPlanes; ++i) {
  85. const std::string ext = std::to_string(i);
  86. m_hits[i] = new LHCb::TbHits();
  87. put(m_hits[i], m_hitLocation + ext);
  88. }
  89.  
  90.  
  91. if(m_readoutFormat=="Pixelman"){
  92. if (m_nArms == 1) {
  93. fillEventFromPixelmanFile(0);
  94. } else if (m_nArms == 2) {
  95. fillEventFromPixelmanFile(0);
  96. fillEventFromPixelmanFile(1);
  97. }
  98. }
  99. else if(m_readoutFormat=="RelaxD"){
  100. fillEventFromRelaxDFile();
  101. }
  102.  
  103. // Check whether there are any events left.
  104. if (!m_neof) {
  105. // Terminate.
  106. SmartIF<IEventProcessor> app(serviceLocator()->service("ApplicationMgr"));
  107. if (app) app->stopRun();
  108. } else {
  109. // Ordering.
  110. for (unsigned int i = 0; i < m_nPlanes; i++) {
  111. std::sort(m_hits[i]->begin(), m_hits[i]->end(),
  112. TbFunctors::GreaterByToT<const LHCb::TbHit*>());
  113. }
  114. m_nEvents++;
  115. }
  116.  
  117. // Prepare for the next event.
  118. return StatusCode::SUCCESS;
  119. }
  120.  
  121. //=============================================================================
  122. // Finalisation
  123. //=============================================================================
  124. StatusCode TbCombatBuilder::finalize() { return TbAlgorithm::finalize(); }
  125.  
  126. //=============================================================================
  127. // Event filling handling Pixelman files
  128. //=============================================================================
  129. void TbCombatBuilder::fillEventFromPixelmanFile(const int armID) {
  130. // Check we havent reached the next event.
  131. bool sameEvent = true;
  132. while (sameEvent) {
  133. std::string line;
  134. if (armID == 0)
  135. m_neof = std::getline(m_dataStream0, line);
  136. else
  137. m_neof = std::getline(m_dataStream1, line);
  138. if (!m_neof) break;
  139. if (line.substr(0, 1) == "#")
  140. sameEvent = false;
  141. else
  142. fillHit(line, armID);
  143. }
  144. }
  145.  
  146. //=============================================================================
  147. // Event filling handling RelaxD files
  148. //=============================================================================
  149. void TbCombatBuilder::fillEventFromRelaxDFile() {
  150.  
  151. //For the RelaxD files, in one event the first series of hits come from the upper arm and the second from the downstream arm.
  152. for(int iArm=0;iArm<m_nArms;iArm++){
  153. // Check we havent reached the next event.
  154. bool sameEvent = true;
  155. bool frameStart = false;
  156. while (sameEvent) {
  157. std::string line;
  158. m_neof = std::getline(m_dataStream0, line);
  159. if (!m_neof) break;
  160. if (line.substr(0,1) != "#"){
  161. frameStart= true;
  162. fillHit(line, iArm);
  163. }
  164. if (line.substr(0, 1) == "#"&& frameStart==true)
  165. sameEvent = false;
  166. }
  167. }
  168. }
  169. //=============================================================================
  170. // Event filling
  171. //=============================================================================
  172. void TbCombatBuilder::fillHit(std::string line, int armID) {
  173. std::stringstream lineStream;
  174. lineStream << line;
  175. int rowTemp, colTemp, ToT;
  176. lineStream >> colTemp >> rowTemp >> ToT;
  177.  
  178. int plane;
  179. if (colTemp > 255) {
  180. if (rowTemp > 255)
  181. plane = 1;
  182. else
  183. plane = 3;
  184. } else {
  185. if (rowTemp > 255)
  186. plane = 0;
  187. else
  188. plane = 2;
  189. }
  190.  
  191. // Arm modification.
  192. if (armID == 1) plane += 4;
  193.  
  194. // Make the hit.
  195. LHCb::TbHit* hit = new LHCb::TbHit();
  196. hit->setRow(rowTemp % m_nPixels);
  197. hit->setCol(colTemp % m_nPixels);
  198. hit->setToT(ToT);
  199. hit->setPixelAddress(hit->row() + hit->col() * m_nPixels);
  200. hit->setTime(m_fakeTime);
  201. hit->setHtime(timingSvc()->globalToLocal(hit->time()));
  202.  
  203. m_hits[plane]->insert(hit);
  204. }
  205.  
  206. //=============================================================================
  207. // End
  208. //=============================================================================