Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-12-17 09:23:58

0001 
0002 #include <micromegas/MicromegasCalibrationData.h>
0003 #include <micromegas/MicromegasMapping.h>
0004 
0005 #include <nlohmann/json.hpp>
0006 
0007 #include <Rtypes.h> // for R__LOAD_LIBRARY macro
0008 
0009 #include <format>
0010 #include <fstream>
0011 
0012 R__LOAD_LIBRARY(libmicromegas.so)
0013 
0014 void ConvertTpotCalibrationToJSon( int runnumber = 74871 )
0015 {
0016 
0017   const auto inputfile = std::format("TPOT_Pedestal-{:08}-0000.root", runnumber );
0018   const auto outputfile = std::format("TPOT_Pedestal-{:08}-0000.json", runnumber );
0019 
0020   std::cout << "EvaluateCalibration - inputfile: " << inputfile << std::endl;
0021   std::cout << "EvaluateCalibration - outputfile: " << outputfile << std::endl;
0022 
0023   MicromegasCalibrationData m_calibration_data;
0024   m_calibration_data.read(inputfile);
0025 
0026   const MicromegasMapping m_mapping;
0027 
0028   static const int nsigma = 5;
0029   static const bool use_maximum = true;
0030 
0031   nlohmann::json calib_list;
0032   // loop over available fees
0033   /// get list of fee ids
0034   for( const auto& fee_id:m_mapping.get_fee_id_list() )
0035   {
0036     double mean_pedestal = 0;
0037     double mean_rms = 0;
0038 
0039     double max_pedestal = 0;
0040     double max_rms = 0;
0041 
0042     int entries = 0;
0043 
0044     for( int ich = 0; ich < MicromegasDefs::m_nchannels_fee; ++ich )
0045     {
0046       const auto pedestal = m_calibration_data.get_pedestal( fee_id, ich );
0047       const auto rms = m_calibration_data.get_rms( fee_id, ich );
0048 
0049       if(!pedestal || pedestal<20) continue;
0050       if(!rms) continue;
0051 
0052       // need to comment out SCOZ - FEE = 8
0053       if( fee_id == 8 && ich > 128 ) continue;
0054 
0055       max_pedestal = std::max<double>(max_pedestal,pedestal);
0056       max_rms = std::max<double>(max_rms,rms);
0057 
0058       mean_pedestal += pedestal;
0059       mean_rms += rms;
0060       ++entries;
0061     }
0062 
0063     mean_pedestal/=entries;
0064     mean_rms/=entries;
0065 
0066     const double mean_threshold = mean_pedestal+nsigma*mean_rms;
0067     std::cout
0068       << "feid: " << fee_id
0069       << " pedestal: " << mean_pedestal
0070       << " rms: " << mean_rms
0071       << " threshold: " << mean_threshold
0072       << " value: " << int( 4.*mean_threshold )
0073       << std::endl;
0074 
0075     const double max_threshold = max_pedestal+nsigma*max_rms;
0076     std::cout
0077       << "feid: " << fee_id
0078       << " pedestal: " << max_pedestal
0079       << " rms: " << max_rms
0080       << " threshold: " << max_threshold
0081       << " value: " << int( 4.*max_threshold )
0082       << std::endl;
0083 
0084     // cable swap. This is a pain. I do not know how to address this
0085     const int fee_id_new = (fee_id == 11) ? 21:fee_id;
0086     nlohmann::json calibration = {
0087       {"fee_id", fee_id_new },
0088       {"threshold", int(4*std::round( use_maximum ? max_threshold:mean_threshold ))}
0089     };
0090 
0091     calib_list.emplace_back(calibration);
0092   }
0093 
0094   std::ofstream out( outputfile );
0095   out << std::setw(2) << calib_list << std::endl;
0096 }