00001 #include "FWCore/Framework/interface/ESHandle.h"
00002 #include "FWCore/ServiceRegistry/interface/Service.h"
00003
00004
00005 #include "CalibTracker/SiStripPedestals/interface/SiStripPedDB.h"
00006
00007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00008 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
00009
00010 #include <FWCore/Framework/interface/EventSetup.h>
00011 #include "DataFormats/Common/interface/Handle.h"
00012
00013
00014
00015
00016 #include <cstdlib>
00017
00018 using namespace std;
00019
00020 SiStripPedDB::SiStripPedDB(const edm::ParameterSet& iConfig)
00021 {
00022 conf_ = iConfig;
00023 pedsPSet_ = conf_.getParameter<edm::ParameterSet>("PedestalsPSet");
00024 nEvTot_=0;
00025 apvFactory_=0;
00026 theEventInitNumber_ = pedsPSet_.getParameter<int>("NumberOfEventsForInit");
00027 theEventIterNumber_ = pedsPSet_.getParameter<int>("NumberOfEventsForIteration");
00028 NumCMstripsInGroup_ = pedsPSet_.getParameter<int>("NumCMstripsInGroup");
00029
00030 }
00031
00032
00033 SiStripPedDB::~SiStripPedDB()
00034 {
00035 if (apvFactory_) {delete apvFactory_;}
00036 }
00037
00038
00039 void SiStripPedDB::beginJob(const edm::EventSetup& es){
00040
00041 using namespace edm;
00042 SelectedDetIds.clear();
00043
00044
00045 SiStripPedestals_ = new SiStripPedestals();
00046
00047
00048 apvFactory_ = new ApvAnalysisFactory(pedsPSet_);
00049
00050
00051 edm::ESHandle<SiStripFedCabling> cabling;
00052 es.get<SiStripFedCablingRcd>().get( cabling );
00053 fedCabling_ = const_cast<SiStripFedCabling*>( cabling.product() );
00054
00055
00056 map<uint32_t, int> detIdApv;
00057 detIdApv.clear();
00058
00059
00060 const vector<uint16_t>& fed_ids = fedCabling_->feds();
00061 vector<uint16_t>::const_iterator ifed;
00062 for ( ifed = fed_ids.begin(); ifed != fed_ids.end(); ifed++ ) {
00063 for ( uint16_t channel = 0; channel < 96; channel++ ) {
00064 const FedChannelConnection& conn = fedCabling_->connection( *ifed, channel );
00065 uint32_t key_id = conn.detId();
00066 int napvs = (conn.nApvPairs())*2;
00067 if(key_id > 0 && napvs >0) {
00068
00069
00070 bool newDetId = apvFactory_->instantiateApvs(key_id,napvs);
00071 if(newDetId)
00072 SelectedDetIds.push_back(key_id);
00073 }
00074 }
00075
00076 }
00077
00078 }
00079
00080
00081
00082 void SiStripPedDB::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
00083 {
00084 nEvTot_++;
00085
00086 using namespace edm;
00087
00088 std::string digiProducer = conf_.getParameter<std::string>("DigiProducer");
00089
00090 edm::Handle< edm::DetSetVector<SiStripRawDigi> > digi_collection;
00091 std::string digiType = "VirginRaw";
00092
00093 iEvent.getByLabel(digiProducer, digiType, digi_collection);
00094
00095
00096 for(vector<uint32_t>::const_iterator myDet = SelectedDetIds.begin();myDet!=SelectedDetIds.end();myDet++)
00097 {
00098 uint32_t detid = *myDet;
00099 vector< edm::DetSet<SiStripRawDigi> >::const_iterator digis = digi_collection->find( detid );
00100 if ( digis->data.empty() ) {
00101 edm::LogError("MonitorDigi_tmp") << "[SiStripRawDigiToRaw::createFedBuffers] Zero digis found!";
00102 }
00103
00104 apvFactory_->update(detid, (*digis));
00105
00106 if((nEvTot_ - theEventInitNumber_)%theEventIterNumber_ == 1)
00107 {
00108 mSiStripPedestals.clear();
00109
00110 SiStripPedestals::InputVector theSiStripVector;
00111 vector<float> tmp_ped;
00112 tmp_ped.clear();
00113 apvFactory_->getPedestal(detid, tmp_ped);
00114 int ibin=0;
00115 for (vector<float>::const_iterator iped=tmp_ped.begin(); iped!=tmp_ped.end();iped++) {
00116 float ped = *iped;
00117 float lTh = 0.5* 12.6;
00118 float hTh = 0.5* 12.6;
00119 if (hTh < lTh){
00120 float tmp = hTh;
00121 hTh = lTh;
00122 lTh = tmp;
00123 }
00124 SiStripPedestals_->setData(ped,theSiStripVector);
00125 ibin++;
00126 }
00127 mSiStripPedestals.push_back(make_pair(detid,theSiStripVector));
00128 }
00129 }
00130 }
00131
00132 void SiStripPedDB::endJob(void){
00133 edm::LogInfo("SiStripPedDB") << "... now write sistrippedestals data in DB" << std::endl;
00134
00135 for (std::vector< std::pair<uint32_t, SiStripPedestals::InputVector > >::iterator iter=mSiStripPedestals.begin(); iter!=mSiStripPedestals.end();iter++)
00136 {
00137 edm::LogInfo("SiStripPedDB")<<"uploading detid "<< iter->first << " vector size " << iter->second.size() <<std::endl;
00138 if ( ! SiStripPedestals_->put(iter->first,iter->second) )
00139 edm::LogError("SiStripPedDB") <<"[SiStripPedDB::analyze] detid " << iter->first << "already exists"<<endl;
00140 }
00141
00142 edm::Service<cond::service::PoolDBOutputService> mydbservice;
00143
00144 if( mydbservice.isAvailable() ){
00145 try{
00146 mydbservice->createNewIOV<SiStripPedestals>(SiStripPedestals_,mydbservice->beginOfTime(),mydbservice->endOfTime(), "SiStripPedestalsRcd");
00147 }catch(const cond::Exception& er){
00148 edm::LogError("SiStripPedDB")<<er.what()<<std::endl;
00149 }catch(const std::exception& er){
00150 edm::LogError("SiStripPedDB")<<"caught std::exception "<<er.what()<<std::endl;
00151 }catch(...){
00152 edm::LogError("SiStripPedDB")<<"Funny error"<<std::endl;
00153 }
00154 }else{
00155 edm::LogError("SiStripPedDB")<<"Service is unavailable"<<std::endl;
00156 }
00157 }
00158