Newer
Older
TB_Chris / TbUT / src / .svn / text-base / TbUTPedestalSubtractorAlgorithm.cpp.svn-base
  1. /*
  2. * TbUTPedestalSubtractorAlgorithm.cpp
  3. *
  4. * Created on: Oct 14, 2014
  5. * Author: ADendek
  6. */
  7.  
  8. #include "TbUTPedestalSubtractorAlgorithm.h"
  9. #include "TbUTDataLocations.h"
  10.  
  11.  
  12. using namespace TbUT;
  13.  
  14. DECLARE_NAMESPACE_ALGORITHM_FACTORY(TbUT,PedestalSubtractorAlgorithm)
  15.  
  16.  
  17. PedestalSubtractorAlgorithm::PedestalSubtractorAlgorithm(const std::string& name, ISvcLocator* pSvcLocator):
  18. GaudiAlgorithm(name, pSvcLocator),
  19. m_isStandalone(true),
  20. m_data(),
  21. m_inputDataLocation(),
  22. m_outputDataLocation(),
  23. m_pedestalInputLocation(),
  24. m_pedestalOutputLocation(),
  25. m_channelMaskInputLocation(),
  26. m_followingOption(),
  27. m_event(0),
  28. m_treningEventNumber(0),
  29. m_skippEvent(0),
  30. m_channelMaskFileValidator(m_channelMaskInputLocation),
  31. m_channelMaskProvider(m_channelMaskFileValidator),
  32. m_pedestal(),
  33. m_pedestalFileValidator(m_pedestalInputLocation),
  34. m_followingFactory(m_channelMaskProvider, m_pedestal,m_pedestalFileValidator ,m_pedestalInputLocation),
  35. m_pedestalFollowingPtr(),
  36. m_pedestalSubtractor(m_pedestal, m_channelMaskProvider)
  37. {
  38. declareProperty("SkippEventNumber", m_skippEvent=0);
  39. declareProperty("InputDataLocation", m_inputDataLocation=TbUT::DataLocations::RawTES);
  40. declareProperty("OutputDataLocation", m_outputDataLocation=TbUT::DataLocations::PedestalTES);
  41. declareProperty("FollowingOption",m_followingOption=TbUT::FollowingOptions::Calculator);
  42. declareProperty("treningEntry",m_treningEventNumber=1024);
  43. declareProperty("ChannelMaskInputLocation", m_channelMaskInputLocation=TbUT::DataLocations::MaskLocation);
  44. declareProperty("PedestalInputFile", m_pedestalInputLocation=TbUT::DataLocations::PedestalLocation);
  45. declareProperty("PedestalOutputFile", m_pedestalOutputLocation=TbUT::DataLocations::PedestalLocation);
  46. declareProperty("standalone",m_isStandalone=true );
  47. }
  48.  
  49. StatusCode PedestalSubtractorAlgorithm::initialize()
  50. {
  51. if(StatusCode::SUCCESS !=initializeBase()) return StatusCode::FAILURE;
  52. if(StatusCode::SUCCESS !=buildFollowing()) return StatusCode::FAILURE;
  53. if(StatusCode::SUCCESS !=retriveMasksFromFile()) return StatusCode::FAILURE;
  54.  
  55. info()<<"Initialized successfully!"<<endmsg;
  56. return StatusCode::SUCCESS;
  57. }
  58.  
  59. StatusCode PedestalSubtractorAlgorithm::execute()
  60. {
  61. if(StatusCode::SUCCESS != getData()) return StatusCode::FAILURE;
  62. m_outputDataContainer=new RawDataContainer<>();
  63.  
  64. if(m_dataContainer->isEmpty()){
  65. info()<<"ped suba put empty"<<endmsg;
  66. put(m_outputDataContainer,m_outputDataLocation);
  67. return StatusCode::SUCCESS;
  68. }
  69.  
  70. for(const auto& rawDataIt: m_dataContainer->getData()){
  71. m_data=new RawData<>(rawDataIt);
  72. RunPhase l_runPhase=getRunPhase();
  73. switch (l_runPhase)
  74. {
  75. case SKIPP:
  76. skippEvent();
  77. case TREANING:
  78. processTreaning();
  79. default:
  80. subtractPedestals();
  81. }
  82. delete m_data;
  83. }
  84. put(m_outputDataContainer,m_outputDataLocation);
  85. return StatusCode::SUCCESS;
  86. }
  87.  
  88. StatusCode PedestalSubtractorAlgorithm::finalize()
  89. {
  90. savePedestalsToFile();
  91. return GaudiAlgorithm::finalize();
  92. }
  93.  
  94. StatusCode PedestalSubtractorAlgorithm::initializeBase()
  95. {
  96. return GaudiAlgorithm::initialize();
  97. }
  98.  
  99. StatusCode PedestalSubtractorAlgorithm::buildFollowing()
  100. try{
  101. m_pedestalFollowingPtr.reset(m_followingFactory.createPedestalFollowing(m_followingOption));
  102. return StatusCode::SUCCESS;
  103. }catch(PedestalFollowingFactory::NoSuchState &exception)
  104. {
  105. error()<<"Invalid Following Option: "<<exception.what()<<endmsg;
  106. return StatusCode::FAILURE;
  107. }
  108.  
  109.  
  110. StatusCode PedestalSubtractorAlgorithm::retriveMasksFromFile()
  111. try{
  112. m_channelMaskProvider.getMaskFromFile(m_channelMaskInputLocation);
  113. return StatusCode::SUCCESS;
  114. }catch(ChannelMaskProvider::InputFileError &exception)
  115. {
  116. error()<<"Channel Mask File Input Error: "<<m_channelMaskInputLocation<<endmsg;
  117. return StatusCode::FAILURE;
  118. }
  119.  
  120.  
  121. StatusCode PedestalSubtractorAlgorithm::getData()
  122. {
  123. m_dataContainer=getIfExists<RawDataContainer<> >(m_inputDataLocation);
  124. if(!m_dataContainer){
  125. error()<< " ==> There is no input data: "<< m_inputDataLocation <<endmsg;
  126. return StatusCode::FAILURE;
  127. }
  128. return StatusCode::SUCCESS;
  129. }
  130.  
  131. PedestalSubtractorAlgorithm::RunPhase PedestalSubtractorAlgorithm::getRunPhase()
  132. {
  133. if(m_event<m_skippEvent)
  134. return SKIPP;
  135. else if (m_event<m_skippEvent+m_treningEventNumber)
  136. return TREANING;
  137. else
  138. return SUBTRACTION;
  139. }
  140.  
  141.  
  142. void PedestalSubtractorAlgorithm::skippEvent()
  143. {
  144. m_event++;
  145. }
  146.  
  147. void PedestalSubtractorAlgorithm::processTreaning()
  148. {
  149. m_pedestalFollowingPtr->processEvent(m_data);
  150. m_event++;
  151. //if( m_followingOption != TbUT::FollowingOptions::Calculator) subtractPedestals();
  152. }
  153.  
  154. void PedestalSubtractorAlgorithm::subtractPedestals()
  155. {
  156. processAndSaveDataToTES();
  157. m_event++;
  158. }
  159.  
  160. void PedestalSubtractorAlgorithm::processAndSaveDataToTES()
  161. {
  162. RawData<> *afterPedestal=new RawData<>();
  163. m_pedestalSubtractor.processEvent(m_data,&afterPedestal);
  164. m_outputDataContainer->addData(*afterPedestal);
  165. }
  166.  
  167.  
  168. StatusCode PedestalSubtractorAlgorithm::savePedestalsToFile()
  169. try{
  170. info()<<"Save Pedestal to File"<<endmsg;
  171. m_pedestalFollowingPtr->savePedestalToFile(m_pedestalOutputLocation);
  172. return StatusCode::SUCCESS;
  173. }
  174. catch(IPedestalFollowing::PedestalCalculatorError &er)
  175. {
  176. error()<<er.what() <<endmsg;
  177. return StatusCode::FAILURE;
  178. }