diff --git a/CRV/CrvCalibration.C b/CRV/CrvCalibration.C index 6852596..d60d864 100644 --- a/CRV/CrvCalibration.C +++ b/CRV/CrvCalibration.C @@ -1,12 +1,17 @@ const double fitRangeStart=0.8; const double fitRangeEnd=1.2; const int minHistEntries=100; -const int spectrumNPeaks=6; +const double minPeakPulseHeight=10.0; +const double minPeakPulseArea=250.0; +const int spectrumNPeaks=100; const double spectrumPeakSigma=4.0; +//const double spectrumPeakThreshold=0.001; const double spectrumPeakThreshold=0.01; -const double peakRatioTolerance=0.2; +const double peakRatioTolerance=0.3; -bool FindSPEpeak(TH1F *hist, TSpectrum &spectrum, double &SPEpeak); +//template +//bool FindSPEpeak(TH1F *hist, TSpectrum &spectrum, std::array &functions, double &SPEpeak, double minPeak); +bool FindSPEpeak(TH1F *hist, TSpectrum &spectrum, TF1 &function, double &SPEpeak, double minPeak); void CrvCalibration(const std::string &inputFileName, const std::string &outputFileName) { @@ -24,7 +29,8 @@ void CrvCalibration(const std::string &inputFileName, const std::string &outputF pedestals[channel]=pedestal; } - TF1 funcCalib("SPEpeak", "gaus"); +// std::array functions={TF1("calibPeak1","gaus"), TF1("calibPeak2","gaus"), TF1("calibPeak3","gaus")}; //only need to fit three peaks + TF1 function("calibPeak","gaus"); TSpectrum spectrum(spectrumNPeaks); //any value of 3 or less results in a "Peak buffer full" warning. std::ofstream outputFile; @@ -39,27 +45,20 @@ void CrvCalibration(const std::string &inputFileName, const std::string &outputF TH1F *hist; double calibValue[2]; - for(int i=0; i<2; ++i) //loop over hisograms with pulse areas and pulse heights + for(int i=0; i<2; ++i) //loop over histograms with pulse areas and pulse heights { if(i==1) hist=(TH1F*)gDirectory->FindObjectAny(Form("crvCalibrationHistPulseArea_%zu",channel)); else hist=(TH1F*)gDirectory->FindObjectAny(Form("crvCalibrationHistPulseHeight_%zu",channel)); + hist->GetListOfFunctions()->Delete(); - double peakCalib=0; - if(!FindSPEpeak(hist, spectrum, peakCalib)) + double SPEpeak=-1; +// if(!FindSPEpeak(hist, spectrum, functions, SPEpeak, (i==0?minPeakPulseHeight:minPeakPulseArea))) + if(!FindSPEpeak(hist, spectrum, function, SPEpeak, (i==0?minPeakPulseHeight:minPeakPulseArea))) { calibValue[i]=-1; continue; } - - funcCalib.SetRange(peakCalib*fitRangeStart,peakCalib*fitRangeEnd); - if(hist->FindBin(peakCalib*fitRangeStart)==hist->FindBin(peakCalib*fitRangeEnd)) //fit range start/end are in the same bin - { - calibValue[i]=-1; - continue; - } - funcCalib.SetParameter(1,peakCalib); - hist->Fit(&funcCalib, "QR"); - calibValue[i]=funcCalib.GetParameter(1); + calibValue[i]=SPEpeak; } outputFile<Close(); } -bool FindSPEpeak(TH1F *hist, TSpectrum &spectrum, double &SPEpeak) +bool FindSPEpeak(TH1F *hist, TSpectrum &spectrum, TF1 &function, double &SPEpeak, double minPeak) +{ + if(hist->GetEntries()FindBin(x*fitRangeStart)==hist->FindBin(x*fitRangeEnd)) return false; //fit range start/end are in the same bin + function.SetRange(x*fitRangeStart,x*fitRangeEnd); + function.SetParameter(1,x); + hist->Fit(&function, "QR"); + SPEpeak = function.GetParameter(1); + + return true; +} + +/* +template +bool FindSPEpeak(TH1F *hist, TSpectrum &spectrum, std::array &functions, double &SPEpeak, double minPeak) { if(hist->GetEntries() > peaks; - for(int iPeak=0; iPeak &a, const std::pair &b) {return a.first fittedPeaks; + for(size_t iPeak=0; iPeakFindBin(x*fitRangeStart)==hist->FindBin(x*fitRangeEnd)) continue; //fit range start/end are in the same bin + functions[iPeak].SetRange(x*fitRangeStart,x*fitRangeEnd); + functions[iPeak].SetParameter(1,x); + hist->Fit(&functions[iPeak], "QR+"); + fittedPeaks.emplace_back(functions[iPeak].GetParameter(1)); + } + if(fittedPeaks.size()==0) return false; - int peakToUse=0; - if(nPeaks>1 && peaks[0].first>0) //if more than one peak is found, the first peak could be due to baseline fluctuations + int peakToUse=-1; + //only need to test two highest peaks (=first two entries in vector) + //one of the peaks could be due to baseline fluctuations + for(size_t iPeak=0; iPeakpeakRatioTolerance) peakToUse=1; //2nd peak is not twice the 1st peak, so the 1st peak is not the SPE peak - //assume that the 2nd peak is the SPE peak - //we have never seen that the 3rd peak was the SPE peak - no need to test it + if(fabs(fittedPeaks.at(jPeak)/fittedPeaks.at(iPeak)-2.0)