CMS 3D CMS Logo

BuildTrackerMap.cc
Go to the documentation of this file.
1 
2 // -*- C++ -*-
3 //
4 // Package: DQM/SiStripMonitorHardware
5 // Class: BuildTrackerMapPlugin
6 //
11 //
12 // Created: 2009/07/22
13 //
14 
15 #include <sstream>
16 #include <fstream>
17 #include <iostream>
18 #include <memory>
19 #include <list>
20 #include <algorithm>
21 #include <cassert>
22 
23 #include "TCanvas.h"
24 #include "TH1F.h"
25 #include "TStyle.h"
26 #include "TPaveStats.h"
27 
36 
42 
46 
48 
50 
51 //
52 // Class declaration
53 //
54 
56 {
57  public:
58 
60  ~BuildTrackerMapPlugin() override {}
61  private:
62  void beginJob() override;
63  void analyze(const edm::Event&, const edm::EventSetup&) override;
64  void endJob() override;
65 
66  void read(bool aMechView,
67  std::string aFile,
68  const TkDetMap* tkDetMap,
69  std::vector<std::unique_ptr<TkHistoMap>> & aTkMapVec,
70  std::vector<bool> & aValidVec);
71  void subtractMap(TkHistoMap* aResult,
72  const TkHistoMap* aSubtr);
73 
74 
75 
76  //input file names
78  //do mechanical view or not
80  //folder name for histograms in DQMStore
82  //print debug messages when problems are found: 1=error debug, 2=light debug, 3=full debug
83  unsigned int printDebug_;
84 
85  bool doDiff_;
87 
88  std::vector<std::unique_ptr<TkHistoMap>> tkHistoMapVec_;
89  std::vector<std::unique_ptr<TkHistoMap>> tkHistoMapVecDiff_;
90 
91  //name of the tkHistoMap to extract
92  std::vector<std::string> tkHistoMapNameVec_;
93  std::vector<double> minVal_;
94  std::vector<double> maxVal_;
95 
96  std::vector<bool> isValidMap_;
97  std::vector<bool> isValidMapDiff_;
98 
100  std::vector<TrackerMap*> tkmap_;
101 
102 };
103 
104 
105 //
106 // Constructors and destructor
107 //
108 
110  : fileName_(iConfig.getUntrackedParameter<std::string>("InputFileName","DQMStore.root")),
111  mechanicalView_(iConfig.getUntrackedParameter<bool>("MechanicalView",true)),
112  folderName_(iConfig.getUntrackedParameter<std::string>("HistogramFolderName","DQMData/")),
113  printDebug_(iConfig.getUntrackedParameter<unsigned int>("PrintDebugMessages",1)),
114  doDiff_(iConfig.getUntrackedParameter<bool>("DoDifference",false)),
115  fileNameDiff_(iConfig.getUntrackedParameter<std::string>("InputFileNameForDiff","DQMStore.root")),
116  tkHistoMapNameVec_(iConfig.getUntrackedParameter<std::vector<std::string> >("TkHistoMapNameVec")),
117  minVal_(iConfig.getUntrackedParameter<std::vector<double> >("MinValueVec")),
118  maxVal_(iConfig.getUntrackedParameter<std::vector<double> >("MaxValueVec")),
119  pset_(iConfig.getParameter<edm::ParameterSet>("TkmapParameters"))
120 {
121 
122 
123 
124 // for (unsigned int i(0); i<34; i++){
125 // if (i<4) histName_[i] << "TIB/layer_" << i+1 << "/" << tkDetMapName_ << "_TIB_L" << i+1;
126 // else if (i<7) histName_[i] << "TID/side_1/wheel_" << i-3 << "/" << tkDetMapName_ << "_TIDM_D" << i-3;
127 // else if (i<10) histName_[i] << "TID/side_2/wheel_" << i-6 << "/" << tkDetMapName_ << "_TIDP_D" << i-6;
128 // else if (i<16) histName_[i] << "TOB/layer_" << i-9 << "/" << tkDetMapName_ << "_TOB_L" << i-9;
129 // else if (i<25) histName_[i] << "TEC/side_1/wheel_" << i-15 << "/" << tkDetMapName_ << "_TECM_W" << i-15;
130 // else if (i<34) histName_[i] << "TEC/side_2/wheel_" << i-24 << "/" << tkDetMapName_ << "_TECP_W" << i-24;
131 
132 // std::cout << "histName[" << i << "] =" << histName_[i] << std::endl;
133 
134 // }
135 
136 }
137 
138 //
139 // Member functions
140 //
141 
142 /*Check that is possible to load in tkhistomaps histograms already stored in a DQM root file (if the folder and name are known)*/
143 void BuildTrackerMapPlugin::read(bool aMechView,
144  std::string aFile,
145  const TkDetMap* tkDetMap,
146  std::vector<std::unique_ptr<TkHistoMap>>& aTkMapVec,
147  std::vector<bool>& aValidVec)
148 {
149 
150  DQMStore * lDqmStore = edm::Service<DQMStore>().operator->();
151  lDqmStore->open(aFile);
152 
153  unsigned int nHists = tkHistoMapNameVec_.size();
154  aTkMapVec.reserve(nHists);
155  aValidVec.reserve(nHists);
156 
158  if (dirName == "") {
159  dirName += "Run ";
160  dirName += aFile.substr(aFile.find_last_of("_")+5,6);
161  dirName += "/SiStrip/Run summary";
162  std::cout << " -- DirName = " << dirName << std::endl;
163  }
164 
165  //lDqmStore->setCurrentFolder(dirName);
166  //lDqmStore->showDirStructure();
167 
168  unsigned int nFailTot=0;
169  unsigned int nTotTot = 0;
170  for (unsigned int i(0); i<nHists; i++){
171 
172  std::unique_ptr<TkHistoMap> tkHistoMap{new TkHistoMap(tkDetMap)};
173 
174  tkHistoMap->loadTkHistoMap(dirName,tkHistoMapNameVec_.at(i),aMechView);
175 
176  std::vector<MonitorElement*>& lMaps = tkHistoMap->getAllMaps();
177 
178  std::cout << " -- map " << i << ", nHistos = " << lMaps.size() << std::endl;
179  unsigned int nFail=0;
180  unsigned int nTot=0;
181 
182  for (unsigned int im(0); im<lMaps.size(); im++){
183  if (!lMaps[im]) {
184  std::cout << " -- Failed to get element " << im << " for map " << i << std::endl;
185  nFail++;
186  nFailTot++;
187  }
188  nTot++;
189  nTotTot++;
190  }
191 
192  if (nFail == nTot) aValidVec[i] = false;
193  aTkMapVec.emplace_back(std::move(tkHistoMap));
194  }
195 
196  if (nFailTot < nTotTot) std::cout << " - " << nTotTot-nFailTot << "/" << nTotTot
197  << " histomaps read with success for file ." << aFile << std::endl;
198  else {
199  std::cout << " - Failed to read any map for file " << aFile << ". Exiting line ... " << __LINE__ << std::endl;
200  exit(1);
201  }
202 
203 // //get list of detid for which |deltaRMS(APV0-APV1)|>1
204 // unsigned int lHistoNumber = 35;
205 // TkDetMap lTkDetMap;
206 // std::ofstream list,listRms0,listRms1;
207 // list.open("./cmBadModuleList.dat",std::ios::out);
208 // listRms0.open("./cmBadModuleList_rms0.dat",std::ios::out);
209 // listRms1.open("./cmBadModuleList_rms1.dat",std::ios::out);
210 // if (!list || !listRms0 || !listRms1) {
211 // std::cout << "Warning, can't open output file to write bad module list !" << std::endl;
212 // exit(1);
213 // }
214 
215 // TCanvas *lCan = new TCanvas("lCan","",1);
216 // TH1F *p_deltaMean = new TH1F("p_deltaMean",";CM_{mean}(APV0)-CM_{mean}(APV1)",500,-2,2);
217 // TH1F *p_deltaRMS = new TH1F("p_deltaRMS",";CM_{RMS}(APV0)-CM_{RMS}(APV1)",500,0,3);
218 // TH1F *p_MeanAPV0 = new TH1F("p_MeanAPV0",";CM_{mean}(APV0)",500,100,140);
219 // //TH1F *p_MeanAPV1 = new TH1F("p_MeanAPV1",";CM_{mean}(APV1)",500,100,140);
220 // TH1F *p_RMSAPV0 = new TH1F("p_RMSAPV0",";CM_{RMS}(APV0)",500,0,10);
221 // //TH1F *p_RMSAPV1 = new TH1F("p_RMSAPV1",";CM_{RMS}(APV1)",500,0,10);
222 
223 
224 
225 // gStyle->SetOptStat(1111111);
226 
227 // for(unsigned int layer=1;layer<lHistoNumber;++layer){
228 // std::vector<uint32_t> dets;
229 // lTkDetMap.getDetsForLayer(layer,dets);
230 // for(size_t i=0;i<dets.size();++i){
231 // if(dets[i]>0){
232 // //if(tkHistoMap[5]->getEntries(dets[i])>0 && tkHistoMap[5]->getValue(dets[i])) {
233 // if(nHists > 3){
234 // if (tkHistoMap[3]->getValue(dets[i]) > 1) {
235 // list << dets[i] << " " << tkHistoMap[3]->getValue(dets[i]) << std::endl;
236 // }
237 // p_deltaRMS->Fill(tkHistoMap[3]->getValue(dets[i]));
238 // }
239 // p_MeanAPV0->Fill(tkHistoMap[0]->getValue(dets[i]));
240 // //p_MeanAPV1->Fill(tkHistoMap[1]->getValue(dets[i]));
241 // p_RMSAPV0->Fill(tkHistoMap[1]->getValue(dets[i]));
242 // if (tkHistoMap[1]->getValue(dets[i]) > 2)
243 // listRms0 << dets[i] << " " << tkHistoMap[1]->getValue(dets[i]) << std::endl;
244 // //p_RMSAPV1->Fill(tkHistoMap[3]->getValue(dets[i]));
245 // //if (tkHistoMap[3]->getValue(dets[i]) > 2)
246 // //listRms1 << dets[i] << " " << tkHistoMap[3]->getValue(dets[i]) << std::endl;
247 
248 // if(nHists > 2) p_deltaMean->Fill(tkHistoMap[2]->getValue(dets[i]));
249 // }
250 // }
251 // }
252 // list.close();
253 // listRms0.close();
254 // listRms1.close();
255 
256 // lCan->cd();
257 // p_deltaRMS->Draw();
258 // //lCan->Print("./deltaRMStotal.png");
259 // lCan->Print("./deltaRMStotal.C");
260 
261 // p_deltaMean->Draw();
262 // lCan->Update();
263 // lCan->Print("./deltaMeantotal.C");
264 
265 // TPaveStats *statBox[2] = {0,0};
266 // statBox[0] = (TPaveStats*)p_MeanAPV0->FindObject("stats");
267 // //statBox[1] = (TPaveStats*)p_MeanAPV1->FindObject("stats");
268 
269 // p_MeanAPV0->Draw();
270 // //p_MeanAPV1->SetLineColor(2);
271 // //p_MeanAPV1->Draw("same");
272 // if (statBox[0]) statBox[0]->Draw("same");
273 // if (statBox[1]) {
274 // statBox[1]->SetLineColor(2);
275 // statBox[1]->SetTextColor(2);
276 // statBox[1]->Draw("same");
277 // }
278 // lCan->Update();
279 // lCan->Print("./meanAPVstotal.C");
280 
281 // statBox[0] = (TPaveStats*)p_RMSAPV0->FindObject("stats");
282 // //statBox[1] = (TPaveStats*)p_RMSAPV1->FindObject("stats");
283 
284 // p_RMSAPV0->Draw();
285 // //p_RMSAPV1->SetLineColor(2);
286 // //p_RMSAPV1->Draw("same");
287 // if (statBox[0]) statBox[0]->Draw("same");
288 // if (statBox[1]) {
289 // statBox[1]->SetLineColor(2);
290 // statBox[1]->SetTextColor(2);
291 // statBox[1]->Draw("same");
292 // }
293 // lCan->Update();
294 // lCan->Print("./rmsAPVstotal.C");
295 
296 
297 }
298 
299 
300 // ------------ method called to for each event ------------
301 void
303  const edm::EventSetup& iSetup)
304 {
305 
306  static bool firstEvent = true;
307 
309  iSetup.get<SiStripFedCablingRcd>().get(fedcabling );
310 
311  edm::ESHandle<TrackerTopology> tTopoHandle;
312  iSetup.get<TrackerTopologyRcd>().get(tTopoHandle);
313  const TrackerTopology* const tTopo = tTopoHandle.product();
314 
315  if ( tkHistoMapVec_.empty() && ( ! tkHistoMapNameVec_.empty() ) ) {
316  edm::ESHandle<TkDetMap> tkDetMapHandle;
317  iSetup.get<TrackerTopologyRcd>().get(tkDetMapHandle);
318  const TkDetMap* tkDetMap = tkDetMapHandle.product();
321  }
322 
323  if (firstEvent) {
324  for (unsigned int i(0); i<tkHistoMapNameVec_.size(); i++){
325  tkmap_.push_back(new TrackerMap(pset_,&(*fedcabling),tTopo));
326  }
327 
328  }
329 
330  firstEvent = false;
331 
332  std::cout << "End of analyze method: tkmap_ size = " << tkmap_.size() << std::endl;
333 
334 }//analyze method
335 
336 // ------------ method called once each job just before starting event loop ------------
337 void
339 {
340 
341 }
342 
343 // ------------ method called once each job just after ending the event loop ------------
344 void
346 {
347  //edm::ESHandle<SiStripFedCabling> pDD1;
348  //iSetup.get<SiStripFedCablingRcd>().get(pDD1);
349  std::cout << "Processing endjob with " << tkHistoMapNameVec_.size()<< " elements." << std::endl;
350 
351  assert (minVal_.size() == tkHistoMapNameVec_.size());
352  assert (maxVal_.size() == tkHistoMapNameVec_.size());
353 
354  for (unsigned int i(0); i<tkHistoMapNameVec_.size(); i++){
355 
356  std::cout << "Processing element " << i << ": " << tkHistoMapNameVec_.at(i) << std::endl;
357  std::cout << "Min, max = " << minVal_.at(i) << " " << maxVal_.at(i) << std::endl;
358 
359  TrackerMap* lTkMap = tkmap_.at(i);
360 
361  if (!lTkMap) {
362  std::cout << "tkmap_ is NULL for element " << i << "... continuing ..." << std::endl;
363  continue;
364  }
365 
366  subtractMap(tkHistoMapVec_.at(i).get(),tkHistoMapVecDiff_.at(i).get());
367 
368 
369  //(pset_,pDD1);
370  lTkMap->setPalette(1);
371  lTkMap->showPalette(true);
372  if (!tkHistoMapVec_.at(i) || !isValidMap_.at(i)) {
373  std::cout << "Warning, tkHistoMap is invalid for element " << i << "... continuing ..." << std::endl;
374  continue;
375  }
376  tkHistoMapVec_.at(i)->dumpInTkMap(lTkMap);
377 
378  //to print all figures to create fancy view
379  //lTkMap->printall(true,0,255,tkHistoMapNameVec_.at(i));
380  lTkMap->save(true,
381  minVal_.at(i),
382  maxVal_.at(i),
383  tkHistoMapNameVec_.at(i)+std::string(".png"));
384  lTkMap->save_as_fedtrackermap(true,
385  minVal_.at(i),
386  maxVal_.at(i),
387  tkHistoMapNameVec_.at(i)+std::string("_FED.png"));
388 
389  }
390 
391 }
392 
393 
395 {
396 
397  std::vector<MonitorElement*>& lMaps = aResult->getAllMaps();
398  const std::vector<MonitorElement*>& lMapsDiff = aSubtr->getAllMaps();
399 
400  assert(lMaps.size() == lMapsDiff.size());
401 
402  for (unsigned int im(0); im<lMaps.size(); im++){
403  if (!lMaps[im] || !lMapsDiff[im]) {
404  std::cout << " -- Failed to get element " << im << " for maps." << std::endl;
405  }
406  else {
407  (lMaps[im]->getTProfile2D())->Add(lMapsDiff[im]->getTProfile2D(),-1);
408  }
409  }
410 
411 }
412 
413 
414 
415 // Define as a plug-in
416 //
417 
BuildTrackerMapPlugin(const edm::ParameterSet &)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
std::vector< std::string > tkHistoMapNameVec_
std::vector< double > minVal_
edm::ParameterSet pset_
void read(bool aMechView, std::string aFile, const TkDetMap *tkDetMap, std::vector< std::unique_ptr< TkHistoMap >> &aTkMapVec, std::vector< bool > &aValidVec)
const std::vector< MonitorElement * > & getAllMaps() const
Definition: TkHistoMap.h:29
std::vector< double > maxVal_
std::vector< std::unique_ptr< TkHistoMap > > tkHistoMapVecDiff_
int iEvent
Definition: GenABIO.cc:230
~BuildTrackerMapPlugin() override
void showPalette(bool printflag1)
Definition: TrackerMap.h:131
void setPalette(int numpalette)
Definition: TrackerMap.h:129
void save(bool print_total=true, float minval=0., float maxval=0., std::string s="svgmap.svg", int width=1500, int height=800)
Definition: TrackerMap.cc:699
bool open(std::string const &filename, bool overwrite=false, std::string const &path="", std::string const &prepend="", OpenRunDirs stripdirs=KeepRunDirs, bool fileMustExist=true)
Definition: DQMStore.cc:2861
void save_as_fedtrackermap(bool print_total=true, float minval=0., float maxval=0., std::string s="fed_svgmap.svg", int width=100+(90+100)*31+300, int height=150+(2940+150)*1+300)
Definition: TrackerMap.cc:2135
void subtractMap(TkHistoMap *aResult, const TkHistoMap *aSubtr)
std::vector< TrackerMap * > tkmap_
void endJob() override
void analyze(const edm::Event &, const edm::EventSetup &) override
std::vector< std::unique_ptr< TkHistoMap > > tkHistoMapVec_
std::vector< bool > isValidMapDiff_
HLT enums.
std::vector< bool > isValidMap_
void loadTkHistoMap(const std::string &path, const std::string &MapName, bool mechanicalView=false)
Definition: TkHistoMap.cc:71
T get() const
Definition: EventSetup.h:68
void beginJob() override
T const * product() const
Definition: ESHandle.h:84
def move(src, dest)
Definition: eostools.py:511