10 #include "GaudiKernel/Chrono.h"
16 #include "TGraphErrors.h"
29 m_outputFileName(
"output.cal"),
30 m_dumpAllHists(false),
32 m_lastPulserLevel(-999),
34 m_fracGraphs(nullptr),
36 m_fitReturns(nullptr),
39 m_currentAmpProf(nullptr),
41 m_pulsedChambers(nullptr),
44 m_intercepts(nullptr),
47 m_peakTimeProf(nullptr),
135 for(
unsigned int pulserLevel=0; pulserLevel < 64; pulserLevel++)
143 const std::vector<Identifier> &
ids =
m_idHelperSvc->cscIdHelper().idVector();
147 for(
const auto & thisChamberId:
ids)
149 std::vector<Identifier> stripVect;
150 m_idHelperSvc->cscIdHelper().idChannels(thisChamberId,stripVect);
154 for(
const auto & thisStripId:stripVect)
157 m_idHelperSvc->cscIdHelper().get_channel_hash(thisStripId,stripHash);
167 char orientation = (
m_idHelperSvc->cscIdHelper().measuresPhi(
id) ?
'Y':
'X');
170 int stationPhi =
m_idHelperSvc->cscIdHelper().stationPhi(
id);
171 int stationEta =
m_idHelperSvc->cscIdHelper().stationEta(
id);
175 char bitName[200], titleSeed[500];
177 sprintf(bitName,
"bitHist%d",(
int)stripHash);
178 sprintf(titleSeed,
"Bit histogram for eta %d, sector %d, layer %d%c strip %d",
179 stationEta,(2*stationPhi+50 -
stationName),wireLayer,orientation,stripNumber);
182 hist->GetXaxis()->SetTitle(
"Bit");
183 hist->GetYaxis()->SetTitle(
"Counts");
193 for(
unsigned int chanItr =0; chanItr <=
m_maxStripHash; chanItr++)
214 if( stripHash < 0 || (
unsigned int) stripHash >
m_maxStripHash ) {
216 ATH_MSG_FATAL(
"The hash "<< (
int) stripHash <<
" is out of range for the Ped-Vector - Crashing!");
217 return StatusCode::FAILURE;
244 return StatusCode::SUCCESS;
256 ATH_MSG_WARNING(
"There was an error collecting information from the RDO this event.");
260 return StatusCode::SUCCESS;
269 bool thereIsAFatal=
false;
285 thereIsAFatal =
true;
292 thereIsAFatal =
true;
300 return StatusCode::FAILURE;
302 return StatusCode::SUCCESS;
319 if (sc_read != StatusCode::SUCCESS)
322 return StatusCode::FAILURE;
332 for(
const auto rod:*fullRDO)
339 int pulserLevel =
rod->calAmplitude();
343 ATH_MSG_INFO(
"New pulser level found. (" << pulserLevel <<
").");
347 if(alreadyExistingProfile ==
m_ampProfs->end())
352 name <<
"ampProf_" << pulserLevel;
370 unsigned int samplingPhase =
rod->samplingPhase();
377 for(
const auto cluster: *
rod)
381 int numStrips = cluster->width();
382 int samplesPerStrip = (cluster->samples()).
size()/numStrips;
386 for(
int stripItr = 0; stripItr <numStrips; stripItr++)
390 m_idHelperSvc->cscIdHelper().get_channel_hash(stripId, cscChannelHashId);
391 int stripHash = cscChannelHashId;
394 int chamberLayer =
m_idHelperSvc->cscIdHelper().chamberLayer(stripId);
400 return StatusCode::FAILURE;
403 int currentWireLayer =
m_idHelperSvc->cscIdHelper().wireLayer(stripId) - 1;
404 if( currentWireLayer < 0 || currentWireLayer > 3)
406 ATH_MSG_FATAL(
"Problem in getting wire layer! - Current value is "
407 <<
m_idHelperSvc->cscIdHelper().wireLayer(stripId) <<
" while only values between 1-4 are allowed.");
408 return StatusCode::FAILURE;
410 bool isThisLayerPulsed = (pulsedWireLayer >> currentWireLayer)&0
x1;
411 if(isThisLayerPulsed)
414 std::vector<uint16_t> samples;
415 cluster->samples(stripItr,samplesPerStrip,samples);
426 if(!readCdo->readChannelPed(stripHash,
ped).isSuccess()){
429 else ATH_MSG_ERROR(
"Failed at getting pedestal from COOL for hash " << stripHash);
431 return StatusCode::RECOVERABLE;
436 if(!readCdo->readChannelNoise(stripHash,
noise).isSuccess())
440 else ATH_MSG_ERROR(
"Failed at getting noise from COOL for hash " << stripHash);
442 return StatusCode::FAILURE;
448 double peakAmp{}, peakTime{};
453 std::vector<float> floatSamples;
454 for(
const auto & thisSample:samples){
456 floatSamples.push_back(thisSample-
ped);
465 success =
m_cscCalibTool->findCharge((
float)samplingPeriod, samplingPhase,floatSamples,peakAmp,peakTime);
471 double adcSamples[4];
472 for(
int i = 0;
i < 4;
i++) adcSamples[
i] = samples[
i] -
ped;
473 double fitResult[3],fitErrors[3],
chi2;
474 double width = samplingPeriod == 50 ? 7.2:14.4;
478 peakAmp = fitResult[0];
479 peakTime = fitResult[1] - (samplingPhase ? 25 : 0);
497 ATH_MSG_WARNING(
"Pulsed layer " << pulsedWireLayer<<
", Samples: " << samples[0] <<
", " << samples[1] <<
", " << samples[2] <<
", " << samples[3]);
514 return StatusCode::SUCCESS;
528 return StatusCode::FAILURE;
530 unsigned int numCalibPoints =
m_ampProfs->size();
531 ATH_MSG_INFO(
"There are " << numCalibPoints <<
" pulser levels to evaluate.");
535 float chargeMax = 530.88;
537 int crossTalkCnt = 0;
539 for(
unsigned int stripHash = 0 ;stripHash <=
m_maxStripHash; stripHash++)
552 int chamberLayer =
m_idHelperSvc->cscIdHelper().chamberLayer(
id);
553 char orientation = (
m_idHelperSvc->cscIdHelper().measuresPhi(
id) ?
'Y':
'X');
559 int stationPhi =
m_idHelperSvc->cscIdHelper().stationPhi(
id);
560 int stationEta =
m_idHelperSvc->cscIdHelper().stationEta(
id);
581 if(orientation !=
'X')
585 bool foundMin(
false);
593 TGraphErrors * calGraph =
new TGraphErrors(numCalibPoints);
594 char calName[20],titleSeed[500];
595 sprintf(calName,
"calGraph%u",stripHash);
596 sprintf(titleSeed,
"Calgraph for eta %d, sector %d, layer %d%c, strip %d",stationEta,(2*stationPhi+50 -
stationName),wireLayer,orientation, stripNumber);
597 calGraph->SetName(calName);
599 calGraph->SetTitle(
title.c_str());
600 calGraph->GetYaxis()->SetTitle(
"ADC counts");
601 calGraph->GetXaxis()->SetTitle(
"Attenuation (-db)");
605 bool isGoodStrip =
false;
611 for(
const auto & [pulserLevel, pAmplitudeProfile] : *
m_ampProfs)
613 if(!pAmplitudeProfile){
615 return StatusCode::FAILURE;
620 if(pAmplitudeProfile->GetBinEntries(stripHash+1))
628 float adcValue = pAmplitudeProfile->GetBinContent(stripHash+1);
629 float adcError = pAmplitudeProfile->GetBinError(stripHash+1);
636 if(adcError != adcError)
648 ATH_MSG_DEBUG(
"\tStoring at db of " <<
db <<
" with attenValue " << attenValue <<
" from pulser level of " << pulserLevel <<
" and adcValue " << adcValue);
654 thisDrop = lastVal - adcValue;
656 <<
"\tlastVal = " << lastVal
657 <<
";lastDrop " << lastDrop <<
"; thisDrop " << thisDrop);
661 fitMinX = attenValue;
671 if(attenValue > fitMaxX)
672 fitMaxX = attenValue;
674 calGraph->SetPoint(calPointItr,attenValue,adcValue);
675 calGraph->SetPointError(calPointItr,0.01,adcError);
681 if(!foundMin && isGoodStrip){
689 ATH_MSG_INFO(
"we have a good stripHash at " << stripHash);
693 float slope, slopeError, intercept, interceptError,
chiSquared;
698 TF1 myFunc(
"myFunction",
m_calFitFunc.c_str(), fitMinX, fitMaxX);
699 myFunc.SetLineColor(kRed);
702 myFunc.SetParameters(0,5);
703 slope = myFunc.GetParameter(1);
704 slopeError = myFunc.GetParError(1);
705 intercept = myFunc.GetParameter(0);
706 interceptError = myFunc.GetParError(0);
708 ndf = myFunc.GetNDF();
712 myFunc.SetParameters(0.1,2000);
714 fitRet = calGraph->Fit(&myFunc,
"RV");
716 slope = myFunc.GetParameter(1)/chargeMax;
717 slopeError = myFunc.GetParError(1);
718 intercept = myFunc.GetParameter(0);
719 interceptError = myFunc.GetParError(0);
721 ndf = myFunc.GetNDF();
725 if(std::abs(slope) < 0.00001 || slope == -999)
731 invertedSlope = 1/slope;
733 ATH_MSG_ERROR(
"Inserting calgraph in for hash " << stripHash);
734 (*m_calGraphs)[stripHash] = calGraph;
736 ATH_MSG_DEBUG(
"StripHash: " << stripHash <<
"; slope: " <<slope
737 <<
"; intercept: " << intercept
744 (*m_fitReturns)[stripHash] = fitRet;
749 if(crossTalkCnt == 23)
755 ATH_MSG_INFO(
"Completed calculating parameters for each strip");
756 return StatusCode::SUCCESS;
773 return StatusCode::RECOVERABLE;
785 return StatusCode::FAILURE;
794 out <<
"END_HEADER\n";
809 for(; slopeItr != slopeEnd; ++slopeItr)
813 ATH_MSG_FATAL(
"Peaktimes out of sync with slopes. Quiting write.");
815 return StatusCode::FAILURE;
819 out << (*slopeItr)->hashId();
824 m_idHelperSvc->cscIdHelper().get_id((*slopeItr)->hashId(),
id, &channelContext);
833 return StatusCode::FAILURE;
836 m_idHelperSvc->cscIdHelper().get_module_hash(
id,chamberHash);
837 out <<
" " << chamberHash;
843 out <<
" " << (*slopeItr)->value();
850 return StatusCode::SUCCESS;
859 bool thereIsAnError =
false;
861 std::string histKey =
"cscSlopeCalibReport";
862 ATH_MSG_DEBUG(
"Recording calibration graphs to TDS with key " << histKey);
878 ATH_MSG_ERROR(
"Failed to record CscCalibReportSlope to storegate");
879 thereIsAnError =
true;
890 sc =
evtStore()->record(calibResults,
"CscCalibResultSlope");
894 thereIsAnError =
true;
900 return StatusCode::RECOVERABLE;
902 return StatusCode::SUCCESS;
913 return StatusCode::RECOVERABLE;
915 out <<
"03-00 <END_HEADER>";
918 out <<
"\n<END_FILE>";
923 return StatusCode::SUCCESS;
934 out <<
"<NEW_PAR> " <<
results.parName() <<
"\n";
935 std::string idString;
939 for(; resItr != resEnd; ++resItr){
940 unsigned int hashId = (*resItr)->hashId();
941 double value = (*resItr)->value();
942 std::string idString;
944 readCdo->indexToStringId(&
m_idHelperSvc->cscIdHelper(), hashId,
"CHANNEL", idString).ignore();
946 out << idString <<
" " <<
value <<
"\n";