CMS 3D CMS Logo

SiStripDetVOffPrinter.cc
Go to the documentation of this file.
7 
8 #include <iostream>
9 #include <fstream>
10 #include <sstream>
11 
13 
18 
19 #include <TROOT.h>
20 #include <TSystem.h>
21 #include <TCanvas.h>
22 #include <TFile.h>
23 #include <TLegend.h>
24 #include <TGraph.h>
25 #include <TH1.h>
26 
27 
29 public:
30 
31  explicit SiStripDetVOffPrinter(const edm::ParameterSet& iConfig );
32  ~SiStripDetVOffPrinter() override;
33  void analyze( const edm::Event& evt, const edm::EventSetup& evtSetup) override;
34  void endJob() override;
35  static void fillDescriptions(edm::ConfigurationDescriptions & descriptions);
36 
37 private:
38 
42 
43  // Manually specify the start/end time. Format: "2002-01-20 23:59:59.000".
46  // Specify output text file name. Leave empty if do not want to dump HV/LV counts in a file.
49 
50  // IOV DETIDs
51  std::map<cond::Time_t, std::set< uint32_t > > iovMap_HVOff;
52  std::map<cond::Time_t, std::set< uint32_t > > iovMap_LVOff;
53  // DETIDs IOV
54  std::map<uint32_t, std::vector< cond::Time_t > > detidMap;
55 };
56 
59  m_condDb( iConfig.getParameter< std::string >("conditionDatabase") ),
60  m_tagName( iConfig.getParameter< std::string >("tagName") ),
61  m_startTime( iConfig.getParameter< std::string >("startTime") ),
62  m_endTime( iConfig.getParameter< std::string >("endTime") ),
63  m_output( iConfig.getParameter< std::string >("output") )
64 {
67 }
68 
70 }
71 
72 void SiStripDetVOffPrinter::analyze(const edm::Event& evt, const edm::EventSetup& evtSetup) {
73 
74  // get total number of modules
75  //auto num_modules = detidReader->getAllDetIds().size();
76 
77  // use start and end time from config file
78  boost::posix_time::ptime p_start, p_end;
79  p_start = boost::posix_time::time_from_string(m_startTime);
80  p_end = boost::posix_time::time_from_string(m_endTime);
81  cond::Time_t startIov = cond::time::from_boost(p_start);
82  cond::Time_t endIov = cond::time::from_boost(p_end);
83  if (startIov > endIov)
84  throw cms::Exception("endTime must be greater than startTime!");
85  edm::LogInfo("SiStripDetVOffPrinter") << "[SiStripDetVOffPrinter::" << __func__ << "] "
86  << "Set start time " << startIov << " (" << boost::posix_time::to_simple_string(p_start) << ")"
87  << "\n ... Set end time " << endIov << " (" << boost::posix_time::to_simple_string(p_end) << ")" ;
88 
89  // open db session
90  edm::LogInfo("SiStripDetVOffPrinter") << "[SiStripDetVOffPrinter::" << __func__ << "] "
91  << "Query the condition database " << m_condDb;
92  cond::persistency::Session condDbSession = m_connectionPool.createSession( m_condDb );
93  condDbSession.transaction().start( true );
94 
95  std::stringstream ss;
96  // list of times with new IOVs within the time range
97  std::vector< cond::Time_t > vTime;
98 
99  // query the database
100  edm::LogInfo("SiStripDetVOffPrinter") << "[SiStripDetVOffPrinter::" << __func__ << "] "
101  << "Reading IOVs from tag " << m_tagName;
102  cond::persistency::IOVProxy iovProxy = condDbSession.readIov(m_tagName, true); // load all?
103  auto iiov = iovProxy.find(startIov);
104  auto eiov = iovProxy.find(endIov);
105  int niov = 0;
106  while (iiov != iovProxy.end() && (*iiov).since <= (*eiov).since){
107  // convert cond::Time_t to seconds since epoch
108  if ((*iiov).since<startIov){
109  vTime.push_back(startIov);
110  } else {
111  vTime.push_back((*iiov).since);
112  }
113  auto payload = condDbSession.fetchPayload<SiStripDetVOff>( (*iiov).payloadId );
114  // print IOVs summary
115  ss << boost::posix_time::to_simple_string(cond::time::to_boost((*iiov).since))
116  << " (" << (*iiov).since << ")"
117  << ", # HV Off=" << std::setw(6) << payload->getHVoffCounts()
118  << ", # LV Off=" << std::setw(6) << payload->getLVoffCounts() << std::endl;
119 
120  // list of detids with HV/LV Off
121  std::vector<uint32_t> detIds;
122  payload->getDetIds(detIds);
123  std::set<uint32_t> detIds_HVOff;
124  std::set<uint32_t> detIds_LVOff;
125  std::vector<uint32_t>::const_iterator it = detIds.begin();
126  for( ; it!=detIds.end(); ++it ) {
127  if(payload->IsModuleHVOff(*it) ) detIds_HVOff.insert(*it);
128  if(payload->IsModuleLVOff(*it) ) detIds_LVOff.insert(*it);
129 
130  if(detidMap.find(*it)==detidMap.end()) {
131  std::vector< cond::Time_t > vec;
132  detidMap[*it] = vec;
133  }
134 
135  // for each module concerned by the IOV, add the time in an history vector
136  detidMap[*it].push_back(vTime.back());
137  }
138 
139  // fill list of channels Off at a given time
140  iovMap_HVOff[vTime.back()] = detIds_HVOff;
141  iovMap_LVOff[vTime.back()] = detIds_LVOff;
142 
143  /*std::vector<uint32_t>::const_iterator it = detIds.begin();
144  for( ; it!=detIds.end(); ++it ) {
145  std::cout << *it << std::endl;
146  }*/
147 
148  ++iiov;
149  ++niov;
150  }
151  vTime.push_back(endIov); // used to compute last IOV duration
152 
153  edm::LogInfo("SiStripDetVOffPrinter") << "[SiStripDetVOffPrinter::" << __func__ << "] "
154  << "Read " << niov << " IOVs from tag " << m_tagName << " corresponding to the specified time interval.\n" << ss.str();
155 
156 
157  // Create a map of IOVs time_duration
158  std::map< cond::Time_t, boost::posix_time::time_duration > mIOVsDuration;
159  std::vector< cond::Time_t >::const_iterator itTime = ++vTime.begin();
160  std::vector< cond::Time_t >::const_iterator itPreviousTime = vTime.begin();
161  //std::vector< cond::Time_t >::const_iterator itLastTime = --vTime.end();
162  for( ; itTime!=vTime.end(); ++itTime ) {
163  mIOVsDuration[ *itPreviousTime ] = cond::time::to_boost(*itTime) - cond::time::to_boost(*itPreviousTime);
164  itPreviousTime = itTime;
165  }
166  boost::posix_time::time_duration time_period = cond::time::to_boost(*(--vTime.end())) - cond::time::to_boost(*(vTime.begin()));
167 
168  // debug
169  /*for( itTime=vTime.begin(); itTime!=itLastTime; ++itTime ) {
170  std::cout<<boost::posix_time::to_simple_string( cond::time::to_boost(*itTime) ) << " " <<boost::posix_time::to_simple_string(mIOVsDuration[ *itTime ])<<std::endl;
171  }*/
172 
173  // Print summary per module
174  edm::LogInfo("SiStripDetVOffPrinter") << "[SiStripDetVOffPrinter::" << __func__ << "] "
175  << detidMap.size() << " modules were Off at some point during the time interval";
176  ss.str("");
177 
178  // Loop over detIds
179  std::map<uint32_t, std::vector< cond::Time_t > >::const_iterator itMap = detidMap.begin();
180  for( ; itMap!=detidMap.end(); ++itMap ) {
181  std::vector< cond::Time_t > vecTime = itMap->second;
182 
183  boost::posix_time::time_duration cumul_time_HVOff(0,0,0,0);
184  boost::posix_time::time_duration cumul_time_LVOff(0,0,0,0);
185  // Loop over IOVs
186  std::vector< cond::Time_t >::const_iterator itTime = vecTime.begin();
187  for( ; itTime!=vecTime.end(); ++itTime ) {
188  if(iovMap_HVOff[*itTime].find(itMap->first) != iovMap_HVOff[*itTime].end()) cumul_time_HVOff+=mIOVsDuration[*itTime];
189  if(iovMap_LVOff[*itTime].find(itMap->first) != iovMap_LVOff[*itTime].end()) cumul_time_LVOff+=mIOVsDuration[*itTime];
190  }
191  ss<<"detId "<< itMap->first <<" #IOVs: "<<vecTime.size()
192  <<" HVOff: "<<cumul_time_HVOff<<" "<<cumul_time_HVOff.total_milliseconds()*100.0/time_period.total_milliseconds()<<"% "
193  <<" LVOff: "<<cumul_time_LVOff<<" "<<cumul_time_LVOff.total_milliseconds()*100.0/time_period.total_milliseconds()<<"%"<<std::endl;
194 
195  }
196 
197  condDbSession.transaction().commit();
198 
199  if (!m_output.empty()) {
200  std::ofstream fout;
201  fout.open(m_output);
202  fout << ss.str();
203  fout.close();
204  }
205 }
206 
207 void
209 
211  desc.add<std::string>("conditionDatabase","frontier://FrontierProd/CMS_CONDITIONS");
212  desc.add<std::string>("tagName","SiStripDetVOff_1hourDelay_v1_Validation");
213  desc.add<std::string>("startTime","2002-01-20 23:59:59.000");
214  desc.add<std::string>("endTime","2002-01-20 23:59:59.000");
215  desc.add<std::string>("output","PerModuleSummary.txt");
216  desc.add<std::string>("connect","");
217 
218  edm::ParameterSetDescription descDBParameters;
219  descDBParameters.addUntracked<std::string>("authenticationPath","");
220  descDBParameters.addUntracked<int>("authenticationSystem",0);
221  descDBParameters.addUntracked<std::string>("security","");
222  descDBParameters.addUntracked<int>("messageLevel",0);
223 
224  desc.add<edm::ParameterSetDescription>("DBParameters",descDBParameters);
225  descriptions.add("siStripDetVOffPrinter", desc);
226 }
227 
228 
230 }
231 
233 
T getParameter(std::string const &) const
std::map< uint32_t, std::vector< cond::Time_t > > detidMap
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
SiStripDetVOffPrinter(const edm::ParameterSet &iConfig)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
void start(bool readOnly=true)
Definition: Session.cc:22
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
Transaction & transaction()
Definition: Session.cc:66
void setParameters(const edm::ParameterSet &connectionPset)
IOVProxy readIov(const std::string &tag, bool full=false)
Definition: Session.cc:81
std::map< cond::Time_t, std::set< uint32_t > > iovMap_HVOff
unsigned long long Time_t
Definition: Time.h:16
void analyze(const edm::Event &evt, const edm::EventSetup &evtSetup) override
cond::persistency::ConnectionPool m_connectionPool
Iterator find(cond::Time_t time)
Definition: IOVProxy.cc:320
Session createSession(const std::string &connectionString, bool writeCapable=false)
ParameterDescriptionBase * add(U const &iLabel, T const &value)
Time_t from_boost(boost::posix_time::ptime bt)
edm::Service< SiStripDetInfoFileReader > detidReader
std::map< cond::Time_t, std::set< uint32_t > > iovMap_LVOff
void add(std::string const &label, ParameterSetDescription const &psetDescription)
boost::posix_time::ptime to_boost(Time_t iValue)
Iterator end() const
Definition: IOVProxy.cc:297
std::shared_ptr< T > fetchPayload(const cond::Hash &payloadHash)
Definition: Session.h:215