CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/SimG4CMS/Calo/src/CaloMeanResponse.cc

Go to the documentation of this file.
00001 #include "SimG4CMS/Calo/interface/CaloMeanResponse.h"
00002 #include "FWCore/Utilities/interface/Exception.h"
00003 
00004 #include "CLHEP/Units/GlobalSystemOfUnits.h"
00005 
00006 #include <iostream>
00007 #include <fstream>
00008 #include <iomanip>
00009 
00010 //#define DebugLog
00011 
00012 CaloMeanResponse::CaloMeanResponse(edm::ParameterSet const & p) {
00013 
00014   edm::ParameterSet m_p = p.getParameter<edm::ParameterSet>("CaloResponse");
00015   edm::FileInPath fp    = m_p.getParameter<edm::FileInPath>("ResponseFile");
00016   std::string fName     = fp.fullPath();
00017   useTable              = m_p.getParameter<bool>("UseResponseTable");
00018   scale                 = m_p.getParameter<double>("ResponseScale");
00019   edm::LogInfo("CaloSim") << "CaloMeanResponse initialized with scale " 
00020                           << scale << " and use Table " << useTable 
00021                           << " from file " << fName;
00022 
00023   readResponse (fName);
00024 }
00025 
00026 CaloMeanResponse::~CaloMeanResponse() {}
00027 
00028 double CaloMeanResponse::getWeight(int genPID, double genP) {
00029   double weight = 1;
00030   bool   found=false;
00031   for (unsigned int i=0; i<pionTypes.size(); i++) {
00032     if (genPID == pionTypes[i]) {
00033       found = true;
00034       break;
00035     }
00036   }
00037   if (found) {
00038     weight = scale;
00039     if (useTable) {
00040       if (piLast >= 0) weight *= pionTable[piLast];
00041       for (unsigned int i=0; i<pionTable.size(); i++) {
00042         if (genP < pionMomentum[i]) {
00043           if (i == 0) weight = scale*pionTable[i];
00044           else        weight = scale*((pionTable[i-1]*(pionMomentum[i]-genP)+
00045                                        pionTable[i]*(genP-pionMomentum[i-1]))/
00046                                       (pionMomentum[i]-pionMomentum[i-1]));
00047           break;
00048         }
00049       }
00050     }
00051 #ifdef DebugLog
00052     LogDebug("CaloSim") << "CaloMeanResponse::getWeight PID " << genPID
00053                         << " uses pion list and gets weight " << weight
00054                         << " for momentum " << genP/GeV << " GeV/c";
00055 #endif
00056   } else {
00057     for (unsigned int i=0; i<protonTypes.size(); i++) {
00058       if (genPID == protonTypes[i]) {
00059         found = true;
00060         break;
00061       }
00062     }
00063     if (found) {
00064       weight = scale;
00065       if (useTable) {
00066         if (pLast >= 0) weight *= protonTable[pLast];
00067         for (unsigned int i=0; i<protonTable.size(); i++) {
00068           if (genP < protonMomentum[i]) {
00069             if (i == 0) weight = scale*protonTable[i];
00070             else        weight = scale*((protonTable[i-1]*(protonMomentum[i]-genP)+
00071                                          protonTable[i]*(genP-protonMomentum[i-1]))/
00072                                         (protonMomentum[i]-protonMomentum[i-1]));
00073             break;
00074           }
00075         }
00076       }
00077 #ifdef DebugLog
00078       LogDebug("CaloSim") << "CaloMeanResponse::getWeight PID " << genPID
00079                           << " uses proton list and gets weight " << weight
00080                           << " for momentum " << genP/GeV << " GeV/c";
00081 #endif
00082     } else {
00083 #ifdef DebugLog
00084       LogDebug("CaloSim") << "CaloMeanResponse::getWeight PID " << genPID
00085                           << " is not in either lists and weight " << weight;
00086 #endif
00087     }
00088   }
00089   return weight;
00090 }
00091 
00092 void CaloMeanResponse::readResponse (std::string fName) {
00093 
00094   std::ifstream infile;
00095   infile.open(fName.c_str(), std::ios::in);
00096 
00097   if (infile) {
00098     int    nene, npart, pid;
00099     double ene, responseData, responseMC, ratio;
00100 
00101     // First read the pion data
00102     infile >> nene >> npart;
00103     for (int i=0; i<npart; i++) {
00104       infile >> pid;
00105       pionTypes.push_back(pid);
00106     }
00107     for (int i=0; i<nene; i++) {
00108       infile >> ene >> responseData >> responseMC;
00109       if (responseMC > 0) ratio = responseData/responseMC;
00110       else                ratio = 1;
00111       pionMomentum.push_back(ene*GeV);
00112       pionTable.push_back(ratio);
00113     }
00114 
00115     // Then read the proton data
00116     infile >> nene >> npart;
00117     for (int i=0; i<npart; i++) {
00118       infile >> pid;
00119       protonTypes.push_back(pid);
00120     }
00121     for (int i=0; i<nene; i++) {
00122       infile >> ene >> responseData >> responseMC;
00123       if (responseMC > 0) ratio = responseData/responseMC;
00124       else                ratio = 1;
00125       protonMomentum.push_back(ene*GeV);
00126       protonTable.push_back(ratio);
00127     }
00128     infile.close();
00129   }
00130 
00131   piLast = (int)(pionTable.size()) - 1;
00132   pLast  = (int)(protonTable.size()) - 1;
00133 #ifdef DebugLog
00134   LogDebug("CaloSim") << "CaloMeanResponse::readResponse finds "
00135                       << pionTypes.size() << " particles to use pion response"
00136                       << " map with a table of " << pionTable.size() 
00137                       << " data points " << piLast;
00138   for (unsigned int i=0; i<pionTypes.size(); i++) 
00139     LogDebug("CaloSim") << "Particle ID[" << i << "] = " << pionTypes[i];
00140   for (unsigned int i=0; i<pionTable.size(); i++) 
00141     LogDebug("CaloSim") << "Momentum[" << i << "] (" << pionMomentum[i]/GeV
00142                         << " GeV/c) --> " << pionTable[i];
00143   LogDebug("CaloSim") << "CaloMeanResponse::readResponse finds "
00144                       << protonTypes.size() << " particles to use proton "
00145                       << "response map with a table of " << protonTable.size()
00146                       << " data points " << pLast;
00147   for (unsigned int i=0; i<protonTypes.size(); i++) 
00148     LogDebug("CaloSim") << "Particle ID[" << i << "] = " << protonTypes[i];
00149   for (unsigned int i=0; i<protonTable.size(); i++) 
00150     LogDebug("CaloSim") << "Momentum[" << i << "] (" << protonMomentum[i]/GeV
00151                         << " GeV/c) --> " << protonTable[i];
00152 #endif
00153 }