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