00001 #include "SimpPmtCalibTool.h"
00002
00003 #include "Event/ReadoutHeader.h"
00004 #include "Event/DaqCrate.h"
00005 #include "Event/DaqPmtCrate.h"
00006 #include "Event/DaqPmtChannel.h"
00007
00008 #include "Event/CalibReadoutHeader.h"
00009 #include "Event/CalibReadoutPmtChannel.h"
00010 #include "Event/CalibReadoutPmtCrate.h"
00011 #include "Event/CalibReadout.h"
00012
00013 #include "Context/TimeStamp.h"
00014
00015 #include "CLHEP/Vector/ThreeVector.h"
00016 #include "DataSvc/ICalibDataSvc.h"
00017 #include "DataSvc/ICableSvc.h"
00018
00019 #include "Conventions/Detectors.h"
00020 #include "Conventions/Trigger.h"
00021 #include "GaudiKernel/SystemOfUnits.h"
00022 #include "GaudiKernel/PhysicalConstants.h"
00023
00024 #include <vector>
00025 #include <fstream>
00026
00027 using namespace Gaudi;
00028 using namespace DayaBay;
00029 using namespace std;
00030
00031 SimpPmtCalibTool::SimpPmtCalibTool(const std::string& type,
00032 const std::string& name,
00033 const IInterface* parent)
00034 : GaudiTool(type, name, parent)
00035 , m_calibDataSvc(0)
00036 , m_cableSvc(0)
00037 {
00038 declareInterface< ICalibTool >(this);
00039 declareProperty("CalibDataSvcName", m_calibDataSvcName = "StaticCalibDataSvc",
00040 "Name of Pmt calibration data Service");
00041 declareProperty("CableSvcName", m_cableSvcName = "StaticCableSvc",
00042 "Name of Cable Service");
00043 declareProperty("UseDynamicPedestal", m_useDynamicPedestal=true,
00044 "Use pedestal value embedded with each hit");
00045 declareProperty("UseUncalibratedCharge", m_useUncalibratedCharge=false,
00046 "Use approximate charge calibration");
00047 declareProperty("UseUncalibratedTime", m_useUncalibratedTime=false,
00048 "Use approximate time calibration");
00049 }
00050
00051 SimpPmtCalibTool::~SimpPmtCalibTool() {}
00052
00053 StatusCode SimpPmtCalibTool::initialize()
00054 {
00055
00056 m_calibDataSvc = svc<ICalibDataSvc>(m_calibDataSvcName, true);
00057
00058
00059 m_cableSvc = svc<ICableSvc>(m_cableSvcName, true);
00060
00061 debug() << "successfully get CableSvc and CalibSvc" << endreq;
00062
00063 return StatusCode::SUCCESS;
00064 }
00065
00066
00067 StatusCode SimpPmtCalibTool::finalize()
00068 {
00069 return StatusCode::SUCCESS;
00070 }
00071
00072 StatusCode SimpPmtCalibTool::calibration(const DayaBay::DaqCrate* readout,
00073 DayaBay::CalibReadout* calibReadout)
00074 {
00075 debug() << "running calibration() " << endreq;
00076 StatusCode sc;
00077
00078
00079 const DayaBay::DaqPmtCrate* pmtReadout = readout->asPmtCrate();
00080 if(!pmtReadout){
00081 error() << "calibration(): no pmt readouts." << endreq;
00082 return StatusCode::FAILURE;
00083 }
00084
00085 DayaBay::CalibReadoutPmtCrate* calibReadoutCrate
00086 = dynamic_cast<DayaBay::CalibReadoutPmtCrate*>(calibReadout);
00087
00088 if(calibReadoutCrate == NULL) {
00089 error() << "calibration(): no calib pmt readouts." << endreq;
00090 return StatusCode::FAILURE;
00091 }
00092
00093 const std::vector<DayaBay::DaqPmtChannel*>& channels
00094 = pmtReadout->channelReadouts();
00095 std::vector<DayaBay::DaqPmtChannel*>::const_iterator channelIter,
00096 channelEnd = channels.end();
00097
00098 int task = 0;
00099 Context context = calibReadoutCrate->header()->context();
00100 ServiceMode svcMode(context, task);
00101
00102
00103 for(channelIter = channels.begin();
00104 channelIter!=channelEnd;
00105 channelIter++){
00106 const DayaBay::DaqPmtChannel* channel = *channelIter;
00107 const DayaBay::FeeChannelId& channelId = channel->channelId();
00108
00109
00110 const DayaBay::DetectorSensor& sensDetId
00111 = m_cableSvc->sensor(channelId, svcMode);
00112
00113 calibReadoutCrate->addSensor(sensDetId);
00114 DayaBay::CalibReadoutPmtChannel& cro_ch
00115 = *(calibReadoutCrate->sensor(sensDetId));
00116
00117 const DayaBay::PmtCalibData* pmtCalib = 0;
00118 if(!m_useUncalibratedCharge || !m_useUncalibratedTime){
00119 pmtCalib = m_calibDataSvc->pmtCalibData(sensDetId, svcMode);
00120 if(!pmtCalib) {
00121 error() << "No calibration properties for PMT: " << sensDetId
00122 << endreq;
00123 return StatusCode::FAILURE;
00124 }
00125 }
00126
00127 const DayaBay::FeeCalibData* feeCalib = 0;
00128 if(!m_useDynamicPedestal){
00129 feeCalib = m_calibDataSvc->feeCalibData(channelId, svcMode);
00130 if(!feeCalib) {
00131 error() << "No calibration properties for FEE channel: "
00132 << channelId << endreq;
00133 return StatusCode::FAILURE;
00134 }
00135 }
00136
00137
00138 std::vector<double> calib_time;
00139 std::vector<double> calib_charge;
00140 double tdcToTime = -(1.0/DayaBay::TdcFrequencyHz) * Units::second;
00141 for(unsigned int hitIdx=0; hitIdx<channel->hitCount(); hitIdx++){
00142
00143 double calibTime = channel->tdc(hitIdx) * tdcToTime;
00144 if(!m_useUncalibratedTime) calibTime -= pmtCalib->m_timeOffset;
00145 calib_time.push_back( calibTime );
00146
00147 double adcBaseline = 0.;
00148 double adcToSpe = 0.;
00149 if(channel->adc(hitIdx)==0) {
00150
00151 calib_charge.push_back(0.);
00152 continue;
00153 }
00154
00155 if(m_useDynamicPedestal){
00156 adcBaseline = channel->preAdcAvg(hitIdx);
00157 }else{
00158 if( channel->isHighGainAdc(hitIdx) ){
00159 adcBaseline = feeCalib->m_adcBaselineHigh;
00160 }else{
00161 adcBaseline = feeCalib->m_adcBaselineLow;
00162 }
00163 }
00164
00165
00166 if(m_useUncalibratedCharge){
00167
00168 adcToSpe = 1. / 20.;
00169 if(! channel->isHighGainAdc(hitIdx) ) adcToSpe = 1.0;
00170 }else{
00171
00172 if(channel->isHighGainAdc(hitIdx)) {
00173 adcToSpe = 1.0 / pmtCalib->m_speHigh;
00174 }else{
00175 adcToSpe = 1.0 / pmtCalib->m_speLow;
00176 }
00177 }
00178 calib_charge.push_back( (channel->adc(hitIdx) - adcBaseline) * adcToSpe);
00179
00180 }
00181 cro_ch.setTime(calib_time);
00182 cro_ch.setCharge(calib_charge);
00183 }
00184
00185 return StatusCode::SUCCESS;
00186 }