CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/CalibTracker/SiStripESProducers/plugins/DBWriter/SiStripFedCablingManipulator.cc

Go to the documentation of this file.
00001 #include "FWCore/Framework/interface/EventSetup.h"
00002 #include "CalibTracker/SiStripESProducers/plugins/DBWriter/SiStripFedCablingManipulator.h"
00003 #include "CondFormats/SiStripObjects/interface/SiStripFedCabling.h"
00004 #include "CondFormats/DataRecord/interface/SiStripFedCablingRcd.h"
00005 #include <fstream>
00006 #include <iostream>
00007 
00008 SiStripFedCablingManipulator::SiStripFedCablingManipulator(const edm::ParameterSet& iConfig):iConfig_(iConfig){
00009   edm::LogInfo("SiStripFedCablingManipulator") << "SiStripFedCablingManipulator constructor "<< std::endl;
00010 }
00011 
00012 
00013 SiStripFedCablingManipulator::~SiStripFedCablingManipulator(){
00014  edm::LogInfo("SiStripFedCablingManipulator") << "SiStripFedCablingManipulator::~SiStripFedCablingManipulator()" << std::endl;
00015 }
00016 
00017 void SiStripFedCablingManipulator::endRun(const edm::Run & run, const edm::EventSetup & es){
00018   edm::ESHandle<SiStripFedCabling> esobj;
00019   es.get<SiStripFedCablingRcd>().get( esobj ); 
00020 
00021 
00022   SiStripFedCabling *obj=0;
00023   manipulate(esobj.product(),obj);
00024 
00025   cond::Time_t Time_ = 0;
00026   
00027   //And now write  data in DB
00028   edm::Service<cond::service::PoolDBOutputService> dbservice;
00029   if( dbservice.isAvailable() ){
00030 
00031     if(obj==NULL){
00032       edm::LogError("SiStripFedCablingManipulator")<<"null pointer obj. nothing will be written "<<std::endl;
00033       return;
00034     }
00035 
00036     std::string openIovAt=iConfig_.getUntrackedParameter<std::string>("OpenIovAt","beginOfTime");
00037     if(openIovAt=="beginOfTime")
00038       Time_=dbservice->beginOfTime();
00039     else if (openIovAt=="currentTime")
00040       dbservice->currentTime();
00041     else
00042       Time_=iConfig_.getUntrackedParameter<uint32_t>("OpenIovAtTime",1);
00043     
00044     //if first time tag is populated
00045     if( dbservice->isNewTagRequest("SiStripFedCablingRcd")){
00046       edm::LogInfo("SiStripFedCablingManipulator") << "first request for storing objects with Record "<< "SiStripFedCablingRcd" << " at time " << Time_ << std::endl;
00047       dbservice->createNewIOV<SiStripFedCabling>(obj, Time_ ,dbservice->endOfTime(), "SiStripFedCablingRcd");      
00048     } else {
00049       edm::LogInfo("SiStripFedCablingManipulator") << "appending a new object to existing tag " <<"SiStripFedCablingRcd" <<" in since mode " << std::endl;
00050       dbservice->appendSinceTime<SiStripFedCabling>(obj, Time_, "SiStripFedCablingRcd"); 
00051     }    
00052   } else{
00053     edm::LogError("SiStripFedCablingManipulator")<<"Service is unavailable"<<std::endl;
00054   }
00055 }
00056 
00057 void SiStripFedCablingManipulator::manipulate(const SiStripFedCabling* iobj,SiStripFedCabling*& oobj){
00058   std::string fp=iConfig_.getParameter<std::string>("file");
00059    
00060 
00061   std::ifstream inputFile_; 
00062   inputFile_.open(fp.c_str());
00063   
00064   std::map<uint32_t, std::pair<uint32_t, uint32_t> > dcuDetIdMap;
00065   uint32_t dcuid, Olddetid, Newdetid; 
00066   
00067   // if(fp.c_str()==""){
00068   if(fp.empty()){
00069     edm::LogInfo("SiStripFedCablingManipulator") << "::manipulate : since no file is specified, the copy of the input cabling will be applied"<< std::endl;
00070     oobj= new SiStripFedCabling(*iobj);
00071 
00072   } else if (!inputFile_.is_open()){
00073     edm::LogError("SiStripFedCablingManipulator") << "::manipulate - ERROR in opening file  " << fp << std::endl;
00074     throw cms::Exception("CorruptedData")  << "::manipulate - ERROR in opening file  " << fp << std::endl;
00075   }else{
00076     
00077     for(;;) {
00078       inputFile_ >> dcuid >> Olddetid >> Newdetid;
00079 
00080       if (!(inputFile_.eof() || inputFile_.fail())){
00081         
00082         if(dcuDetIdMap.find(dcuid)==dcuDetIdMap.end()){
00083           
00084           edm::LogInfo("SiStripFedCablingManipulator") << dcuid << " " << Olddetid << " " << Newdetid << std::endl;
00085           
00086           dcuDetIdMap[dcuid]=std::pair<uint32_t, uint32_t>(Olddetid,Newdetid);
00087         }else{
00088           edm::LogError("SiStripFedCablingManipulator") << "::manipulate - ERROR duplicated dcuid " << dcuid <<std::endl;     
00089           throw cms::Exception("CorruptedData") << "SiStripFedCablingManipulator::manipulate - ERROR duplicated dcuid " << dcuid ;
00090           break;
00091         }
00092       }else if (inputFile_.eof()){
00093         edm::LogInfo("SiStripFedCablingManipulator")<< "::manipulate - END of file reached"<<std::endl;
00094         break;
00095       }else if (inputFile_.fail()) {
00096         edm::LogError("SiStripFedCablingManipulator")<<"::manipulate - ERROR while reading file"<<std::endl;     
00097         break;
00098       }
00099     }
00100     inputFile_.close();
00101     std::map<uint32_t, std::pair<uint32_t, uint32_t> >::const_iterator it=dcuDetIdMap.begin();
00102     for (;it!=dcuDetIdMap.end();++it)
00103       edm::LogInfo("SiStripFedCablingManipulator")<< "::manipulate - Map "<< it->first << " " << it->second.first << " " << it->second.second;
00104    
00105 
00106     std::vector<FedChannelConnection> conns;
00107   
00108     const std::vector<uint16_t>& feds=iobj->feds();
00109     std::vector<uint16_t>::const_iterator ifeds=feds.begin();  
00110     for(;ifeds!=feds.end();ifeds++){
00111       const std::vector<FedChannelConnection>& conns_per_fed =iobj->connections( *ifeds );
00112       std::vector<FedChannelConnection>::const_iterator iconn=conns_per_fed.begin();
00113       for(;iconn!=conns_per_fed.end();++iconn){
00114         std::map<uint32_t, std::pair<uint32_t, uint32_t> >::const_iterator it=dcuDetIdMap.find(iconn->dcuId());
00115         if(it!=dcuDetIdMap.end() && it->second.first==iconn->detId()){
00116           edm::LogInfo("SiStripFedCablingManipulator")<< "::manipulate - fedid "<< *ifeds << " dcuid " <<  iconn->dcuId() << " oldDet " << iconn->detId() << " newDetID " << it->second.second ;
00117           conns.push_back(FedChannelConnection( 
00118                                                iconn->fecCrate(),
00119                                                iconn->fecSlot(),
00120                                                iconn->fecRing(),
00121                                                iconn->ccuAddr(),
00122                                                iconn->ccuChan(),
00123                                                iconn->i2cAddr(0),
00124                                                iconn->i2cAddr(1),
00125                                                iconn->dcuId(),
00126                                                it->second.second,  //<------ New detid
00127                                                iconn->nApvPairs(),
00128                                                iconn->fedId(),
00129                                                iconn->fedCh(),
00130                                                iconn->fiberLength(),
00131                                                iconn->dcu(),
00132                                                iconn->pll(),
00133                                                iconn->mux(),
00134                                                iconn->lld()
00135                                                )
00136                           );
00137         }else{
00138           conns.push_back(FedChannelConnection( 
00139                                                iconn->fecCrate(),
00140                                                iconn->fecSlot(),
00141                                                iconn->fecRing(),
00142                                                iconn->ccuAddr(),
00143                                                iconn->ccuChan(),
00144                                                iconn->i2cAddr(0),
00145                                                iconn->i2cAddr(1),
00146                                                iconn->dcuId(),
00147                                                iconn->detId(),
00148                                                iconn->nApvPairs(),
00149                                                iconn->fedId(),
00150                                                iconn->fedCh(),
00151                                                iconn->fiberLength(),
00152                                                iconn->dcu(),
00153                                                iconn->pll(),
00154                                                iconn->mux(),
00155                                                iconn->lld()
00156                                                )
00157                           );
00158         }
00159       }
00160     }
00161     
00162     oobj = new SiStripFedCabling( conns );
00163 
00164   }  
00165 }