10 #include "GaudiKernel/Chrono.h"
19 #include "TGraphErrors.h"
32 m_outputFileName(
"output.cal"),
33 m_dumpAllHists(false),
35 m_lastPulserLevel(-999),
37 m_fracGraphs(nullptr),
39 m_fitReturns(nullptr),
42 m_currentAmpProf(nullptr),
44 m_pulsedChambers(nullptr),
47 m_intercepts(nullptr),
50 m_peakTimeProf(nullptr),
139 for(
unsigned int pulserLevel=0; pulserLevel < 64; pulserLevel++)
147 const std::vector<Identifier> &
ids =
m_idHelperSvc->cscIdHelper().idVector();
151 for(
const auto & thisChamberId:
ids)
153 std::vector<Identifier> stripVect;
154 m_idHelperSvc->cscIdHelper().idChannels(thisChamberId,stripVect);
158 for(
const auto & thisStripId:stripVect)
161 m_idHelperSvc->cscIdHelper().get_channel_hash(thisStripId,stripHash);
171 char orientation = (
m_idHelperSvc->cscIdHelper().measuresPhi(
id) ?
'Y':
'X');
174 int stationPhi =
m_idHelperSvc->cscIdHelper().stationPhi(
id);
175 int stationEta =
m_idHelperSvc->cscIdHelper().stationEta(
id);
179 char bitName[200], titleSeed[500];
181 sprintf(bitName,
"bitHist%d",(
int)stripHash);
182 sprintf(titleSeed,
"Bit histogram for eta %d, sector %d, layer %d%c strip %d",
183 stationEta,(2*stationPhi+50 -
stationName),wireLayer,orientation,stripNumber);
186 hist->GetXaxis()->SetTitle(
"Bit");
187 hist->GetYaxis()->SetTitle(
"Counts");
197 for(
unsigned int chanItr =0; chanItr <=
m_maxStripHash; chanItr++)
218 if( stripHash < 0 || (
unsigned int) stripHash >
m_maxStripHash ) {
220 ATH_MSG_FATAL(
"The hash "<< (
int) stripHash <<
" is out of range for the Ped-Vector - Crashing!");
221 return StatusCode::FAILURE;
248 return StatusCode::SUCCESS;
260 ATH_MSG_WARNING(
"There was an error collecting information from the RDO this event.");
264 return StatusCode::SUCCESS;
273 bool thereIsAFatal=
false;
289 thereIsAFatal =
true;
296 thereIsAFatal =
true;
305 return StatusCode::FAILURE;
307 return StatusCode::SUCCESS;
324 if (sc_read != StatusCode::SUCCESS)
327 return StatusCode::FAILURE;
337 for(
const auto rod:*fullRDO)
344 int pulserLevel =
rod->calAmplitude();
348 ATH_MSG_INFO(
"New pulser level found. (" << pulserLevel <<
").");
352 if(alreadyExistingProfile ==
m_ampProfs->end())
357 name <<
"ampProf_" << pulserLevel;
375 unsigned int samplingPhase =
rod->samplingPhase();
382 for(
const auto cluster: *
rod)
386 int numStrips = cluster->width();
387 int samplesPerStrip = (cluster->samples()).
size()/numStrips;
391 for(
int stripItr = 0; stripItr <numStrips; stripItr++)
395 m_idHelperSvc->cscIdHelper().get_channel_hash(stripId, cscChannelHashId);
396 int stripHash = cscChannelHashId;
399 int chamberLayer =
m_idHelperSvc->cscIdHelper().chamberLayer(stripId);
405 return StatusCode::FAILURE;
408 int currentWireLayer =
m_idHelperSvc->cscIdHelper().wireLayer(stripId) - 1;
409 if( currentWireLayer < 0 || currentWireLayer > 3)
411 ATH_MSG_FATAL(
"Problem in getting wire layer! - Current value is "
412 <<
m_idHelperSvc->cscIdHelper().wireLayer(stripId) <<
" while only values between 1-4 are allowed.");
413 return StatusCode::FAILURE;
415 bool isThisLayerPulsed = (pulsedWireLayer >> currentWireLayer)&0
x1;
416 if(isThisLayerPulsed)
419 std::vector<uint16_t> samples;
420 cluster->samples(stripItr,samplesPerStrip,samples);
431 if(!readCdo->readChannelPed(stripHash,
ped).isSuccess()){
434 else ATH_MSG_ERROR(
"Failed at getting pedestal from COOL for hash " << stripHash);
436 return StatusCode::RECOVERABLE;
441 if(!readCdo->readChannelNoise(stripHash,
noise).isSuccess())
445 else ATH_MSG_ERROR(
"Failed at getting noise from COOL for hash " << stripHash);
447 return StatusCode::FAILURE;
453 double peakAmp{}, peakTime{};
458 std::vector<float> floatSamples;
459 for(
const auto & thisSample:samples){
461 floatSamples.push_back(thisSample-
ped);
470 success =
m_cscCalibTool->findCharge((
float)samplingPeriod, samplingPhase,floatSamples,peakAmp,peakTime);
476 double adcSamples[4];
477 for(
int i = 0;
i < 4;
i++) adcSamples[
i] = samples[
i] -
ped;
478 double fitResult[3],fitErrors[3],
chi2;
479 double width = samplingPeriod == 50 ? 7.2:14.4;
483 peakAmp = fitResult[0];
484 peakTime = fitResult[1] - (samplingPhase ? 25 : 0);
502 ATH_MSG_WARNING(
"Pulsed layer " << pulsedWireLayer<<
", Samples: " << samples[0] <<
", " << samples[1] <<
", " << samples[2] <<
", " << samples[3]);
519 return StatusCode::SUCCESS;
533 return StatusCode::FAILURE;
535 unsigned int numCalibPoints =
m_ampProfs->size();
536 ATH_MSG_INFO(
"There are " << numCalibPoints <<
" pulser levels to evaluate.");
540 float chargeMax = 530.88;
542 int crossTalkCnt = 0;
544 for(
unsigned int stripHash = 0 ;stripHash <=
m_maxStripHash; stripHash++)
557 int chamberLayer =
m_idHelperSvc->cscIdHelper().chamberLayer(
id);
558 char orientation = (
m_idHelperSvc->cscIdHelper().measuresPhi(
id) ?
'Y':
'X');
564 int stationPhi =
m_idHelperSvc->cscIdHelper().stationPhi(
id);
565 int stationEta =
m_idHelperSvc->cscIdHelper().stationEta(
id);
586 if(orientation !=
'X')
590 bool foundMin(
false);
598 TGraphErrors * calGraph =
new TGraphErrors(numCalibPoints);
599 char calName[20],titleSeed[500];
600 sprintf(calName,
"calGraph%u",stripHash);
601 sprintf(titleSeed,
"Calgraph for eta %d, sector %d, layer %d%c, strip %d",stationEta,(2*stationPhi+50 -
stationName),wireLayer,orientation, stripNumber);
602 calGraph->SetName(calName);
604 calGraph->SetTitle(
title.c_str());
605 calGraph->GetYaxis()->SetTitle(
"ADC counts");
606 calGraph->GetXaxis()->SetTitle(
"Attenuation (-db)");
610 bool isGoodStrip =
false;
616 for(
const auto & [pulserLevel, pAmplitudeProfile] : *
m_ampProfs)
618 if(!pAmplitudeProfile){
620 return StatusCode::FAILURE;
625 if(pAmplitudeProfile->GetBinEntries(stripHash+1))
633 float adcValue = pAmplitudeProfile->GetBinContent(stripHash+1);
634 float adcError = pAmplitudeProfile->GetBinError(stripHash+1);
641 if(adcError != adcError)
653 ATH_MSG_DEBUG(
"\tStoring at db of " <<
db <<
" with attenValue " << attenValue <<
" from pulser level of " << pulserLevel <<
" and adcValue " << adcValue);
659 thisDrop = lastVal - adcValue;
661 <<
"\tlastVal = " << lastVal
662 <<
";lastDrop " << lastDrop <<
"; thisDrop " << thisDrop);
666 fitMinX = attenValue;
676 if(attenValue > fitMaxX)
677 fitMaxX = attenValue;
679 calGraph->SetPoint(calPointItr,attenValue,adcValue);
680 calGraph->SetPointError(calPointItr,0.01,adcError);
686 if(!foundMin && isGoodStrip){
694 ATH_MSG_INFO(
"we have a good stripHash at " << stripHash);
698 float slope, slopeError, intercept, interceptError,
chiSquared;
703 TF1 myFunc(
"myFunction",
m_calFitFunc.c_str(), fitMinX, fitMaxX);
704 myFunc.SetLineColor(kRed);
707 myFunc.SetParameters(0,5);
708 slope = myFunc.GetParameter(1);
709 slopeError = myFunc.GetParError(1);
710 intercept = myFunc.GetParameter(0);
711 interceptError = myFunc.GetParError(0);
713 ndf = myFunc.GetNDF();
717 myFunc.SetParameters(0.1,2000);
719 fitRet = calGraph->Fit(&myFunc,
"RV");
721 slope = myFunc.GetParameter(1)/chargeMax;
722 slopeError = myFunc.GetParError(1);
723 intercept = myFunc.GetParameter(0);
724 interceptError = myFunc.GetParError(0);
726 ndf = myFunc.GetNDF();
730 if(std::abs(slope) < 0.00001 || slope == -999)
736 invertedSlope = 1/slope;
738 ATH_MSG_ERROR(
"Inserting calgraph in for hash " << stripHash);
739 (*m_calGraphs)[stripHash] = calGraph;
741 ATH_MSG_DEBUG(
"StripHash: " << stripHash <<
"; slope: " <<slope
742 <<
"; intercept: " << intercept
749 (*m_fitReturns)[stripHash] = fitRet;
754 if(crossTalkCnt == 23)
760 ATH_MSG_INFO(
"Completed calculating parameters for each strip");
761 return StatusCode::SUCCESS;
778 return StatusCode::RECOVERABLE;
790 return StatusCode::FAILURE;
799 out <<
"END_HEADER\n";
814 for(; slopeItr != slopeEnd; ++slopeItr)
818 ATH_MSG_FATAL(
"Peaktimes out of sync with slopes. Quiting write.");
820 return StatusCode::FAILURE;
824 out << (*slopeItr)->hashId();
829 m_idHelperSvc->cscIdHelper().get_id((*slopeItr)->hashId(),
id, &channelContext);
838 return StatusCode::FAILURE;
841 m_idHelperSvc->cscIdHelper().get_module_hash(
id,chamberHash);
842 out <<
" " << chamberHash;
848 out <<
" " << (*slopeItr)->value();
855 return StatusCode::SUCCESS;
864 bool thereIsAnError =
false;
866 std::string histKey =
"cscSlopeCalibReport";
867 ATH_MSG_DEBUG(
"Recording calibration graphs to TDS with key " << histKey);
883 ATH_MSG_ERROR(
"Failed to record CscCalibReportSlope to storegate");
884 thereIsAnError =
true;
895 sc =
evtStore()->record(calibResults,
"CscCalibResultSlope");
899 thereIsAnError =
true;
905 return StatusCode::RECOVERABLE;
907 return StatusCode::SUCCESS;
918 return StatusCode::RECOVERABLE;
920 out <<
"03-00 <END_HEADER>";
923 out <<
"\n<END_FILE>";
928 return StatusCode::SUCCESS;
939 out <<
"<NEW_PAR> " <<
results.parName() <<
"\n";
940 std::string idString;
944 for(; resItr != resEnd; ++resItr){
945 unsigned int hashId = (*resItr)->hashId();
946 double value = (*resItr)->value();
947 std::string idString;
949 readCdo->indexToStringId(&
m_idHelperSvc->cscIdHelper(), hashId,
"CHANNEL", idString).ignore();
951 out << idString <<
" " <<
value <<
"\n";