#include "Calibrator.h" #include "TFile.h" #include "TTree.h" #include "DataStructs.h" #include #include #include "TSpectrum.h" namespace SabreCal { Calibrator::Calibrator(const std::string& gainfile) : m_gainMap(gainfile) { TH1::AddDirectory(kFALSE); } Calibrator::~Calibrator() {} void Calibrator::Run(const std::string& datafile, const std::string& outputfile, const std::string& plotfile) { if(!m_gainMap.IsValid()) { std::cerr<<"ERR -- Bad gain map at Calibrator::Run!"<IsOpen()) { std::cerr<<"ERR -- Bad data file "<Get("Data"); uint16_t energy, board, channel; tree->SetBranchAddress("Energy", &energy); tree->SetBranchAddress("Board", &board); tree->SetBranchAddress("Channel", &channel); int gchan; uint64_t nentries = tree->GetEntries(); float flush_percent = 0.1f; uint64_t count=0, flush_count=0, flush_val=nentries*flush_percent; std::vector histograms; for(int i=0; iGetEntry(i); count++; if(count == flush_val) { flush_count++; count = 0; std::cout<<"\rPercent of data processed: "< 100.0) //Reject electronic noise histograms[i].Fill(params.slope*(energy*s_gainFactor) + params.offset); } std::cout<Close(); std::cout<<"Finding calibration parameters..."<& grams) { TSpectrum finder; double threshold = 0.5; double sigma = 5.0; Parameters these_params; double peakMean; for(auto& histo : grams) { finder.Search(&histo, sigma, "nobackground", threshold); if(finder.GetNPeaks() == 0) { std::cerr<<"No peaks found in histogram "< 1) { peakMean = 0.0; double peakAmp = 0.0; std::cerr<<"Multiple peaks found in histogram "< peakAmp) { peakMean = mean; peakAmp = amp; } } these_params.slope = s_241Am_alphaKE/peakMean; these_params.offset = 0.0; m_params.push_back(these_params); } else { peakMean = finder.GetPositionX()[0]; these_params.slope = s_241Am_alphaKE/peakMean; these_params.offset = 0.0; m_params.push_back(these_params); } } } void Calibrator::WriteHistograms(const std::vector &grams, const std::string &plotfile) { TFile* output = TFile::Open(plotfile.c_str(), "RECREATE"); if(!output->IsOpen()) { std::cerr<<"Unable to open file "<Close(); } void Calibrator::WriteParameters(const std::string &outputfile) { std::ofstream output(outputfile); if(!output.is_open()) { std::cerr<<"Unable to open file "<