Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-10-11 04:33:32

0001 #include "FWCore/Framework/interface/EventSetup.h"
0002 #include "CalibTracker/SiStripESProducers/plugins/DBWriter/SiStripFedCablingManipulator.h"
0003 #include <fstream>
0004 #include <iostream>
0005 
0006 SiStripFedCablingManipulator::SiStripFedCablingManipulator(const edm::ParameterSet& iConfig)
0007     : iConfig_(iConfig), fedCablingToken_(esConsumes<edm::Transition::EndRun>()) {
0008   edm::LogInfo("SiStripFedCablingManipulator") << "SiStripFedCablingManipulator constructor " << std::endl;
0009 }
0010 
0011 SiStripFedCablingManipulator::~SiStripFedCablingManipulator() {
0012   edm::LogInfo("SiStripFedCablingManipulator")
0013       << "SiStripFedCablingManipulator::~SiStripFedCablingManipulator()" << std::endl;
0014 }
0015 
0016 void SiStripFedCablingManipulator::endRun(const edm::Run& run, const edm::EventSetup& es) {
0017   const auto& esobj = es.getData(fedCablingToken_);
0018 
0019   auto obj = manipulate(esobj);
0020 
0021   cond::Time_t Time_ = 0;
0022 
0023   //And now write  data in DB
0024   edm::Service<cond::service::PoolDBOutputService> dbservice;
0025   if (dbservice.isAvailable()) {
0026     if (!obj) {
0027       edm::LogError("SiStripFedCablingManipulator") << "null pointer obj. nothing will be written " << std::endl;
0028       return;
0029     }
0030 
0031     std::string openIovAt = iConfig_.getUntrackedParameter<std::string>("OpenIovAt", "beginOfTime");
0032     if (openIovAt == "beginOfTime")
0033       Time_ = dbservice->beginOfTime();
0034     else if (openIovAt == "currentTime")
0035       dbservice->currentTime();
0036     else
0037       Time_ = iConfig_.getUntrackedParameter<uint32_t>("OpenIovAtTime", 1);
0038 
0039     //if first time tag is populated
0040     if (dbservice->isNewTagRequest("SiStripFedCablingRcd")) {
0041       edm::LogInfo("SiStripFedCablingManipulator") << "first request for storing objects with Record "
0042                                                    << "SiStripFedCablingRcd"
0043                                                    << " at time " << Time_ << std::endl;
0044       dbservice->createOneIOV<SiStripFedCabling>(*obj, Time_, "SiStripFedCablingRcd");
0045     } else {
0046       edm::LogInfo("SiStripFedCablingManipulator") << "appending a new object to existing tag "
0047                                                    << "SiStripFedCablingRcd"
0048                                                    << " in since mode " << std::endl;
0049       dbservice->appendOneIOV<SiStripFedCabling>(*obj, Time_, "SiStripFedCablingRcd");
0050     }
0051   } else {
0052     edm::LogError("SiStripFedCablingManipulator") << "Service is unavailable" << std::endl;
0053   }
0054 }
0055 
0056 std::unique_ptr<SiStripFedCabling> SiStripFedCablingManipulator::manipulate(const SiStripFedCabling& iobj) {
0057   std::string fp = iConfig_.getParameter<std::string>("file");
0058   std::unique_ptr<SiStripFedCabling> oobj;
0059 
0060   std::ifstream inputFile_;
0061   inputFile_.open(fp.c_str());
0062 
0063   std::map<uint32_t, std::pair<uint32_t, uint32_t> > dcuDetIdMap;
0064   uint32_t dcuid, Olddetid, Newdetid;
0065 
0066   // if(fp.c_str()==""){
0067   if (fp.empty()) {
0068     edm::LogInfo("SiStripFedCablingManipulator")
0069         << "::manipulate : since no file is specified, the copy of the input cabling will be applied" << std::endl;
0070     oobj = std::make_unique<SiStripFedCabling>(iobj);
0071   } else if (!inputFile_.is_open()) {
0072     edm::LogError("SiStripFedCablingManipulator") << "::manipulate - ERROR in opening file  " << fp << std::endl;
0073     throw cms::Exception("CorruptedData") << "::manipulate - ERROR in opening file  " << fp << std::endl;
0074   } else {
0075     for (;;) {
0076       inputFile_ >> dcuid >> Olddetid >> Newdetid;
0077 
0078       if (!(inputFile_.eof() || inputFile_.fail())) {
0079         if (dcuDetIdMap.find(dcuid) == dcuDetIdMap.end()) {
0080           edm::LogInfo("SiStripFedCablingManipulator") << dcuid << " " << Olddetid << " " << Newdetid << std::endl;
0081 
0082           dcuDetIdMap[dcuid] = std::pair<uint32_t, uint32_t>(Olddetid, Newdetid);
0083         } else {
0084           edm::LogError("SiStripFedCablingManipulator")
0085               << "::manipulate - ERROR duplicated dcuid " << dcuid << std::endl;
0086           throw cms::Exception("CorruptedData")
0087               << "SiStripFedCablingManipulator::manipulate - ERROR duplicated dcuid " << dcuid;
0088           break;
0089         }
0090       } else if (inputFile_.eof()) {
0091         edm::LogInfo("SiStripFedCablingManipulator") << "::manipulate - END of file reached" << std::endl;
0092         break;
0093       } else if (inputFile_.fail()) {
0094         edm::LogError("SiStripFedCablingManipulator") << "::manipulate - ERROR while reading file" << std::endl;
0095         break;
0096       }
0097     }
0098     inputFile_.close();
0099     std::map<uint32_t, std::pair<uint32_t, uint32_t> >::const_iterator it = dcuDetIdMap.begin();
0100     for (; it != dcuDetIdMap.end(); ++it)
0101       edm::LogInfo("SiStripFedCablingManipulator")
0102           << "::manipulate - Map " << it->first << " " << it->second.first << " " << it->second.second;
0103 
0104     std::vector<FedChannelConnection> conns;
0105 
0106     auto feds = iobj.fedIds();
0107     for (auto ifeds = feds.begin(); ifeds != feds.end(); ifeds++) {
0108       auto conns_per_fed = iobj.fedConnections(*ifeds);
0109       for (auto iconn = conns_per_fed.begin(); iconn != conns_per_fed.end(); ++iconn) {
0110         std::map<uint32_t, std::pair<uint32_t, uint32_t> >::const_iterator it = dcuDetIdMap.find(iconn->dcuId());
0111         if (it != dcuDetIdMap.end() && it->second.first == iconn->detId()) {
0112           edm::LogInfo("SiStripFedCablingManipulator")
0113               << "::manipulate - fedid " << *ifeds << " dcuid " << iconn->dcuId() << " oldDet " << iconn->detId()
0114               << " newDetID " << it->second.second;
0115           conns.push_back(FedChannelConnection(iconn->fecCrate(),
0116                                                iconn->fecSlot(),
0117                                                iconn->fecRing(),
0118                                                iconn->ccuAddr(),
0119                                                iconn->ccuChan(),
0120                                                iconn->i2cAddr(0),
0121                                                iconn->i2cAddr(1),
0122                                                iconn->dcuId(),
0123                                                it->second.second,  //<------ New detid
0124                                                iconn->nApvPairs(),
0125                                                iconn->fedId(),
0126                                                iconn->fedCh(),
0127                                                iconn->fiberLength(),
0128                                                iconn->dcu(),
0129                                                iconn->pll(),
0130                                                iconn->mux(),
0131                                                iconn->lld()));
0132         } else {
0133           conns.push_back(FedChannelConnection(iconn->fecCrate(),
0134                                                iconn->fecSlot(),
0135                                                iconn->fecRing(),
0136                                                iconn->ccuAddr(),
0137                                                iconn->ccuChan(),
0138                                                iconn->i2cAddr(0),
0139                                                iconn->i2cAddr(1),
0140                                                iconn->dcuId(),
0141                                                iconn->detId(),
0142                                                iconn->nApvPairs(),
0143                                                iconn->fedId(),
0144                                                iconn->fedCh(),
0145                                                iconn->fiberLength(),
0146                                                iconn->dcu(),
0147                                                iconn->pll(),
0148                                                iconn->mux(),
0149                                                iconn->lld()));
0150         }
0151       }
0152     }
0153 
0154     oobj = std::make_unique<SiStripFedCabling>(conns);
0155   }
0156   return oobj;
0157 }