00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "SimCalorimetry/HcalSimAlgos/interface/HPDNoiseLibraryReader.h"
00018
00019 using namespace edm;
00020 using namespace std;
00021
00022 HPDNoiseLibraryReader::HPDNoiseLibraryReader(const edm::ParameterSet& iConfig)
00023 : theNoiseRate(0), theNoisyPhi(0), theRandFlat(0), theRandGaussQ(0)
00024 {
00025 setRandomEngine();
00026
00027 ParameterSet pSet = iConfig.getParameter<edm::ParameterSet>("HPDNoiseLibrary");
00028 FileInPath filepath = pSet.getParameter<edm::FileInPath>("FileName");
00029 theHPDName = pSet.getUntrackedParameter<string> ("HPDName", "HPD");
00030 string pName = filepath.fullPath();
00031 if (pName.find(".") == 0) pName.erase(0,2);
00032 theReader = new HPDNoiseReader(pName);
00033 theNames = theReader->allNames();
00034
00035 fillRate();
00036 }
00037
00038 HPDNoiseLibraryReader::~HPDNoiseLibraryReader()
00039 {
00040 if (theRandFlat)delete theRandFlat;
00041 if (theRandGaussQ)delete theRandGaussQ;
00042 }
00043
00044 void HPDNoiseLibraryReader::initializeServices()
00045 {
00046 if (not edmplugin::PluginManager::isAvailable()) {
00047 edmplugin::PluginManager::configure(edmplugin::standard::config());
00048 }
00049
00050 std::string config =
00051 "process CorrNoise = {"
00052 "service = RandomNumberGeneratorService"
00053 "{"
00054 "untracked uint32 sourceSeed = 123456789"
00055 "}"
00056 "}";
00057
00058
00059 edm::ServiceToken tempToken = edm::ServiceRegistry::createServicesFromConfig(config);
00060
00061
00062 edm::ServiceRegistry::Operate operate(tempToken);
00063 }
00064 void HPDNoiseLibraryReader::setRandomEngine()
00065 {
00066 edm::Service < edm::RandomNumberGenerator > rng;
00067 if (!rng.isAvailable()) {
00068 throw cms::Exception("Configuration") << "HcalHPDNoiseLibrary requires the RandomNumberGeneratorService\n"
00069 "which is not present in the configuration file. You must add the service\n"
00070 "in the configuration file or remove the modules that require it.";
00071 }
00072 setRandomEngine(rng->getEngine());
00073 }
00074 void HPDNoiseLibraryReader::setRandomEngine(CLHEP::HepRandomEngine & engine)
00075 {
00076 if(theRandGaussQ) delete theRandGaussQ;
00077 if(theRandFlat) delete theRandFlat;
00078 theRandGaussQ = new CLHEP::RandGaussQ(engine);
00079 theRandFlat = new CLHEP::RandFlat(engine);
00080 }
00081
00082 void HPDNoiseLibraryReader::fillRate()
00083 {
00084 for (size_t i = 0; i < theNames.size(); ++i) {
00085 HPDNoiseReader::Handle hpdObj = theReader->getHandle(theNames[i]);
00086 if (theReader->valid(hpdObj)) {
00087 theNoiseRate.push_back(theReader->rate(hpdObj));
00088 } else {
00089 std::cerr << "HPD Handle Object is not valid!" << endl;
00090 }
00091 }
00092 }
00093
00094 HPDNoiseData* HPDNoiseLibraryReader::getNoiseData(int iphi)
00095 {
00096
00097
00098 HPDNoiseData* data;
00099
00100
00101
00102 if (!(applyNoise(iphi))) return data;
00103
00104 int zside = 1;
00105 if(iphi>72){
00106 iphi = iphi-72;
00107 zside = -1;
00108 }
00109 std::string name;
00110 if (zside == 1) {
00111 name = "ZPlus" + theHPDName + itos(iphi);
00112 } else if (zside == -1){
00113 name = "ZMinus" + theHPDName + itos(iphi);
00114 }else {
00115 cerr << " ZSide Calculation Error." << endl;
00116 }
00117 HPDNoiseReader::Handle hpdObj = theReader->getHandle(name);
00118 if (theReader->valid(hpdObj)) {
00119
00120 unsigned long entry = theRandFlat->fireInt( theReader->totalEntries(hpdObj));
00121 theReader->getEntry(hpdObj, entry, &data);
00122 }else{
00123 std::cerr << " HPD Name in the library is not valid." << std::endl;
00124 }
00125 return data;
00126 }
00127
00128
00129 void HPDNoiseLibraryReader::getNoisyPhis()
00130 {
00131
00132 clearPhi();
00133 double rndm[144];
00134
00135 theRandFlat->shootArray(144, rndm);
00136
00137 for (int i = 0; i < 144; ++i) {
00138 if (rndm[i] < theNoiseRate[i]) {
00139 theNoisyPhi.push_back(i + 1);
00140 }
00141 }
00142 }
00143
00144 void HPDNoiseLibraryReader::getBiasedNoisyPhis()
00145 {
00146
00147 clearPhi();
00148 double rndm[144];
00149
00150 theRandFlat->shootArray(144, rndm);
00151 for (int i = 0; i < 144; ++i) {
00152 if (rndm[i] < theNoiseRate[i]) {
00153 theNoisyPhi.push_back(i + 1);
00154 }
00155 }
00156
00157 if (theNoisyPhi.size() == 0) {
00158 int iPhi = (theRandFlat->fireInt(144)) + 1;
00159
00160 theNoisyPhi.push_back(iPhi);
00161 }
00162 }
00163
00164 vector<pair <HcalDetId, const float* > > HPDNoiseLibraryReader::getNoisyHcalDetIds()
00165 {
00166
00167 vector< pair<HcalDetId, const float* > > result;
00168
00169
00170 getNoisyPhis();
00171 for (int i = 0; i < int(theNoisyPhi.size()); ++i) {
00172 int iphi = theNoisyPhi[i];
00173 HPDNoiseData* data;
00174 data = getNoiseData(iphi);
00175 for(unsigned int i=0; i<data->size();++i)
00176 {
00177 pair < HcalDetId, const float* >tmp_pair( data->getDataFrame(i).id(), data->getDataFrame(i).getFrame());
00178 result.push_back(tmp_pair);
00179 }
00180 }
00181 return result;
00182 }
00183
00184 vector<pair <HcalDetId, const float* > > HPDNoiseLibraryReader::getBiasedNoisyHcalDetIds()
00185 {
00186
00187 vector< pair<HcalDetId, const float* > > result;
00188
00189
00190
00191 getBiasedNoisyPhis();
00192 for (int i = 0; i < int(theNoisyPhi.size()); ++i) {
00193 int iphi = theNoisyPhi[i];
00194 HPDNoiseData* data;
00195 data = getNoiseData(iphi);
00196 for(unsigned int i=0; i<data->size();++i)
00197 {
00198 pair < HcalDetId, const float* >tmp_pair( data->getDataFrame(i).id(), data->getDataFrame(i).getFrame());
00199 result.push_back(tmp_pair);
00200 }
00201 }
00202 return result;
00203 }
00204
00205 bool HPDNoiseLibraryReader::applyNoise(int iphi)
00206 {
00207
00208 bool isAccepted = false;
00209 vector < int >::iterator phi_iter;
00210 phi_iter = find(theNoisyPhi.begin(), theNoisyPhi.end(), iphi);
00211 if (phi_iter != theNoisyPhi.end()){
00212 isAccepted = true;
00213 }
00214 return isAccepted;
00215 }
00216 string HPDNoiseLibraryReader::itos(int i)
00217 {
00218 stringstream s;
00219 s << i;
00220 return s.str();
00221 }
00222
00223 void HPDNoiseLibraryReader::clearPhi() {
00224 theNoisyPhi.clear();
00225 }