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