Newer
Older
TB_Chris / TbAlgorithms / src / .svn / text-base / TbVisualiserOutput.cpp.svn-base
  1. #include <fstream>
  2. #include <algorithm>
  3.  
  4. // Gaudi
  5. #include "GaudiKernel/PhysicalConstants.h"
  6.  
  7. // Tb/TbEvent
  8. #include "Event/TbCluster.h"
  9.  
  10. // Tb/TbKernel
  11. #include "TbKernel/TbConstants.h"
  12. #include "TbKernel/TbFunctors.h"
  13.  
  14. // Local
  15. #include "TbVisualiserOutput.h"
  16.  
  17. DECLARE_ALGORITHM_FACTORY(TbVisualiserOutput)
  18.  
  19. //=============================================================================
  20. // Standard constructor
  21. //=============================================================================
  22. TbVisualiserOutput::TbVisualiserOutput(const std::string& name,
  23. ISvcLocator* pSvcLocator)
  24. : TbAlgorithm(name, pSvcLocator), m_event(0) {
  25. declareProperty("ViewerEvent", m_viewerEvent = 7);
  26. declareProperty("ClusterLocation",
  27. m_clusterLocation = LHCb::TbClusterLocation::Default);
  28. declareProperty("TrackLocation",
  29. m_trackLocation = LHCb::TbTrackLocation::Default);
  30. }
  31.  
  32. //=============================================================================
  33. // Destructor
  34. //=============================================================================
  35. TbVisualiserOutput::~TbVisualiserOutput() {}
  36.  
  37. //=============================================================================
  38. // Initialization
  39. //=============================================================================
  40. StatusCode TbVisualiserOutput::initialize() {
  41.  
  42. StatusCode sc = TbAlgorithm::initialize();
  43. if (sc.isFailure()) return sc;
  44. m_clusterFinder =
  45. tool<ITbClusterFinder>("TbClusterFinder", "ClusterFinder", this);
  46.  
  47. return StatusCode::SUCCESS;
  48. }
  49.  
  50. //=============================================================================
  51. // Main execution
  52. //=============================================================================
  53. StatusCode TbVisualiserOutput::execute() {
  54. m_tracks = getIfExists<LHCb::TbTracks>(m_trackLocation);
  55. if (!m_tracks) {
  56. return Error("No tracks in " + m_trackLocation);
  57. }
  58. if (m_event == m_viewerEvent) {
  59.  
  60. for (unsigned int i = 0; i < m_nPlanes; ++i) {
  61. const std::string clusterLocation = m_clusterLocation + std::to_string(i);
  62. LHCb::TbClusters* clusters =
  63. getIfExists<LHCb::TbClusters>(clusterLocation);
  64. m_clusterFinder->setClusters(clusters, i);
  65. }
  66. outputViewerData();
  67. }
  68.  
  69. m_event++;
  70. return StatusCode::SUCCESS;
  71. }
  72.  
  73. //=============================================================================
  74. // Viewer output
  75. //=============================================================================
  76. void TbVisualiserOutput::outputViewerData() {
  77. std::ofstream myfile;
  78. myfile.open("KeplerViewerData.dat", std::ofstream::app);
  79. myfile << "# Output\n";
  80. // First output the chips.
  81. for (unsigned int i = 0; i < m_nPlanes; i++) {
  82. myfile << "Chip ";
  83. Gaudi::XYZPoint posn1(0., 14.08, 0.);
  84. Gaudi::XYZPoint posn = geomSvc()->localToGlobal(posn1, i);
  85. myfile << posn.x() << " " << posn.y() << " " << posn.z() << " ";
  86.  
  87. Gaudi::XYZPoint posn2(14.08, 14.08, 0.);
  88. posn = geomSvc()->localToGlobal(posn2, i);
  89. myfile << posn.x() << " " << posn.y() << " " << posn.z() << " ";
  90.  
  91. Gaudi::XYZPoint posn3(14.08, 0., 0.);
  92. posn = geomSvc()->localToGlobal(posn3, i);
  93. myfile << posn.x() << " " << posn.y() << " " << posn.z() << " ";
  94.  
  95. Gaudi::XYZPoint posn4(0., 0., 0.);
  96. posn = geomSvc()->localToGlobal(posn4, i);
  97. myfile << posn.x() << " " << posn.y() << " " << posn.z() << " ";
  98.  
  99. myfile << "\n";
  100. }
  101. // Tracks.
  102. for (const LHCb::TbTrack* track : *m_tracks) {
  103. myfile << "Track ";
  104. myfile << track->firstState().tx() << " " << track->firstState().x() << " "
  105. << track->firstState().ty() << " " << track->firstState().y() << " "
  106. << track->htime() << "\n";
  107. }
  108. // Clusters.
  109. for (unsigned int i = 0; i < m_nPlanes; i++) {
  110. auto ic = m_clusterFinder->first(i);
  111. const auto end = m_clusterFinder->end(i);
  112. for (; ic != end; ++ic) {
  113. const int tag = (*ic)->associated();
  114. myfile << "Cluster ";
  115. myfile << (*ic)->x() << " " << (*ic)->y() << " " << (*ic)->z() << " "
  116. << (*ic)->htime() << " " << tag << " \n";
  117. // Its hits.
  118. for (const auto hit : (*ic)->hits()) {
  119. myfile << "Pixel ";
  120. double xLocal = 0.;
  121. double yLocal = 0.;
  122. geomSvc()->pixelToPoint(hit->scol(), hit->row(), i, xLocal, yLocal);
  123. Gaudi::XYZPoint pLocal(xLocal - 0.5 * Tb::PixelPitch,
  124. yLocal - 0.5 * Tb::PixelPitch, 0.);
  125. Gaudi::XYZPoint posn = geomSvc()->localToGlobal(pLocal, i);
  126. myfile << posn.x() << " " << posn.y() << " " << posn.z() << " ";
  127. Gaudi::XYZPoint posn2(pLocal.x() + Tb::PixelPitch, pLocal.y(), 0.);
  128. posn = geomSvc()->localToGlobal(posn2, i);
  129. myfile << posn.x() << " " << posn.y() << " " << posn.z() << " ";
  130. Gaudi::XYZPoint posn3(pLocal.x() + Tb::PixelPitch,
  131. pLocal.y() + Tb::PixelPitch, 0.);
  132. posn = geomSvc()->localToGlobal(posn3, i);
  133. myfile << posn.x() << " " << posn.y() << " " << posn.z() << " ";
  134. Gaudi::XYZPoint posn4(pLocal.x(), pLocal.y() + Tb::PixelPitch, 0.);
  135. posn = geomSvc()->localToGlobal(posn4, i);
  136. myfile << posn.x() << " " << posn.y() << " " << posn.z() << " ";
  137. myfile << hit->htime() << " " << hit->ToT() << "\n";
  138. }
  139. }
  140. }
  141. myfile.close();
  142. }