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 
35 
41 
45 
47 
49 
50 //
51 // Class declaration
52 //
53 
55 public:
58 
60  ~BuildTrackerMapPlugin() override {}
61 
62 private:
63  void beginJob() override;
64  void analyze(const edm::Event&, const edm::EventSetup&) override;
65  void endJob() override;
66 
67  void read(bool aMechView,
68  std::string aFile,
69  const TkDetMap* tkDetMap,
70  std::vector<std::unique_ptr<TkHistoMap>>& aTkMapVec,
71  std::vector<bool>& aValidVec);
72  void subtractMap(TkHistoMap* aResult, const TkHistoMap* aSubtr);
73 
74  //input file names
76  //do mechanical view or not
78  //folder name for histograms in DQMStore
80  //print debug messages when problems are found: 1=error debug, 2=light debug, 3=full debug
81  unsigned int printDebug_;
82 
83  bool doDiff_;
85 
86  std::vector<std::unique_ptr<TkHistoMap>> tkHistoMapVec_;
87  std::vector<std::unique_ptr<TkHistoMap>> tkHistoMapVecDiff_;
88 
89  //name of the tkHistoMap to extract
90  std::vector<std::string> tkHistoMapNameVec_;
91  std::vector<double> minVal_;
92  std::vector<double> maxVal_;
93 
94  std::vector<bool> isValidMap_;
95  std::vector<bool> isValidMapDiff_;
96 
98  std::vector<TrackerMap*> tkmap_;
99 
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  fedCablingToken_(esConsumes<>()),
121  tTopoToken_(esConsumes<>()),
122  tkDetMapToken_(esConsumes<>()) {
123  // for (unsigned int i(0); i<34; i++){
124  // if (i<4) histName_[i] << "TIB/layer_" << i+1 << "/" << tkDetMapName_ << "_TIB_L" << i+1;
125  // else if (i<7) histName_[i] << "TID/side_1/wheel_" << i-3 << "/" << tkDetMapName_ << "_TIDM_D" << i-3;
126  // else if (i<10) histName_[i] << "TID/side_2/wheel_" << i-6 << "/" << tkDetMapName_ << "_TIDP_D" << i-6;
127  // else if (i<16) histName_[i] << "TOB/layer_" << i-9 << "/" << tkDetMapName_ << "_TOB_L" << i-9;
128  // else if (i<25) histName_[i] << "TEC/side_1/wheel_" << i-15 << "/" << tkDetMapName_ << "_TECM_W" << i-15;
129  // else if (i<34) histName_[i] << "TEC/side_2/wheel_" << i-24 << "/" << tkDetMapName_ << "_TECP_W" << i-24;
130 
131  // std::cout << "histName[" << i << "] =" << histName_[i] << std::endl;
132 
133  // }
134 }
135 
136 //
137 // Member functions
138 //
139 
140 /*Check that is possible to load in tkhistomaps histograms already stored in a DQM root file (if the folder and name are known)*/
141 void BuildTrackerMapPlugin::read(bool aMechView,
142  std::string aFile,
143  const TkDetMap* tkDetMap,
144  std::vector<std::unique_ptr<TkHistoMap>>& aTkMapVec,
145  std::vector<bool>& aValidVec) {
146  DQMStore* lDqmStore = edm::Service<DQMStore>().operator->();
147  lDqmStore->open(aFile);
148 
149  unsigned int nHists = tkHistoMapNameVec_.size();
150  aTkMapVec.reserve(nHists);
151  aValidVec.reserve(nHists);
152 
154  if (dirName.empty()) {
155  dirName += "Run ";
156  dirName += aFile.substr(aFile.find_last_of('_') + 5, 6);
157  dirName += "/SiStrip/Run summary";
158  std::cout << " -- DirName = " << dirName << std::endl;
159  }
160 
161  //lDqmStore->setCurrentFolder(dirName);
162 
163  unsigned int nFailTot = 0;
164  unsigned int nTotTot = 0;
165  for (unsigned int i(0); i < nHists; i++) {
166  std::unique_ptr<TkHistoMap> tkHistoMap{new TkHistoMap(tkDetMap)};
167 
168  tkHistoMap->loadTkHistoMap(dirName, tkHistoMapNameVec_.at(i), aMechView);
169 
170  std::vector<MonitorElement*>& lMaps = tkHistoMap->getAllMaps();
171 
172  std::cout << " -- map " << i << ", nHistos = " << lMaps.size() << std::endl;
173  unsigned int nFail = 0;
174  unsigned int nTot = 0;
175 
176  for (unsigned int im(0); im < lMaps.size(); im++) {
177  if (!lMaps[im]) {
178  std::cout << " -- Failed to get element " << im << " for map " << i << std::endl;
179  nFail++;
180  nFailTot++;
181  }
182  nTot++;
183  nTotTot++;
184  }
185 
186  if (nFail == nTot)
187  aValidVec[i] = false;
188  aTkMapVec.emplace_back(std::move(tkHistoMap));
189  }
190 
191  if (nFailTot < nTotTot)
192  std::cout << " - " << nTotTot - nFailTot << "/" << nTotTot << " histomaps read with success for file ." << aFile
193  << std::endl;
194  else {
195  std::cout << " - Failed to read any map for file " << aFile << ". Exiting line ... " << __LINE__ << std::endl;
196  exit(1);
197  }
198 
199  // //get list of detid for which |deltaRMS(APV0-APV1)|>1
200  // unsigned int lHistoNumber = 35;
201  // TkDetMap lTkDetMap;
202  // std::ofstream list,listRms0,listRms1;
203  // list.open("./cmBadModuleList.dat",std::ios::out);
204  // listRms0.open("./cmBadModuleList_rms0.dat",std::ios::out);
205  // listRms1.open("./cmBadModuleList_rms1.dat",std::ios::out);
206  // if (!list || !listRms0 || !listRms1) {
207  // std::cout << "Warning, can't open output file to write bad module list !" << std::endl;
208  // exit(1);
209  // }
210 
211  // TCanvas *lCan = new TCanvas("lCan","",1);
212  // TH1F *p_deltaMean = new TH1F("p_deltaMean",";CM_{mean}(APV0)-CM_{mean}(APV1)",500,-2,2);
213  // TH1F *p_deltaRMS = new TH1F("p_deltaRMS",";CM_{RMS}(APV0)-CM_{RMS}(APV1)",500,0,3);
214  // TH1F *p_MeanAPV0 = new TH1F("p_MeanAPV0",";CM_{mean}(APV0)",500,100,140);
215  // //TH1F *p_MeanAPV1 = new TH1F("p_MeanAPV1",";CM_{mean}(APV1)",500,100,140);
216  // TH1F *p_RMSAPV0 = new TH1F("p_RMSAPV0",";CM_{RMS}(APV0)",500,0,10);
217  // //TH1F *p_RMSAPV1 = new TH1F("p_RMSAPV1",";CM_{RMS}(APV1)",500,0,10);
218 
219  // gStyle->SetOptStat(1111111);
220 
221  // for(unsigned int layer=1;layer<lHistoNumber;++layer){
222  // std::vector<uint32_t> dets;
223  // lTkDetMap.getDetsForLayer(layer,dets);
224  // for(size_t i=0;i<dets.size();++i){
225  // if(dets[i]>0){
226  // //if(tkHistoMap[5]->getEntries(dets[i])>0 && tkHistoMap[5]->getValue(dets[i])) {
227  // if(nHists > 3){
228  // if (tkHistoMap[3]->getValue(dets[i]) > 1) {
229  // list << dets[i] << " " << tkHistoMap[3]->getValue(dets[i]) << std::endl;
230  // }
231  // p_deltaRMS->Fill(tkHistoMap[3]->getValue(dets[i]));
232  // }
233  // p_MeanAPV0->Fill(tkHistoMap[0]->getValue(dets[i]));
234  // //p_MeanAPV1->Fill(tkHistoMap[1]->getValue(dets[i]));
235  // p_RMSAPV0->Fill(tkHistoMap[1]->getValue(dets[i]));
236  // if (tkHistoMap[1]->getValue(dets[i]) > 2)
237  // listRms0 << dets[i] << " " << tkHistoMap[1]->getValue(dets[i]) << std::endl;
238  // //p_RMSAPV1->Fill(tkHistoMap[3]->getValue(dets[i]));
239  // //if (tkHistoMap[3]->getValue(dets[i]) > 2)
240  // //listRms1 << dets[i] << " " << tkHistoMap[3]->getValue(dets[i]) << std::endl;
241 
242  // if(nHists > 2) p_deltaMean->Fill(tkHistoMap[2]->getValue(dets[i]));
243  // }
244  // }
245  // }
246  // list.close();
247  // listRms0.close();
248  // listRms1.close();
249 
250  // lCan->cd();
251  // p_deltaRMS->Draw();
252  // //lCan->Print("./deltaRMStotal.png");
253  // lCan->Print("./deltaRMStotal.C");
254 
255  // p_deltaMean->Draw();
256  // lCan->Update();
257  // lCan->Print("./deltaMeantotal.C");
258 
259  // TPaveStats *statBox[2] = {0,0};
260  // statBox[0] = (TPaveStats*)p_MeanAPV0->FindObject("stats");
261  // //statBox[1] = (TPaveStats*)p_MeanAPV1->FindObject("stats");
262 
263  // p_MeanAPV0->Draw();
264  // //p_MeanAPV1->SetLineColor(2);
265  // //p_MeanAPV1->Draw("same");
266  // if (statBox[0]) statBox[0]->Draw("same");
267  // if (statBox[1]) {
268  // statBox[1]->SetLineColor(2);
269  // statBox[1]->SetTextColor(2);
270  // statBox[1]->Draw("same");
271  // }
272  // lCan->Update();
273  // lCan->Print("./meanAPVstotal.C");
274 
275  // statBox[0] = (TPaveStats*)p_RMSAPV0->FindObject("stats");
276  // //statBox[1] = (TPaveStats*)p_RMSAPV1->FindObject("stats");
277 
278  // p_RMSAPV0->Draw();
279  // //p_RMSAPV1->SetLineColor(2);
280  // //p_RMSAPV1->Draw("same");
281  // if (statBox[0]) statBox[0]->Draw("same");
282  // if (statBox[1]) {
283  // statBox[1]->SetLineColor(2);
284  // statBox[1]->SetTextColor(2);
285  // statBox[1]->Draw("same");
286  // }
287  // lCan->Update();
288  // lCan->Print("./rmsAPVstotal.C");
289 }
290 
291 // ------------ method called to for each event ------------
293  static bool firstEvent = true;
294 
295  if (tkHistoMapVec_.empty() && (!tkHistoMapNameVec_.empty())) {
296  const auto tkDetMap = &iSetup.getData(tkDetMapToken_);
298  if (doDiff_)
300  }
301 
302  if (firstEvent) {
303  const auto fedcabling = &iSetup.getData(fedCablingToken_);
304  const auto tTopo = &iSetup.getData(tTopoToken_);
305  for (unsigned int i(0); i < tkHistoMapNameVec_.size(); i++) {
306  tkmap_.push_back(new TrackerMap(pset_, fedcabling, tTopo));
307  }
308  }
309 
310  firstEvent = false;
311 
312  std::cout << "End of analyze method: tkmap_ size = " << tkmap_.size() << std::endl;
313 
314 } //analyze method
315 
316 // ------------ method called once each job just before starting event loop ------------
318 
319 // ------------ method called once each job just after ending the event loop ------------
321  //edm::ESHandle<SiStripFedCabling> pDD1;
322  //iSetup.get<SiStripFedCablingRcd>().get(pDD1);
323  std::cout << "Processing endjob with " << tkHistoMapNameVec_.size() << " elements." << std::endl;
324 
325  assert(minVal_.size() == tkHistoMapNameVec_.size());
326  assert(maxVal_.size() == tkHistoMapNameVec_.size());
327 
328  for (unsigned int i(0); i < tkHistoMapNameVec_.size(); i++) {
329  std::cout << "Processing element " << i << ": " << tkHistoMapNameVec_.at(i) << std::endl;
330  std::cout << "Min, max = " << minVal_.at(i) << " " << maxVal_.at(i) << std::endl;
331 
332  TrackerMap* lTkMap = tkmap_.at(i);
333 
334  if (!lTkMap) {
335  std::cout << "tkmap_ is NULL for element " << i << "... continuing ..." << std::endl;
336  continue;
337  }
338 
339  subtractMap(tkHistoMapVec_.at(i).get(), tkHistoMapVecDiff_.at(i).get());
340 
341  //(pset_,pDD1);
342  lTkMap->setPalette(1);
343  lTkMap->showPalette(true);
344  if (!tkHistoMapVec_.at(i) || !isValidMap_.at(i)) {
345  std::cout << "Warning, tkHistoMap is invalid for element " << i << "... continuing ..." << std::endl;
346  continue;
347  }
348  tkHistoMapVec_.at(i)->dumpInTkMap(lTkMap);
349 
350  //to print all figures to create fancy view
351  //lTkMap->printall(true,0,255,tkHistoMapNameVec_.at(i));
352  lTkMap->save(true, minVal_.at(i), maxVal_.at(i), tkHistoMapNameVec_.at(i) + std::string(".png"));
353  lTkMap->save_as_fedtrackermap(
354  true, minVal_.at(i), maxVal_.at(i), tkHistoMapNameVec_.at(i) + std::string("_FED.png"));
355  }
356 }
357 
359  std::vector<MonitorElement*>& lMaps = aResult->getAllMaps();
360  const std::vector<MonitorElement*>& lMapsDiff = aSubtr->getAllMaps();
361 
362  assert(lMaps.size() == lMapsDiff.size());
363 
364  for (unsigned int im(0); im < lMaps.size(); im++) {
365  if (!lMaps[im] || !lMapsDiff[im]) {
366  std::cout << " -- Failed to get element " << im << " for maps." << std::endl;
367  } else {
368  (lMaps[im]->getTProfile2D())->Add(lMapsDiff[im]->getTProfile2D(), -1);
369  }
370  }
371 }
372 
373 // Define as a plug-in
374 //
375 
BuildTrackerMapPlugin(const edm::ParameterSet &)
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
std::vector< std::string > tkHistoMapNameVec_
std::vector< double > minVal_
const std::vector< MonitorElement * > & getAllMaps() const
Definition: TkHistoMap.h:45
edm::ParameterSet pset_
void read(bool aMechView, std::string aFile, const TkDetMap *tkDetMap, std::vector< std::unique_ptr< TkHistoMap >> &aTkMapVec, std::vector< bool > &aValidVec)
assert(be >=bs)
edm::ESGetToken< TrackerTopology, TrackerTopologyRcd > tTopoToken_
std::vector< double > maxVal_
std::vector< std::unique_ptr< TkHistoMap > > tkHistoMapVecDiff_
int iEvent
Definition: GenABIO.cc:224
~BuildTrackerMapPlugin() override
void showPalette(bool printflag1)
Definition: TrackerMap.h:157
dqm::legacy::DQMStore DQMStore
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:2604
void setPalette(int numpalette)
Definition: TrackerMap.h:155
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:811
bool getData(T &iHolder) const
Definition: EventSetup.h:122
edm::ESGetToken< TkDetMap, TrackerTopologyRcd > tkDetMapToken_
edm::ESGetToken< SiStripFedCabling, SiStripFedCablingRcd > fedCablingToken_
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:77
void beginJob() override
dqm::legacy::MonitorElement MonitorElement
def move(src, dest)
Definition: eostools.py:511
DQM_DEPRECATED 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:797
def exit(msg="")