CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/CondTools/Hcal/plugins/HcalPedestalsCheck.cc

Go to the documentation of this file.
00001 #include "CondTools/Hcal/interface/HcalPedestalsCheck.h"
00002 
00003 HcalPedestalsCheck::HcalPedestalsCheck(edm::ParameterSet const& ps)
00004 {
00005   outfile = ps.getUntrackedParameter<std::string>("outFile","null");
00006   dumprefs = ps.getUntrackedParameter<std::string>("dumpRefPedsTo","null");
00007   dumpupdate = ps.getUntrackedParameter<std::string>("dumpUpdatePedsTo","null");
00008   checkemapflag = ps.getUntrackedParameter<bool>("checkEmap",true);
00009   validatepedestalsflag = ps.getUntrackedParameter<bool>("validatePedestals",false);
00010   epsilon = ps.getUntrackedParameter<double>("deltaP",0);
00011 }
00012 
00013 HcalPedestalsCheck::~HcalPedestalsCheck()
00014 {
00015 }
00016 
00017 void HcalPedestalsCheck::analyze(const edm::Event& ev, const edm::EventSetup& es)
00018 {
00019   using namespace edm::eventsetup;
00020 
00021   // get fake pedestals from file ("new pedestals")
00022   edm::ESHandle<HcalPedestals> newPeds;
00023   es.get<HcalPedestalsRcd>().get("update",newPeds);
00024   const HcalPedestals* myNewPeds = newPeds.product();
00025 
00026   // get DB pedestals from Frontier/OrcoX ("reference")
00027   edm::ESHandle<HcalPedestals> refPeds;
00028   es.get<HcalPedestalsRcd>().get("reference",refPeds);
00029   const HcalPedestals* myRefPeds = refPeds.product();
00030 
00031   // get e-map from reference
00032   edm::ESHandle<HcalElectronicsMap> refEMap;
00033   es.get<HcalElectronicsMapRcd>().get("reference",refEMap);
00034   const HcalElectronicsMap* myRefEMap = refEMap.product();
00035 
00036   // dump pedestals:
00037   if(!(dumprefs.compare("null")==0)){
00038     std::ofstream outStream(dumprefs.c_str());
00039     std::cout << "--- Dumping Pedestals - reference ---" << std::endl;
00040     HcalDbASCIIIO::dumpObject (outStream, (*myRefPeds) );
00041   }
00042   if(!(dumpupdate.compare("null")==0)){
00043     std::ofstream outStream2(dumpupdate.c_str());
00044     std::cout << "--- Dumping Pedestals - updated ---" << std::endl;
00045     HcalDbASCIIIO::dumpObject (outStream2, (*myNewPeds) );
00046   }
00047 
00048   if(validatepedestalsflag){
00049     std::vector<DetId> listNewChan = myNewPeds->getAllChannels();
00050     std::vector<DetId> listRefChan = myRefPeds->getAllChannels();
00051     std::vector<DetId>::iterator cell;
00052     bool failflag = false;
00053     for (std::vector<DetId>::iterator it = listRefChan.begin(); it != listRefChan.end(); it++)
00054       {
00055         DetId mydetid = *it;
00056         cell = std::find(listNewChan.begin(), listNewChan.end(), mydetid);
00057         if (cell == listNewChan.end()) // not present in new list, take old pedestals
00058           {
00059             throw cms::Exception("DataDoesNotMatch") << "Channel missing";
00060             failflag = true;
00061             break;
00062           }
00063         else // present in new list, take new pedestals
00064           {
00065             const float* values = (myNewPeds->getValues( mydetid ))->getValues();
00066             const float* oldvalue = (myRefPeds->getValues( mydetid ))->getValues();
00067             if( (*oldvalue != *values) || (*(oldvalue + 1)!=*(values+1)) || (*(oldvalue + 2)!=*(values+2)) || (*(oldvalue + 3)!=*(values+3)) ){
00068                throw cms::Exception("DataDoesNotMatch") << "Value does not match";
00069                failflag = true;
00070                break;
00071             }
00072             // compare the values of the pedestals for valid channels between update and reference 
00073 
00074             listNewChan.erase(cell);  // fix 25.02.08
00075           }      
00076         }
00077        if(!failflag) std::cout << "These are identical" << std::endl;
00078     }
00079  
00080     if(epsilon!=0){   
00081     std::vector<DetId> listNewChan = myNewPeds->getAllChannels();
00082     std::vector<DetId> listRefChan = myRefPeds->getAllChannels();
00083     std::vector<DetId>::iterator cell;
00084     bool failflag = false;
00085     for (std::vector<DetId>::iterator it = listRefChan.begin(); it != listRefChan.end(); it++)
00086       {
00087         DetId mydetid = *it;
00088         cell = std::find(listNewChan.begin(), listNewChan.end(), mydetid);
00089         if (cell == listNewChan.end())
00090           {
00091             continue;
00092           }
00093         else
00094           {
00095             const float* values = (myNewPeds->getValues( mydetid ))->getValues();
00096             const float* oldvalue = (myRefPeds->getValues( mydetid ))->getValues();
00097             if( (fabs(*oldvalue-*values)>epsilon) || (fabs(*(oldvalue+1)-*(values+1))>epsilon) || (fabs(*(oldvalue+2)-*(values+2))>epsilon) || (fabs(*(oldvalue+3)-*(values+3))>epsilon) ){
00098                throw cms::Exception("DataDoesNotMatch") << "Values differ by more than deltaP";
00099                failflag = true;
00100                break;
00101             }
00102             listNewChan.erase(cell);  // fix 25.02.08
00103           }
00104       }
00105     if(!failflag) std::cout << "These are identical to within deltaP" << std::endl;
00106     }
00107     if(!(outfile.compare("null")==0))
00108     {
00109     // first get the list of all channels from the update
00110     std::vector<DetId> listNewChan = myNewPeds->getAllChannels();
00111 
00112     // go through list of valid channels from reference, look up if pedestals exist for update
00113     // push back into new vector the corresponding updated pedestals,
00114     // or if it doesn't exist, the reference
00115     HcalPedestals *resultPeds = new HcalPedestals(myRefPeds->isADC() );
00116     std::vector<DetId> listRefChan = myRefPeds->getAllChannels();
00117     std::vector<DetId>::iterator cell;
00118     for (std::vector<DetId>::iterator it = listRefChan.begin(); it != listRefChan.end(); it++)
00119       {
00120         DetId mydetid = *it;
00121         cell = std::find(listNewChan.begin(), listNewChan.end(), mydetid);
00122         if (cell == listNewChan.end()) // not present in new list, take old pedestals
00123           {
00124             //   bool addValue (DetId fId, const float fValues [4]);
00125             const HcalPedestal* item = myRefPeds->getValues(mydetid);
00126             std::cout << "o";
00127             resultPeds->addValues(*item);
00128           }
00129         else // present in new list, take new pedestals
00130           {
00131             const HcalPedestal* item = myNewPeds->getValues(mydetid);
00132             std::cout << "n";
00133             resultPeds->addValues(*item);
00134             // compare the values of the pedestals for valid channels between update and reference
00135             listNewChan.erase(cell);  // fix 25.02.08
00136           }
00137       }
00138 
00139 
00140     for (std::vector<DetId>::iterator it = listNewChan.begin(); it != listNewChan.end(); it++)  // fix 25.02.08
00141       {
00142         DetId mydetid = *it;
00143         const HcalPedestal* item = myNewPeds->getValues(mydetid);
00144         std::cout << "N";
00145         resultPeds->addValues(*item);
00146       }
00147 
00148 
00149     std::cout << std::endl;
00150 
00151     std::vector<DetId> listResult = resultPeds->getAllChannels();
00152     // get the e-map list of channels
00153     std::vector<HcalGenericDetId> listEMap = myRefEMap->allPrecisionId();
00154     // look up if emap channels are all present in pedestals, if not then cerr
00155     if(checkemapflag){
00156     for (std::vector<HcalGenericDetId>::const_iterator it = listEMap.begin(); it != listEMap.end(); it++)
00157       {
00158         DetId mydetid = DetId(it->rawId());
00159         HcalGenericDetId mygenid(it->rawId());
00160         //      std::cout << "id = " << mygenid << ", hashed id = " << mygenid.hashedId() << std::endl;
00161         if (std::find(listResult.begin(), listResult.end(), mydetid ) == listResult.end())
00162           {
00163             std::cout << "Conditions not found for DetId = " << HcalGenericDetId(it->rawId()) << std::endl;
00164           }
00165       }
00166     }
00167 
00168     // dump the resulting list of pedestals into a file
00169     std::ofstream outStream3(outfile.c_str());
00170     std::cout << "--- Dumping Pedestals - the combined ones ---" << std::endl;
00171     HcalDbASCIIIO::dumpObject (outStream3, (*resultPeds) );
00172     }
00173 
00174     // const float* values = myped->getValues (channelID);
00175     //    if (values) std::cout << "pedestals for channel " << channelID << ": "
00176     //                    << values [0] << '/' << values [1] << '/' << values [2] << '/' << values [3] << std::endl; 
00177 
00178 }
00179 
00180 //vecDetId HcalPedestalsCheck::getMissingDetIds(vector<HcalPedestals> & myPedestals)
00181 //{
00182 //  HcalGeometry myHcalGeometry;
00183 //  // get the valid detid from the various subdetectors
00184 //  vecDetId validHB = myHcalGeometry.getValidDetIds(Hcal,HcalBarrel);  // check these numbers
00185 //  vecDetId validHE = myHcalGeometry.getValidDetIds(Hcal,HcalEndcap);
00186 //  vecDetId validHF = myHcalGeometry.getValidDetIds(Hcal,HcalForward);
00187 //  vecDetId validHO = myHcalGeometry.getValidDetIds(Hcal,HcalOuter);
00188 //  vecDetId validZDC = myHcalGeometry.getValidDetIds(Calo,2);
00189 //
00190 //  // check if everything is there in pedestals
00191 //
00192 //
00193 //}
00194 
00195 
00196 DEFINE_FWK_MODULE(HcalPedestalsCheck);