00001 #include "EsFrontEndAlg.h"
00002
00003 #include "Event/SimHeader.h"
00004 #include "Event/ElecHeader.h"
00005 #include "Event/ElecFeeCrate.h"
00006 #include "Event/ElecFecCrate.h"
00007
00008 #include "DataSvc/IRunDataSvc.h"
00009 #include "Event/RunData.h"
00010
00011 #include "ElecSim/IEsPulseTool.h"
00012 #include "ElecSim/IEsFrontEndTool.h"
00013
00014 #include "CLHEP/Units/SystemOfUnits.h"
00015
00016 EsFrontEndAlg::EsFrontEndAlg(const std::string& name, ISvcLocator* pSvcLocator)
00017 : DybAlgorithm<DayaBay::ElecHeader>(name,pSvcLocator)
00018 , m_pmtTool(0)
00019 , m_rpcTool(0)
00020 , m_feeTool(0)
00021 , m_fecTool(0)
00022 {
00023 declareProperty("SimLocation",m_simLocation=DayaBay::SimHeaderLocation::Default,
00024 "Location in the TES where the input SimHeader is to be found.");
00025 declareProperty("Detectors",m_detectorNames,"List of active detectors");
00026 declareProperty("PmtTool",m_pmtToolName="EsPmtEffectPulseTool",
00027 "Name of the PMT simulation tool");
00028 declareProperty("RpcTool",m_rpcToolName="EsIdealPulseTool",
00029 "Name of the RPC simulation tool");
00030 declareProperty("FeeTool",m_feeToolName="EsIdealFeeTool",
00031 "Name of the PMT Front-end electronics simulation tool");
00032 declareProperty("FecTool",m_fecToolName="EsIdealFecTool",
00033 "Name of the RPC Front-end electronics simulation tool");
00034 declareProperty("MaxSimulationTime",
00035 m_maxSimulationTime=50*CLHEP::microsecond,
00036 "Maximum time window length for electronics simulation");
00037
00038 m_detectorNames.push_back("DayaBayAD1");
00039 m_detectorNames.push_back("DayaBayAD2");
00040 m_detectorNames.push_back("DayaBayIWS");
00041 m_detectorNames.push_back("DayaBayOWS");
00042 m_detectorNames.push_back("LingAoAD1");
00043 m_detectorNames.push_back("LingAoAD2");
00044 m_detectorNames.push_back("LingAoIWS");
00045 m_detectorNames.push_back("LingAoOWS");
00046 m_detectorNames.push_back("FarAD1");
00047 m_detectorNames.push_back("FarAD2");
00048 m_detectorNames.push_back("FarAD3");
00049 m_detectorNames.push_back("FarAD4");
00050 m_detectorNames.push_back("FarIWS");
00051 m_detectorNames.push_back("FarOWS");
00052 m_detectorNames.push_back("SABAD1");
00053 m_detectorNames.push_back("SABAD2");
00054 }
00055
00056 EsFrontEndAlg::~EsFrontEndAlg()
00057 {
00058 }
00059
00060 StatusCode EsFrontEndAlg::initialize()
00061 {
00062
00063
00064 int nDetNames = m_detectorNames.size();
00065 for(int detIdx=0; detIdx<nDetNames; detIdx++){
00066 DayaBay::Detector det(m_detectorNames[detIdx]);
00067 if(det.site() == Site::kUnknown
00068 || det.detectorId() == DetectorId::kUnknown){
00069 error() << "Invalid detector name: " << m_detectorNames[detIdx] << endreq;
00070 return StatusCode::FAILURE;
00071 }
00072 m_detectors.push_back(det);
00073 }
00074
00075
00076 try {
00077 m_pmtTool = tool<IEsPulseTool>(m_pmtToolName);
00078 }
00079 catch(const GaudiException& exg) {
00080 fatal() << "Failed to get pmt tool: \"" << m_pmtToolName << "\"" << endreq;
00081 return StatusCode::FAILURE;
00082 }
00083 info () << "Added tool " << m_pmtToolName << endreq;
00084
00085
00086 if(m_pmtToolName == m_rpcToolName){
00087 m_rpcTool = m_pmtTool;
00088 }else{
00089 try {
00090 m_rpcTool = tool<IEsPulseTool>(m_rpcToolName);
00091 }
00092 catch(const GaudiException& exg) {
00093 fatal() << "Failed to get rpc tool: \"" << m_rpcToolName << "\"" << endreq;
00094 return StatusCode::FAILURE;
00095 }
00096 info () << "Added tool " << m_rpcToolName << endreq;
00097 }
00098
00099
00100 try {
00101 m_feeTool = tool<IEsFrontEndTool>(m_feeToolName);
00102 }
00103 catch(const GaudiException& exg) {
00104 fatal() << "Failed to get fee tool: \"" << m_feeToolName << "\"" << endreq;
00105 return StatusCode::FAILURE;
00106 }
00107 info () << "Added tool " << m_feeToolName << endreq;
00108
00109
00110 try {
00111 m_fecTool = tool<IEsFrontEndTool>(m_fecToolName);
00112 }
00113 catch(const GaudiException& exg) {
00114 fatal() << "Failed to get fec tool: \"" << m_fecToolName << "\"" << endreq;
00115 return StatusCode::FAILURE;
00116 }
00117 info () << "Added tool " << m_fecToolName << endreq;
00118
00119
00120 IRunDataSvc* runDataSvc = svc<IRunDataSvc>("RunDataSvc",true);
00121 if(!runDataSvc){
00122 error() << "Failed to get RunDataSvc" << endreq;
00123 return StatusCode::FAILURE;
00124 }
00125 int task = 1;
00126 Context emptyContext;
00127 ServiceMode svcMode(emptyContext, task);
00128 const DayaBay::RunData* runData = runDataSvc->runData(svcMode);
00129 if(!runData){
00130 error() << "Failed to get RunData for simulation" << endreq;
00131 return StatusCode::FAILURE;
00132 }
00133 DayaBay::RunData modifiedRunData(*runData);
00134 modifiedRunData.setDetectors(m_detectors);
00135 StatusCode sc = runDataSvc->setRunData(modifiedRunData);
00136 if( !sc.isSuccess() ){
00137 error() << "Failed to set detectors in run" << endreq;
00138 }
00139 return sc;
00140 }
00141
00142 StatusCode EsFrontEndAlg::execute()
00143 {
00144
00145
00146 DayaBay::SimHeader* simHeader = getTES<DayaBay::SimHeader>(m_simLocation);
00147
00148
00149 DayaBay::ElecHeader* elecHeader = MakeHeaderObject();
00150
00151
00152
00153 TimeStamp earliest = simHeader->earliest();
00154 TimeStamp latest = simHeader->latest();
00155
00156 earliest.Add(-DayaBay::preTimeTolerance/CLHEP::second);
00157 latest.Add(DayaBay::postTimeTolerance/CLHEP::second);
00158
00159 elecHeader->setContext(simHeader->context());
00160 elecHeader->setEarliest(earliest);
00161 elecHeader->setLatest(latest);
00162 elecHeader->setTimeStamp(earliest);
00163
00164
00165 DayaBay::ElecPulseHeader* pulseHeader =
00166 new DayaBay::ElecPulseHeader(elecHeader);
00167 elecHeader->setPulseHeader(pulseHeader);
00168
00169
00170 DayaBay::ElecCrateHeader* crateHeader =
00171 new DayaBay::ElecCrateHeader(elecHeader);
00172 elecHeader->setCrateHeader(crateHeader);
00173
00174 double simDeltaT = (latest-earliest).GetSeconds()*CLHEP::second;
00175 if( simDeltaT > m_maxSimulationTime ){
00176
00177
00178
00179
00180 info() << "SimHits span too much time (" << simDeltaT/CLHEP::second
00181 << " us). Simulate kinematics individually." << endreq;
00182 return StatusCode::SUCCESS;
00183 }
00184
00185
00186 const DayaBay::SimHitHeader* simHitHeader = simHeader->hits();
00187 if ( !simHitHeader ) {
00188 info() << "Empty simhitheader. Do not try to process non-existent hit collections." <<endreq;
00189 }
00190 else {
00191 const DayaBay::SimHitHeader::hc_map& hcMap = simHitHeader->hitCollection();
00192
00193 info() << "Processing hit collections" <<endreq;
00194
00196 DayaBay::SimHitHeader::hc_map::const_iterator hcIter;
00197 for (hcIter=hcMap.begin(); hcIter != hcMap.end(); ++hcIter) {
00198
00199 DayaBay::Detector det(hcIter->first);
00200 debug() << "Checking " << det.detName() << " for hits." << endreq;
00201 DayaBay::SimHitCollection* hits = hcIter->second;
00202 if(!hits) return StatusCode::FAILURE;
00203
00204 debug() << "Got hit collection from " << det.detName()<<" (id= "
00205 << det.siteDetPackedData() << ") " << " with "
00206 << hits->collection().size() << " hits." << endreq;
00207
00208 StatusCode sc;
00209
00210 if( std::find(m_detectors.begin(),m_detectors.end(),det)
00211 == m_detectors.end()){
00212 debug() << "Detector " << det.detName() << " not simulated." << endreq;
00213 continue;
00214 }
00215 if( det.isAD() || det.isWaterShield() ){
00216
00217 debug() << "Processing PMT hits" <<endreq;
00218 DayaBay::ElecPulseCollection* pulses =
00219 new DayaBay::ElecPulseCollection(pulseHeader, det);
00220 sc = m_pmtTool->generatePulses(hits, pulses);
00221 if( sc != StatusCode::SUCCESS ) return sc;
00222 pulseHeader->addPulseCollection(pulses);
00223
00224 DayaBay::ElecFeeCrate* crate = new DayaBay::ElecFeeCrate(det,
00225 crateHeader);
00226 sc = m_feeTool->generateSignals(pulses, crate);
00227 if( sc != StatusCode::SUCCESS ) return sc;
00228 crateHeader->addCrate(crate);
00229
00230 }else if(det.detectorId() == DetectorId::kRPC){
00231
00232 debug() << "Processing RPC hits" <<endreq;
00233 DayaBay::ElecPulseCollection* pulses =
00234 new DayaBay::ElecPulseCollection(pulseHeader, det);
00235 sc = m_rpcTool->generatePulses(hits, pulses);
00236 if( sc != StatusCode::SUCCESS ) return sc;
00237 pulseHeader->addPulseCollection(pulses);
00238
00239 DayaBay::ElecFecCrate* crate = new DayaBay::ElecFecCrate(det,
00240 crateHeader);
00241 sc = m_fecTool->generateSignals(pulses, crate);
00242 if( sc != StatusCode::SUCCESS ) return sc;
00243 crateHeader->addCrate(crate);
00244 }else{
00245 error() << "Unknown detector " << det << endreq;
00246 return StatusCode::FAILURE;
00247 }
00248 }
00249
00250 }
00251 return StatusCode::SUCCESS;
00252 }
00253
00254 StatusCode EsFrontEndAlg::finalize()
00255 {
00256 return StatusCode::SUCCESS;
00257 }
00258