Newer
Older
TB_Chris / TbAlignment / src / .svn / text-base / TbAlignment.cpp.svn-base
  1. #include <fstream>
  2.  
  3. // Gaudi
  4. #include "GaudiKernel/IEventProcessor.h"
  5.  
  6. // Tb/TbKernel
  7. #include "TbKernel/TbModule.h"
  8. #include "TbKernel/TbAlignmentTrack.h"
  9.  
  10. // Local
  11. #include "TbAlignment.h"
  12.  
  13. DECLARE_ALGORITHM_FACTORY(TbAlignment)
  14.  
  15. //=============================================================================
  16. // Standard constructor, initializes variables
  17. //=============================================================================
  18. TbAlignment::TbAlignment(const std::string &name, ISvcLocator *pSvcLocator)
  19. : TbAlgorithm(name, pSvcLocator), m_lastTrackPrint(0), m_tracks() {
  20.  
  21. declareProperty("OutputAlignmentFile", m_outputFile = "Alignment_out.dat");
  22. declareProperty("AlignmentTechniques", m_alignmentSequence);
  23. declareProperty("NTracks", m_nTracks = 0);
  24. }
  25.  
  26. //=============================================================================
  27. // Destructor
  28. //=============================================================================
  29. TbAlignment::~TbAlignment() {
  30.  
  31. // Delete the alignment tracks.
  32. for (auto track : m_tracks) {
  33. if (track) delete track;
  34. }
  35. m_tracks.clear();
  36. }
  37.  
  38. //=============================================================================
  39. // Initialization
  40. //=============================================================================
  41. StatusCode TbAlignment::initialize() {
  42.  
  43. // Initialise the base class.
  44. StatusCode sc = TbAlgorithm::initialize();
  45. if (sc.isFailure()) return sc;
  46.  
  47. if (m_alignmentSequence.empty()) {
  48. return Error("Alignment sequence is empty.");
  49. }
  50. unsigned int scounter = 0;
  51. for (const auto &toolName : m_alignmentSequence) {
  52. info() << "Adding tool " << toolName << endmsg;
  53. const std::string name = "s" + std::to_string(scounter++);
  54. TbAlignmentBase* newTool = tool<TbAlignmentBase>(toolName, name, this);
  55. if (!newTool) return Error("Cannot retrieve tool " + toolName);
  56. m_toolChain.push_back(newTool);
  57. }
  58. m_toolIterator = m_toolChain.begin();
  59. return StatusCode::SUCCESS;
  60. }
  61.  
  62. //=============================================================================
  63. // Main execution
  64. //=============================================================================
  65. StatusCode TbAlignment::execute() {
  66.  
  67. auto currentTool = *m_toolIterator;
  68. StatusCode sc = currentTool->execute(m_tracks);
  69. if (sc.isFailure()) return sc;
  70.  
  71. const unsigned int nTracks = m_tracks.size();
  72. if (nTracks - (nTracks % 1000) != m_lastTrackPrint) {
  73. m_lastTrackPrint = nTracks - (nTracks % 1000);
  74. info() << "Collected " << m_lastTrackPrint << " alignment tracks" << endmsg;
  75. }
  76. // If requested, stop processing events after a given number of tracks.
  77. if (m_nTracks > 0 && nTracks > m_nTracks) {
  78. currentTool->plotResiduals(m_tracks, "Before");
  79. currentTool->align(m_tracks);
  80. currentTool->plotResiduals(m_tracks, "After");
  81. ++m_toolIterator;
  82. if (m_toolIterator == m_toolChain.end()) {
  83. SmartIF<IEventProcessor> app(serviceLocator()->service("ApplicationMgr"));
  84. if (app) app->stopRun();
  85. return StatusCode::SUCCESS;
  86. }
  87. currentTool = *m_toolIterator;
  88. // Reset track cache
  89. if (currentTool->clearTracks()) {
  90. for (auto it = m_tracks.begin(), end = m_tracks.end(); it != end; ++it) {
  91. if (*it) delete *it;
  92. }
  93. m_tracks.clear();
  94. m_lastTrackPrint = 0;
  95. }
  96. }
  97. return StatusCode::SUCCESS;
  98. }
  99.  
  100. //=============================================================================
  101. // Finalization
  102. //=============================================================================
  103. StatusCode TbAlignment::finalize() {
  104.  
  105. if (m_toolIterator != m_toolChain.end()) {
  106. info() << "Event loop terminating before chain completion" << endmsg;
  107. (*m_toolIterator)->align(m_tracks);
  108. }
  109. writeAlignmentFile();
  110. // Finalise the base class.
  111. return TbAlgorithm::finalize();
  112. }
  113.  
  114. //=============================================================================
  115. // Save the alignment constants to file
  116. //=============================================================================
  117. bool TbAlignment::writeAlignmentFile() {
  118. // Write results to output file
  119. info() << "Writing alignment output file to " << m_outputFile << endmsg;
  120. std::ofstream txtfile(m_outputFile.c_str());
  121. if (!txtfile) {
  122. error() << "Cannot create file " << m_outputFile << endmsg;
  123. return false;
  124. }
  125. auto modules = geomSvc()->modules();
  126. for (auto im = modules.begin(), end = modules.end(); im != end; ++im) {
  127. txtfile << (*im)->id() << " " << std::setw(12) << std::setprecision(10)
  128. << (*im)->x() << " " << std::setw(12) << std::setprecision(10)
  129. << (*im)->y() << " " << std::setw(12) << std::setprecision(10)
  130. << (*im)->z() << " " << std::setw(12) << std::setprecision(10)
  131. << (*im)->rotX() << " " << std::setw(12) << std::setprecision(10)
  132. << (*im)->rotY() << " " << std::setw(12) << std::setprecision(10)
  133. << (*im)->rotZ() << std::endl;
  134. }
  135. txtfile.close();
  136. return true;
  137. }