11 #include "GaudiKernel/Chrono.h"
20 #include "TGraphErrors.h"
34 m_cscCalibTool(nullptr),
36 m_outputFileName(
"output.cal"),
37 m_dumpAllHists(false),
39 m_lastPulserLevel(-999),
41 m_fracGraphs(nullptr),
43 m_fitReturns(nullptr),
46 m_currentAmpProf(nullptr),
48 m_pulsedChambers(nullptr),
51 m_intercepts(nullptr),
54 m_peakTimeProf(nullptr),
136 IToolSvc* toolSvc=
nullptr;
148 for(
unsigned int pulserLevel=0; pulserLevel < 64; pulserLevel++)
156 const std::vector<Identifier> &
ids =
m_idHelperSvc->cscIdHelper().idVector();
160 for(
const auto & thisChamberId:
ids)
162 std::vector<Identifier> stripVect;
163 m_idHelperSvc->cscIdHelper().idChannels(thisChamberId,stripVect);
167 for(
const auto & thisStripId:stripVect)
170 m_idHelperSvc->cscIdHelper().get_channel_hash(thisStripId,stripHash);
180 char orientation = (
m_idHelperSvc->cscIdHelper().measuresPhi(
id) ?
'Y':
'X');
183 int stationPhi =
m_idHelperSvc->cscIdHelper().stationPhi(
id);
184 int stationEta =
m_idHelperSvc->cscIdHelper().stationEta(
id);
188 char bitName[200], titleSeed[500];
190 sprintf(bitName,
"bitHist%d",(
int)stripHash);
191 sprintf(titleSeed,
"Bit histogram for eta %d, sector %d, layer %d%c strip %d",
192 stationEta,(2*stationPhi+50 -
stationName),wireLayer,orientation,stripNumber);
195 hist->GetXaxis()->SetTitle(
"Bit");
196 hist->GetYaxis()->SetTitle(
"Counts");
206 for(
unsigned int chanItr =0; chanItr <=
m_maxStripHash; chanItr++)
227 if( stripHash < 0 || (
unsigned int) stripHash >
m_maxStripHash ) {
229 ATH_MSG_FATAL(
"The hash "<< (
int) stripHash <<
" is out of range for the Ped-Vector - Crashing!");
230 return StatusCode::FAILURE;
257 return StatusCode::SUCCESS;
269 ATH_MSG_WARNING(
"There was an error collecting information from the RDO this event.");
273 return StatusCode::SUCCESS;
282 bool thereIsAFatal=
false;
298 thereIsAFatal =
true;
305 thereIsAFatal =
true;
314 return StatusCode::FAILURE;
316 return StatusCode::SUCCESS;
333 if (sc_read != StatusCode::SUCCESS)
336 return StatusCode::FAILURE;
346 for(
const auto rod:*fullRDO)
353 int pulserLevel =
rod->calAmplitude();
357 ATH_MSG_INFO(
"New pulser level found. (" << pulserLevel <<
").");
361 if(alreadyExistingProfile ==
m_ampProfs->end())
366 name <<
"ampProf_" << pulserLevel;
384 unsigned int samplingPhase =
rod->samplingPhase();
391 for(
const auto cluster: *
rod)
395 int numStrips = cluster->width();
396 int samplesPerStrip = (cluster->samples()).
size()/numStrips;
400 for(
int stripItr = 0; stripItr <numStrips; stripItr++)
404 m_idHelperSvc->cscIdHelper().get_channel_hash(stripId, cscChannelHashId);
405 int stripHash = cscChannelHashId;
408 int chamberLayer =
m_idHelperSvc->cscIdHelper().chamberLayer(stripId);
414 return StatusCode::FAILURE;
417 int currentWireLayer =
m_idHelperSvc->cscIdHelper().wireLayer(stripId) - 1;
418 if( currentWireLayer < 0 || currentWireLayer > 3)
420 ATH_MSG_FATAL(
"Problem in getting wire layer! - Current value is "
421 <<
m_idHelperSvc->cscIdHelper().wireLayer(stripId) <<
" while only values between 1-4 are allowed.");
422 return StatusCode::FAILURE;
424 bool isThisLayerPulsed = (pulsedWireLayer >> currentWireLayer)&0
x1;
425 if(isThisLayerPulsed)
429 cluster->samples(stripItr,samplesPerStrip,
samples);
440 if(!readCdo->readChannelPed(stripHash,
ped).isSuccess()){
443 else ATH_MSG_ERROR(
"Failed at getting pedestal from COOL for hash " << stripHash);
445 return StatusCode::RECOVERABLE;
450 if(!readCdo->readChannelNoise(stripHash,
noise).isSuccess())
454 else ATH_MSG_ERROR(
"Failed at getting noise from COOL for hash " << stripHash);
456 return StatusCode::FAILURE;
462 double peakAmp{}, peakTime{};
467 std::vector<float> floatSamples;
468 for(
const auto & thisSample:
samples){
470 floatSamples.push_back(thisSample-
ped);
485 double adcSamples[4];
487 double fitResult[3],fitErrors[3],
chi2;
488 double width = samplingPeriod == 50 ? 7.2:14.4;
492 peakAmp = fitResult[0];
493 peakTime = fitResult[1] - (samplingPhase ? 25 : 0);
528 return StatusCode::SUCCESS;
542 return StatusCode::FAILURE;
544 unsigned int numCalibPoints =
m_ampProfs->size();
545 ATH_MSG_INFO(
"There are " << numCalibPoints <<
" pulser levels to evaluate.");
549 float chargeMax = 530.88;
551 int crossTalkCnt = 0;
553 for(
unsigned int stripHash = 0 ;stripHash <=
m_maxStripHash; stripHash++)
566 int chamberLayer =
m_idHelperSvc->cscIdHelper().chamberLayer(
id);
567 char orientation = (
m_idHelperSvc->cscIdHelper().measuresPhi(
id) ?
'Y':
'X');
573 int stationPhi =
m_idHelperSvc->cscIdHelper().stationPhi(
id);
574 int stationEta =
m_idHelperSvc->cscIdHelper().stationEta(
id);
595 if(orientation !=
'X')
599 bool foundMin(
false);
607 TGraphErrors * calGraph =
new TGraphErrors(numCalibPoints);
608 char calName[20],titleSeed[500];
609 sprintf(calName,
"calGraph%u",stripHash);
610 sprintf(titleSeed,
"Calgraph for eta %d, sector %d, layer %d%c, strip %d",stationEta,(2*stationPhi+50 -
stationName),wireLayer,orientation, stripNumber);
611 calGraph->SetName(calName);
613 calGraph->SetTitle(
title.c_str());
614 calGraph->GetYaxis()->SetTitle(
"ADC counts");
615 calGraph->GetXaxis()->SetTitle(
"Attenuation (-db)");
619 bool isGoodStrip =
false;
625 for(
const auto & [pulserLevel, pAmplitudeProfile] : *
m_ampProfs)
627 if(!pAmplitudeProfile){
629 return StatusCode::FAILURE;
634 if(pAmplitudeProfile->GetBinEntries(stripHash+1))
642 float adcValue = pAmplitudeProfile->GetBinContent(stripHash+1);
643 float adcError = pAmplitudeProfile->GetBinError(stripHash+1);
650 if(adcError != adcError)
662 ATH_MSG_DEBUG(
"\tStoring at db of " <<
db <<
" with attenValue " << attenValue <<
" from pulser level of " << pulserLevel <<
" and adcValue " << adcValue);
668 thisDrop = lastVal - adcValue;
670 <<
"\tlastVal = " << lastVal
671 <<
";lastDrop " << lastDrop <<
"; thisDrop " << thisDrop);
675 fitMinX = attenValue;
685 if(attenValue > fitMaxX)
686 fitMaxX = attenValue;
688 calGraph->SetPoint(calPointItr,attenValue,adcValue);
689 calGraph->SetPointError(calPointItr,0.01,adcError);
695 if(!foundMin && isGoodStrip){
703 ATH_MSG_INFO(
"we have a good stripHash at " << stripHash);
707 float slope, slopeError, intercept, interceptError,
chiSquared;
712 TF1 myFunc(
"myFunction",
m_calFitFunc.c_str(), fitMinX, fitMaxX);
713 myFunc.SetLineColor(kRed);
716 myFunc.SetParameters(0,5);
717 slope = myFunc.GetParameter(1);
718 slopeError = myFunc.GetParError(1);
719 intercept = myFunc.GetParameter(0);
720 interceptError = myFunc.GetParError(0);
722 ndf = myFunc.GetNDF();
726 myFunc.SetParameters(0.1,2000);
728 fitRet = calGraph->Fit(&myFunc,
"RV");
730 slope = myFunc.GetParameter(1)/chargeMax;
731 slopeError = myFunc.GetParError(1);
732 intercept = myFunc.GetParameter(0);
733 interceptError = myFunc.GetParError(0);
735 ndf = myFunc.GetNDF();
739 if(std::abs(slope) < 0.00001 || slope == -999)
745 invertedSlope = 1/slope;
747 ATH_MSG_ERROR(
"Inserting calgraph in for hash " << stripHash);
748 (*m_calGraphs)[stripHash] = calGraph;
750 ATH_MSG_DEBUG(
"StripHash: " << stripHash <<
"; slope: " <<slope
751 <<
"; intercept: " << intercept
758 (*m_fitReturns)[stripHash] = fitRet;
763 if(crossTalkCnt == 23)
769 ATH_MSG_INFO(
"Completed calculating parameters for each strip");
770 return StatusCode::SUCCESS;
787 return StatusCode::RECOVERABLE;
799 return StatusCode::FAILURE;
808 out <<
"END_HEADER\n";
823 for(; slopeItr != slopeEnd; ++slopeItr)
827 ATH_MSG_FATAL(
"Peaktimes out of sync with slopes. Quiting write.");
829 return StatusCode::FAILURE;
833 out << (*slopeItr)->hashId();
838 m_idHelperSvc->cscIdHelper().get_id((*slopeItr)->hashId(),
id, &channelContext);
847 return StatusCode::FAILURE;
850 m_idHelperSvc->cscIdHelper().get_module_hash(
id,chamberHash);
851 out <<
" " << chamberHash;
857 out <<
" " << (*slopeItr)->value();
864 return StatusCode::SUCCESS;
873 bool thereIsAnError =
false;
875 std::string histKey =
"cscSlopeCalibReport";
876 ATH_MSG_DEBUG(
"Recording calibration graphs to TDS with key " << histKey);
892 ATH_MSG_ERROR(
"Failed to record CscCalibReportSlope to storegate");
893 thereIsAnError =
true;
908 thereIsAnError =
true;
914 return StatusCode::RECOVERABLE;
916 return StatusCode::SUCCESS;
927 return StatusCode::RECOVERABLE;
929 out <<
"03-00 <END_HEADER>";
932 out <<
"\n<END_FILE>";
937 return StatusCode::SUCCESS;
948 out <<
"<NEW_PAR> " <<
results.parName() <<
"\n";
949 std::string idString;
953 for(; resItr != resEnd; ++resItr){
954 unsigned int hashId = (*resItr)->hashId();
955 double value = (*resItr)->value();
956 std::string idString;
958 readCdo->indexToStringId(&
m_idHelperSvc->cscIdHelper(), hashId,
"CHANNEL", idString).ignore();
960 out << idString <<
" " <<
value <<
"\n";