Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-05 04:10:01

0001 #ifndef SISTRIPPOPCON_UNITTEST_HANDLER_H
0002 #define SISTRIPPOPCON_UNITTEST_HANDLER_H
0003 
0004 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
0005 #include "FWCore/ServiceRegistry/interface/Service.h"
0006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0007 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
0008 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0009 
0010 #include "CondCore/PopCon/interface/PopConSourceHandler.h"
0011 #include "CondCore/CondDB/interface/Types.h"
0012 
0013 #include "OnlineDB/SiStripESSources/interface/SiStripCondObjBuilderFromDb.h"
0014 #include "OnlineDB/SiStripConfigDb/interface/SiStripDbParams.h"
0015 #include "OnlineDB/SiStripConfigDb/interface/SiStripPartition.h"
0016 
0017 #include <sstream>
0018 #include <vector>
0019 #include <string>
0020 #include <iostream>
0021 #include <typeinfo>
0022 #include <time.h>
0023 
0024 #include "CLHEP/Random/RandFlat.h"
0025 #include "CLHEP/Random/RandGauss.h"
0026 
0027 #include "CondFormats/SiStripObjects/interface/SiStripNoises.h"
0028 #include "CalibTracker/SiStripCommon/interface/SiStripDetInfoFileReader.h"
0029 
0030 namespace popcon {
0031 
0032   template <typename T>
0033   class SiStripPopConHandlerUnitTest : public popcon::PopConSourceHandler<T> {
0034   public:
0035     enum DataType { UNDEFINED = 0, _Cabling = 1, _Pedestal = 2, _Noise = 3, _Threshold = 4, _BadStrip = 5 };
0036 
0037     //---------------------------------------
0038     //
0039     SiStripPopConHandlerUnitTest(const edm::ParameterSet& pset)
0040         : m_name(pset.getUntrackedParameter<std::string>("name", "SiStripPopPopConConfigDbObjHandler")),
0041           m_since(pset.getUntrackedParameter<uint32_t>("since", 5)),
0042           m_debugMode(pset.getUntrackedParameter<bool>("debug", false)){};
0043 
0044     //---------------------------------------
0045     //
0046     ~SiStripPopConHandlerUnitTest(){};
0047 
0048     //---------------------------------------
0049     //
0050     void getNewObjects() {
0051       edm::LogInfo("SiStripPopPopConConfigDbObjHandler") << "[getNewObjects] for PopCon application " << m_name;
0052 
0053       if (m_debugMode) {
0054         std::stringstream ss;
0055         ss << "\n\n------- " << m_name << " - > getNewObjects\n";
0056         if (this->tagInfo().size) {
0057           //check whats already inside of database
0058           ss << "got offlineInfo" << this->tagInfo().name << ", size " << this->tagInfo().size
0059              << " , last object valid since " << this->tagInfo().lastInterval.since << " token "
0060              << this->tagInfo().lastInterval.payloadId << "\n\n UserText " << this->userTextLog() << "\n LogDBEntry \n"
0061              << this->logDBEntry().logId << "\n"
0062              << this->logDBEntry().destinationDB << "\n"
0063              << this->logDBEntry().provenance << "\n"
0064              << this->logDBEntry().usertext << "\n"
0065              << this->logDBEntry().iovtag << "\n"
0066              << this->logDBEntry().iovtimetype << "\n"
0067              << this->logDBEntry().payloadIdx << "\n"
0068              << this->logDBEntry().payloadName << "\n"
0069              << this->logDBEntry().payloadToken << "\n"
0070              << this->logDBEntry().payloadContainer << "\n"
0071              << this->logDBEntry().exectime << "\n"
0072              << this->logDBEntry().execmessage << "\n"
0073              << "\n\n-- user text "
0074              << this->logDBEntry().usertext.substr(this->logDBEntry().usertext.find_last_of("@"));
0075         } else {
0076           ss << " First object for this tag ";
0077         }
0078         edm::LogInfo("SiStripPopPopConConfigDbObjHandler") << ss.str();
0079       }
0080       if (isTransferNeeded())
0081         setForTransfer();
0082 
0083       edm::LogInfo("SiStripPopPopConConfigDbObjHandler")
0084           << "[getNewObjects] for PopCon application " << m_name << " Done\n--------------\n";
0085     }
0086 
0087     //---------------------------------------
0088     //
0089     std::string id() const { return m_name; }
0090 
0091   private:
0092     //methods
0093 
0094     DataType getDataType() {
0095       if (typeid(T) == typeid(SiStripFedCabling)) {
0096         edm::LogInfo("SiStripPopPopConConfigDbObjHandler")
0097             << "[getDataType] for PopCon application " << m_name << " " << typeid(T).name();
0098         return _Cabling;
0099       }
0100       return UNDEFINED;
0101     }
0102 
0103     //---------------------------------------
0104     //
0105     bool isTransferNeeded() {
0106       edm::LogInfo("SiStripPopPopConConfigDbObjHandler") << "[isTransferNeeded] checking for transfer" << std::endl;
0107       std::stringstream ss_logdb, ss;
0108       std::stringstream ss1;
0109 
0110       //get log information from previous upload
0111       if (this->tagInfo().size)
0112         ss_logdb << this->logDBEntry().usertext.substr(this->logDBEntry().usertext.find_last_of("@"));
0113       else
0114         ss_logdb << "";
0115 
0116       ss << "@ " << clock();
0117 
0118       if (!strcmp(ss.str().c_str(), ss_logdb.str().c_str())) {
0119         //string are equal, no need to do transfer
0120         edm::LogInfo("SiStripPopPopConConfigDbObjHandler")
0121             << "[isTransferNeeded] the selected conditions are already uploaded in the last iov ("
0122             << this->tagInfo().lastInterval.since << ") open for the object " << this->logDBEntry().payloadName
0123             << " in the db " << this->logDBEntry().destinationDB << " parameters: " << ss.str()
0124             << "\n NO TRANSFER NEEDED";
0125         return false;
0126       }
0127       this->m_userTextLog = ss.str();
0128       edm::LogInfo("SiStripPopPopConConfigDbObjHandler")
0129           << "[isTransferNeeded] the selected conditions will be uploaded: " << ss.str() << "\n A- " << ss.str()
0130           << "\n B- " << ss_logdb.str() << "\n Fine";
0131 
0132       return true;
0133     }
0134 
0135     //---------------------------------------
0136     //
0137     void setForTransfer() {
0138       edm::LogInfo("SiStripPopPopConConfigDbObjHandler")
0139           << "[setForTransfer] " << m_name << " getting data to be transferred " << std::endl;
0140 
0141       T* obj = 0;
0142 
0143       fillObject(obj);
0144 
0145       if (!this->tagInfo().size)
0146         m_since = 1;
0147       else if (m_debugMode)
0148         m_since = this->tagInfo().lastInterval.since + 1;
0149 
0150       if (obj != 0) {
0151         edm::LogInfo("SiStripPopPopConConfigDbObjHandler") << "setting since = " << m_since << std::endl;
0152         this->m_to_transfer.push_back(std::make_pair(obj, m_since));
0153       } else {
0154         edm::LogError("SiStripPopPopConConfigDbObjHandler")
0155             << "[setForTransfer] " << m_name << "  : NULL pointer of obj " << typeid(T).name()
0156             << " reported by SiStripCondObjBuilderFromDb\n Transfer aborted" << std::endl;
0157       }
0158     }
0159 
0160   private:
0161     // data members
0162     std::string m_name;
0163     unsigned long long m_since;
0164     bool m_debugMode;
0165     edm::Service<SiStripCondObjBuilderFromDb> condObjBuilder;
0166 
0167     void fillObject(T*& obj) {
0168       if (typeid(T) == typeid(SiStripNoises)) {
0169         obj = new SiStripNoises();
0170 
0171         const auto detInfo =
0172             SiStripDetInfoFileReader::read(edm::FileInPath{SiStripDetInfoFileReader::kDefaultFile}.fullPath());
0173         int count = -1;
0174         for (const auto& it : detInfo.getAllData()) {
0175           count++;
0176           //Generate Noise for det detid
0177           SiStripNoises::InputVector theSiStripVector;
0178           for (int strip = 0; strip < 128 * it.second.nApvs; ++strip) {
0179             float MeanNoise = 5;
0180             float RmsNoise = 1;
0181 
0182             float noise = CLHEP::RandGauss::shoot(MeanNoise, RmsNoise);
0183 
0184             //double badStripProb = .5;
0185             //bool disable = (CLHEP::RandFlat::shoot(1.) < badStripProb ? true:false);
0186 
0187             obj->setData(noise, theSiStripVector);
0188             if (count < 6)
0189               edm::LogInfo("SiStripNoisesBuilder") << "detid " << it.first << " \t"
0190                                                    << " strip " << strip << " \t" << noise << " \t"
0191                                                    << theSiStripVector.back() / 10 << " \t" << std::endl;
0192           }
0193 
0194           if (!obj->put(it.first, theSiStripVector))
0195             edm::LogError("SiStripNoisesBuilder")
0196                 << "[SiStripNoisesBuilder::analyze] detid already exists" << std::endl;
0197         }
0198       }
0199     }
0200   };
0201 
0202 }  // namespace popcon
0203 
0204 #endif  //SISTRIPPOPCON_UNITTEST_HANDLER_H