diff --git a/NPLib/Tools/CalibrationManager.cxx b/NPLib/Tools/CalibrationManager.cxx index 315b8b999a4502180cbefaa1e79a99105a44e0f9..d57297e504ab006a3878f6cc13054c19226c5d0a 100644 --- a/NPLib/Tools/CalibrationManager.cxx +++ b/NPLib/Tools/CalibrationManager.cxx @@ -33,59 +33,59 @@ CalibrationManager* CalibrationManager::instance = 0; CalibrationManager* CalibrationManager::getInstance(string configFileName) - { - // A new instance of CalibrationManager is created if it does not exist: - if (instance == 0) { - instance = new CalibrationManager(configFileName); - } +{ + // A new instance of CalibrationManager is created if it does not exist: + if (instance == 0) { + instance = new CalibrationManager(configFileName); + } + + // The instance of CalibrationManager is returned: + return instance; +} - // The instance of CalibrationManager is returned: - return instance; - } - ////////////////////////////////////////////////////////////////// CalibrationManager::CalibrationManager(string configFileName) { - // Read configuration file Buffer - string lineBuffer, dataBuffer; - - // Open file - ifstream inputConfigFile; - inputConfigFile.open(configFileName.c_str()); - - cout << endl; - cout << "/////////// Calibration Information ///////////" << endl; - cout << "Getting list of Calibration File" << endl; - - if (!inputConfigFile) { - cout << "Calibration Path file :" << configFileName << " not found " << endl; - return; - } - - else { - cout << "Reading list of file from :" << configFileName << endl; - while (!inputConfigFile.eof()) { - getline(inputConfigFile, lineBuffer); - - // search for token giving the list of Root files to treat - if ( lineBuffer.compare(0, 19, "CalibrationFilePath") == 0 ) { - while (!inputConfigFile.eof()) { - inputConfigFile >> dataBuffer; - - // ignore comment Line - if (dataBuffer.compare(0, 1, "%") == 0) { - inputConfigFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); - } - - else if (!inputConfigFile.eof()) { - AddFile(dataBuffer); - cout << "Adding file " << dataBuffer << " to Calibration" << endl; - } - } - } + // Read configuration file Buffer + string lineBuffer, dataBuffer; + + // Open file + ifstream inputConfigFile; + inputConfigFile.open(configFileName.c_str()); + + cout << endl; + cout << "/////////// Calibration Information ///////////" << endl; + cout << "Getting list of Calibration File" << endl; + + if (!inputConfigFile) { + cout << "Calibration Path file :" << configFileName << " not found " << endl; + return; + } + + else { + cout << "Reading list of file from :" << configFileName << endl; + while (!inputConfigFile.eof()) { + getline(inputConfigFile, lineBuffer); + + // search for token giving the list of Root files to treat + if ( lineBuffer.compare(0, 19, "CalibrationFilePath") == 0 ) { + while (!inputConfigFile.eof()) { + inputConfigFile >> dataBuffer; + + // ignore comment Line + if (dataBuffer.compare(0, 1, "%") == 0) { + inputConfigFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); + } + + else if (!inputConfigFile.eof()) { + AddFile(dataBuffer); + cout << "Adding file " << dataBuffer << " to Calibration" << endl; + } + } } - } - cout << "/////////////////////////////////" << endl; + } + } + cout << "/////////////////////////////////" << endl; } ////////////////////////////////////////////////////////////////// @@ -95,245 +95,266 @@ CalibrationManager::~CalibrationManager() ////////////////////////////////////////////////////////////////// bool CalibrationManager::AddParameter(string DetectorName , string ParameterName , string Token ) { - string ParameterPath = DetectorName + "/" + ParameterName ; - fToken[Token] = ParameterPath ; - return true; + string ParameterPath = DetectorName + "/" + ParameterName ; + fToken[Token] = ParameterPath ; + return true; } ///////////////////////////////////////////////////////////////// void CalibrationManager::ClearCalibration() { - fCalibrationCoeff.clear(); + fCalibrationCoeff.clear(); } ///////////////////////////////////////////////////////////////// vector<double> CalibrationManager::GetCorrection(const string& ParameterPath) { - vector<double> Coeff ; - map< string , vector<double> >::iterator it ; - it = fCalibrationCoeff.find(ParameterPath) ; + vector<double> Coeff ; + map< string , vector<double> >::iterator it ; + it = fCalibrationCoeff.find(ParameterPath) ; - if(it == fCalibrationCoeff.end() ) - { - /* cout << "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX " << endl ; - cout << " ERROR: PARAMETER " << ParameterPath << " IS NOT FOUND IN THE CALIBRATION DATA BASE " << endl ; - cout << "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX " << endl ;*/ + if(it == fCalibrationCoeff.end() ) + { + /* cout << "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX " << endl ; + cout << " ERROR: PARAMETER " << ParameterPath << " IS NOT FOUND IN THE CALIBRATION DATA BASE " << endl ; + cout << "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX " << endl ;*/ - return Coeff ; - } + return Coeff ; + } - Coeff = it->second ; + Coeff = it->second ; - return(Coeff); + return(Coeff); } ////////////////////////////////////////////////////////////////// void CalibrationManager::LoadParameterFromFile() { - ifstream CalibFile ; - string DataBuffer ; - string LineBuffer ; - - // Get pointer to the TAsciifile CalibrationFile in RootOuput - TAsciiFile* AcsiiCalibration = RootOutput::getInstance()->GetAsciiFileCalibration(); - - - for(unsigned int i = 0 ; i < fFileList.size() ; i++) - { - CalibFile.open( fFileList[i].c_str() ); - map<string,string>::iterator it ; - - if(!CalibFile) - { - cout << "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX " << endl ; - cout << " WARNING: FILE " << fFileList[i] << " IS MISSING " << endl ; - cout << "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX " << endl ; - } - - else - { - // Append the Calibration File to the RootOuput for Back-up - string comment = "%%% From File " + fFileList[i] + "%%%"; - AcsiiCalibration->AppendLine(comment.c_str()); - AcsiiCalibration->Append(fFileList[i].c_str()); - - - while( !CalibFile.eof() ) - { - // Read the file Line by line - getline(CalibFile, LineBuffer); - - // Create a istringstream to manipulate the line easely - istringstream theLine (LineBuffer,istringstream::in); - theLine >> DataBuffer ; - - // Comment support, comment symbole is % - if(DataBuffer.compare(0, 1, "%") == 0) { - CalibFile.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );} - - // Search word in the token list - it=fToken.find(DataBuffer); - // if the word is find, values are read - if( it!=fToken.end() ) - { - vector<double> Coeff ; - while( theLine >> DataBuffer ){ - Coeff.push_back( atof(DataBuffer.c_str()) ) ; - } - - // Check this parameter is not already define - if( fCalibrationCoeff.find(it->second) != fCalibrationCoeff.end() ) - cout << "WARNING: Parameter " << it->second << " Already found. It will be rewritted " << endl; - - // Add the list of Coeff to the Coeff map using Parameter Path as index - fCalibrationCoeff[ it->second ] = Coeff ; - } - - } - - } - - - CalibFile.close() ; - } - } + ifstream CalibFile ; + string DataBuffer ; + string LineBuffer ; + + // Get pointer to the TAsciifile CalibrationFile in RootOuput + TAsciiFile* AcsiiCalibration = RootOutput::getInstance()->GetAsciiFileCalibration(); + + + for(unsigned int i = 0 ; i < fFileList.size() ; i++) + { + CalibFile.open( fFileList[i].c_str() ); + map<string,string>::iterator it ; + + if(!CalibFile) + { + cout << "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX " << endl ; + cout << " WARNING: FILE " << fFileList[i] << " IS MISSING " << endl ; + cout << "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX " << endl ; + } + + else + { + // Append the Calibration File to the RootOuput for Back-up + string comment = "%%% From File " + fFileList[i] + "%%%"; + AcsiiCalibration->AppendLine(comment.c_str()); + AcsiiCalibration->Append(fFileList[i].c_str()); + + + while( !CalibFile.eof() ) + { + // Read the file Line by line + getline(CalibFile, LineBuffer); + + // Create a istringstream to manipulate the line easely + istringstream theLine (LineBuffer,istringstream::in); + theLine >> DataBuffer ; + + // Comment support, comment symbole is % + if(DataBuffer.compare(0, 1, "%") == 0) { + CalibFile.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );} + + // Search word in the token list + it=fToken.find(DataBuffer); + // if the word is find, values are read + if( it!=fToken.end() ) + { + vector<double> Coeff ; + while( theLine >> DataBuffer ){ + Coeff.push_back( atof(DataBuffer.c_str()) ) ; + } + + // Check this parameter is not already define + if( fCalibrationCoeff.find(it->second) != fCalibrationCoeff.end() ) + cout << "WARNING: Parameter " << it->second << " Already found. It will be rewritted " << endl; + + // Add the list of Coeff to the Coeff map using Parameter Path as index + fCalibrationCoeff[ it->second ] = Coeff ; + } + + } + + } + + + CalibFile.close() ; + } +} ////////////////////////////////////////////////////////////////// -double CalibrationManager::ApplyCalibration(const string& ParameterPath , const double& RawValue) - { - map< string , vector<double> >::iterator it ; - - // Find the good parameter in the Map - // Using Find method of stl is the fastest way - it = fCalibrationCoeff.find(ParameterPath) ; - - // If the find methods return the end iterator it's mean the parameter was not found - if(it == fCalibrationCoeff.end() ) - { - /* cout << "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX " << endl ; - cout << " ERROR: PARAMETER " << ParameterPath << " IS NOT FOUND IN THE CALIBRATION DATA BASE " << endl ; - cout << "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX " << endl; -exit(1); */ - - return RawValue ; - } - - // Else we take the second part of the element (first is index, ie: parameter path) - // Second is the vector of Coeff - vector<double> Coeff = it->second ; - - // The vector size give the degree of calibration - // We just apply the coeff and returned the calibrated value - - double CalibratedValue = 0 ; - for(unsigned int i = 0 ; i < Coeff.size() ; i++) - { - CalibratedValue += Coeff[i]*pow(RawValue, (double)i); - } - - return CalibratedValue ; - - } +double CalibrationManager::ApplyCalibration(const string& ParameterPath , const double& RawValue){ + map< string , vector<double> >::iterator it ; + + // Find the good parameter in the Map + // Using Find method of stl is the fastest way + it = fCalibrationCoeff.find(ParameterPath) ; + + // If the find methods return the end iterator it's mean the parameter was not found + if(it == fCalibrationCoeff.end() ){ + return RawValue ; + } + // Else we take the second part of the element (first is index, ie: parameter path) + // Second is the vector of Coeff + vector<double> Coeff = it->second ; + + // The vector size give the degree of calibration + // We just apply the coeff and returned the calibrated value + + double CalibratedValue = 0 ; + for(unsigned int i = 0 ; i < Coeff.size() ; i++){ + CalibratedValue += Coeff[i]*pow(RawValue, (double)i); + } + + return CalibratedValue ; + +} +////////////////////////////////////////////////////////////////// +double CalibrationManager::ApplyCalibrationDebug(const string& ParameterPath , const double& RawValue){ + map< string , vector<double> >::iterator it ; + + // Find the good parameter in the Map + // Using Find method of stl is the fastest way + it = fCalibrationCoeff.find(ParameterPath) ; + + // If the find methods return the end iterator it's mean the parameter was not found + if(it == fCalibrationCoeff.end() ){ + cout << " PARAMETER " << ParameterPath << " IS NOT FOUND IN THE CALIBRATION DATA BASE " << endl ; + return RawValue ; + } + + // Else we take the second part of the element (first is index, ie: parameter path) + // Second is the vector of Coeff + cout << it->first << " : raw = " << RawValue << " coeff = " ; + vector<double> Coeff = it->second ; + + // The vector size give the degree of calibration + // We just apply the coeff and returned the calibrated value + + double CalibratedValue = 0 ; + for(unsigned int i = 0 ; i < Coeff.size() ; i++){ + cout << Coeff[i] << " " ; + CalibratedValue += Coeff[i]*pow(RawValue, (double)i); + } +cout << endl ; + return CalibratedValue ; +} ////////////////////////////////////////////////////////////////// double CalibrationManager::ApplyResistivePositionCalibration(const string& ParameterPath , const double& DeltaRawValue){ - map< string , vector<double> >::iterator it ; - - // Find the good parameter in the Map - // Using Find method of stl is the fastest way - it = fCalibrationCoeff.find(ParameterPath) ; - - // If the find methods return the end iterator it's mean the parameter was not found - if(it == fCalibrationCoeff.end() ) - { - /* cout << "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX " << endl ; - cout << " ERROR: PARAMETER " << ParameterPath << " IS NOT FOUND IN THE CALIBRATION DATA BASE " << endl ; - cout << "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX " << endl; -exit(1); */ - - return DeltaRawValue ; - } - - // Else we take the second part of the element (first is index, ie: parameter path) - // Second is the vector of Coeff - vector<double> Coeff = it->second ; - - // Check that the number of coeff is ok - if(Coeff.size()!=2) return DeltaRawValue ; - - double CalibratedValue = (DeltaRawValue-Coeff[0])/(Coeff[1]-Coeff[0]) ; - return CalibratedValue ; - - } + map< string , vector<double> >::iterator it ; + + // Find the good parameter in the Map + // Using Find method of stl is the fastest way + it = fCalibrationCoeff.find(ParameterPath) ; + + // If the find methods return the end iterator it's mean the parameter was not found + if(it == fCalibrationCoeff.end() ) + { + /* cout << "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX " << endl ; + cout << " ERROR: PARAMETER " << ParameterPath << " IS NOT FOUND IN THE CALIBRATION DATA BASE " << endl ; + cout << "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX " << endl; + exit(1); */ + + return DeltaRawValue ; + } + + // Else we take the second part of the element (first is index, ie: parameter path) + // Second is the vector of Coeff + vector<double> Coeff = it->second ; + + // Check that the number of coeff is ok + if(Coeff.size()!=2) return DeltaRawValue ; + + double CalibratedValue = (DeltaRawValue-Coeff[0])/(Coeff[1]-Coeff[0]) ; + return CalibratedValue ; + +} ////////////////////////////////////////////////////////////////// bool CalibrationManager::ApplyThreshold(const string& ParameterPath, const double& RawValue) { - map< string , vector<double> >::iterator it ; + map< string , vector<double> >::iterator it ; - // Find the good parameter in the Map - // Using Find method of stl is the fastest way - it = fCalibrationCoeff.find(ParameterPath) ; + // Find the good parameter in the Map + // Using Find method of stl is the fastest way + it = fCalibrationCoeff.find(ParameterPath) ; - // If the find methods return the end iterator it's mean the parameter was not found - if(it == fCalibrationCoeff.end() ) - { - // cout << "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX " << endl ; - // cout << " ERROR: PARAMETER " << ParameterPath << " IS NOT FOUND IN THE CALIBRATION DATA BASE " << endl ; - // cout << "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX " << endl ; - return false; - } + // If the find methods return the end iterator it's mean the parameter was not found + if(it == fCalibrationCoeff.end() ) + { + // cout << "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX " << endl ; + // cout << " ERROR: PARAMETER " << ParameterPath << " IS NOT FOUND IN THE CALIBRATION DATA BASE " << endl ; + // cout << "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX " << endl ; + return false; + } - // Else we take the second part of the element (first is index, ie: parameter path) - // Second is the vector of Coeff - vector<double> Coeff = it->second ; + // Else we take the second part of the element (first is index, ie: parameter path) + // Second is the vector of Coeff + vector<double> Coeff = it->second ; - // The vector size give the degree of calibration - // We just apply the coeff and returned the calibrated value + // The vector size give the degree of calibration + // We just apply the coeff and returned the calibrated value - double ThresholdValue = 0 ; + double ThresholdValue = 0 ; - if(Coeff.size()==2){ThresholdValue = Coeff[0] + 3*Coeff[1];} + if(Coeff.size()==2){ThresholdValue = Coeff[0] + 3*Coeff[1];} - if(RawValue > ThresholdValue) - { - return true; - } - else return false; + if(RawValue > ThresholdValue) + { + return true; + } + else return false; } ///////////////////////////////////////////////////////////////////////////////////////////// double CalibrationManager::GetPedestal(const string& ParameterPath) { - map< string , vector<double> >::iterator it ; + map< string , vector<double> >::iterator it ; - // Find the good parameter in the Map - // Using Find method of stl is the fastest way - it = fCalibrationCoeff.find(ParameterPath) ; + // Find the good parameter in the Map + // Using Find method of stl is the fastest way + it = fCalibrationCoeff.find(ParameterPath) ; - // If the find methods return the end iterator it's mean the parameter was not found - if(it == fCalibrationCoeff.end() ) - { - cout << "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX " << endl ; - cout << " ERROR: PARAMETER " << ParameterPath << " IS NOT FOUND IN THE CALIBRATION DATA BASE " << endl ; - cout << "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX " << endl ; - } + // If the find methods return the end iterator it's mean the parameter was not found + if(it == fCalibrationCoeff.end() ) + { + cout << "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX " << endl ; + cout << " ERROR: PARAMETER " << ParameterPath << " IS NOT FOUND IN THE CALIBRATION DATA BASE " << endl ; + cout << "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX " << endl ; + } - // Else we take the second part of the element (first is index, ie: parameter path) - // Second is the vector of Coeff - vector<double> Coeff = it->second ; + // Else we take the second part of the element (first is index, ie: parameter path) + // Second is the vector of Coeff + vector<double> Coeff = it->second ; - // The vector size give the degree of calibration - // We just apply the coeff and returned the calibrated value + // The vector size give the degree of calibration + // We just apply the coeff and returned the calibrated value - double PedestalValue = 0 ; + double PedestalValue = 0 ; - if(Coeff.size()==2){PedestalValue = Coeff[0];} + if(Coeff.size()==2){PedestalValue = Coeff[0];} - return PedestalValue; + return PedestalValue; } diff --git a/NPLib/Tools/CalibrationManager.h b/NPLib/Tools/CalibrationManager.h index df2c0a6052da7b68523db215e10a9fcaf8576587..5166ee82a4f19bd95e64c10fff118ea08e305bcd 100644 --- a/NPLib/Tools/CalibrationManager.h +++ b/NPLib/Tools/CalibrationManager.h @@ -59,6 +59,8 @@ class CalibrationManager // call like : myCalibrationManager->ApplyCalibration( "MUST2/Telescope5_Si_X38_E" , RawEnergy ) // return the Calibrated value double ApplyCalibration(const string& ParameterPath , const double& RawValue); + // Same but with debug information outputs + double ApplyCalibrationDebug(const string& ParameterPath , const double& RawValue); double ApplyResistivePositionCalibration(const string& ParameterPath , const double& RawValue); bool ApplyThreshold(const string& ParameterPath, const double& RawValue); double GetPedestal(const string& ParameterPath);