CMS 3D CMS Logo

ApeTreeCreateDefault.cc
Go to the documentation of this file.
1 
2 // -*- C++ -*-
3 //
4 // Package: DefaultApeTree
5 // Class: DefaultApeTree
6 //
14 //
15 // Original Author: Marius Teroerde (code from ApeEstimator.cc and
16 // ApeEstimatorSummary.cc)
17 // Created: Tue Nov 14 11:43 CET 2017
18 //
19 //
20 
21 // system include files
22 #include <fstream>
23 #include <sstream>
24 
25 // user include files
32 
36 
39 
40 #include "CLHEP/Matrix/SymMatrix.h"
41 
44 
45 //...............
48 
54 
55 #include "TString.h"
56 #include "TFile.h"
57 #include "TDirectory.h"
58 #include "TTree.h"
59 #include "TMath.h"
60 //
61 // class declaration
62 //
63 
65 public:
66  explicit ApeTreeCreateDefault(const edm::ParameterSet&);
67  ~ApeTreeCreateDefault() override;
68 
69  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
70 
71 private:
72  void beginJob() override;
73  void analyze(const edm::Event&, const edm::EventSetup&) override;
74  void endJob() override;
75 
76  void sectorBuilder();
77  bool checkIntervalsForSectors(const unsigned int sectorCounter, const std::vector<double>&) const;
78  bool checkModuleIds(const unsigned int, const std::vector<unsigned int>&) const;
79  bool checkModuleBools(const bool, const std::vector<unsigned int>&) const;
80  bool checkModuleDirections(const int, const std::vector<int>&) const;
81  bool checkModulePositions(const float, const std::vector<double>&) const;
82 
83  // ----------member data ---------------------------
86  const std::vector<edm::ParameterSet> sectors_;
87 
88  std::map<unsigned int, TrackerSectorStruct> m_tkSector_;
89  std::map<unsigned int, ReducedTrackerTreeVariables> m_tkTreeVar_;
90  unsigned int noSectors;
91 };
92 
93 //
94 // constants, enums and typedefs
95 //
96 
97 //
98 // static data member definitions
99 //
100 
101 //
102 // constructors and destructor
103 //
105  : resultFile_(iConfig.getParameter<std::string>("resultFile")),
106  trackerTreeFile_(iConfig.getParameter<std::string>("trackerTreeFile")),
107  sectors_(iConfig.getParameter<std::vector<edm::ParameterSet>>("sectors")) {}
108 
110 
111 //
112 // member functions
113 //
115  // Same procedure as in ApeEstimator.cc
116  TFile* tkTreeFile(TFile::Open(trackerTreeFile_.c_str()));
117  if (tkTreeFile) {
118  edm::LogInfo("SectorBuilder") << "TrackerTreeFile OK";
119  } else {
120  edm::LogError("SectorBuilder") << "TrackerTreeFile not found";
121  return;
122  }
123  TTree* tkTree(nullptr);
124  tkTreeFile->GetObject("TrackerTreeGenerator/TrackerTree/TrackerTree", tkTree);
125  if (tkTree) {
126  edm::LogInfo("SectorBuilder") << "TrackerTree OK";
127  } else {
128  edm::LogError("SectorBuilder") << "TrackerTree not found in file";
129  return;
130  }
131 
132  unsigned int rawId(999), subdetId(999), layer(999), side(999), half(999), rod(999), ring(999), petal(999), blade(999),
133  panel(999), outerInner(999), module(999), nStrips(999);
134  bool isDoubleSide(false), isRPhi(false), isStereo(false);
135  int uDirection(999), vDirection(999), wDirection(999);
136  float posR(999.F), posPhi(999.F), posEta(999.F), posX(999.F), posY(999.F), posZ(999.F);
137 
138  tkTree->SetBranchAddress("RawId", &rawId);
139  tkTree->SetBranchAddress("SubdetId", &subdetId);
140  tkTree->SetBranchAddress("Layer", &layer);
141  tkTree->SetBranchAddress("Side", &side);
142  tkTree->SetBranchAddress("Half", &half);
143  tkTree->SetBranchAddress("Rod", &rod);
144  tkTree->SetBranchAddress("Ring", &ring);
145  tkTree->SetBranchAddress("Petal", &petal);
146  tkTree->SetBranchAddress("Blade", &blade);
147  tkTree->SetBranchAddress("Panel", &panel);
148  tkTree->SetBranchAddress("OuterInner", &outerInner);
149  tkTree->SetBranchAddress("Module", &module);
150  tkTree->SetBranchAddress("NStrips", &nStrips);
151  tkTree->SetBranchAddress("IsDoubleSide", &isDoubleSide);
152  tkTree->SetBranchAddress("IsRPhi", &isRPhi);
153  tkTree->SetBranchAddress("IsStereo", &isStereo);
154  tkTree->SetBranchAddress("UDirection", &uDirection);
155  tkTree->SetBranchAddress("VDirection", &vDirection);
156  tkTree->SetBranchAddress("WDirection", &wDirection);
157  tkTree->SetBranchAddress("PosR", &posR);
158  tkTree->SetBranchAddress("PosPhi", &posPhi);
159  tkTree->SetBranchAddress("PosEta", &posEta);
160  tkTree->SetBranchAddress("PosX", &posX);
161  tkTree->SetBranchAddress("PosY", &posY);
162  tkTree->SetBranchAddress("PosZ", &posZ);
163 
164  int nModules(tkTree->GetEntries());
165  TrackerSectorStruct allSectors;
166 
167  //Loop over all Sectors
168  unsigned int sectorCounter(0);
169  std::vector<edm::ParameterSet> v_sectorDef(sectors_);
170  edm::LogInfo("SectorBuilder") << "There are " << v_sectorDef.size() << " Sectors defined";
171 
172  for (auto const& parSet : v_sectorDef) {
173  ++sectorCounter;
174  const std::string& sectorName(parSet.getParameter<std::string>("name"));
175  std::vector<unsigned int> v_rawId(parSet.getParameter<std::vector<unsigned int>>("rawId")),
176  v_subdetId(parSet.getParameter<std::vector<unsigned int>>("subdetId")),
177  v_layer(parSet.getParameter<std::vector<unsigned int>>("layer")),
178  v_side(parSet.getParameter<std::vector<unsigned int>>("side")),
179  v_half(parSet.getParameter<std::vector<unsigned int>>("half")),
180  v_rod(parSet.getParameter<std::vector<unsigned int>>("rod")),
181  v_ring(parSet.getParameter<std::vector<unsigned int>>("ring")),
182  v_petal(parSet.getParameter<std::vector<unsigned int>>("petal")),
183  v_blade(parSet.getParameter<std::vector<unsigned int>>("blade")),
184  v_panel(parSet.getParameter<std::vector<unsigned int>>("panel")),
185  v_outerInner(parSet.getParameter<std::vector<unsigned int>>("outerInner")),
186  v_module(parSet.getParameter<std::vector<unsigned int>>("module")),
187  v_nStrips(parSet.getParameter<std::vector<unsigned int>>("nStrips")),
188  v_isDoubleSide(parSet.getParameter<std::vector<unsigned int>>("isDoubleSide")),
189  v_isRPhi(parSet.getParameter<std::vector<unsigned int>>("isRPhi")),
190  v_isStereo(parSet.getParameter<std::vector<unsigned int>>("isStereo"));
191  std::vector<int> v_uDirection(parSet.getParameter<std::vector<int>>("uDirection")),
192  v_vDirection(parSet.getParameter<std::vector<int>>("vDirection")),
193  v_wDirection(parSet.getParameter<std::vector<int>>("wDirection"));
194  std::vector<double> v_posR(parSet.getParameter<std::vector<double>>("posR")),
195  v_posPhi(parSet.getParameter<std::vector<double>>("posPhi")),
196  v_posEta(parSet.getParameter<std::vector<double>>("posEta")),
197  v_posX(parSet.getParameter<std::vector<double>>("posX")),
198  v_posY(parSet.getParameter<std::vector<double>>("posY")),
199  v_posZ(parSet.getParameter<std::vector<double>>("posZ"));
200 
201  if (!this->checkIntervalsForSectors(sectorCounter, v_posR) ||
202  !this->checkIntervalsForSectors(sectorCounter, v_posPhi) ||
203  !this->checkIntervalsForSectors(sectorCounter, v_posEta) ||
204  !this->checkIntervalsForSectors(sectorCounter, v_posX) ||
205  !this->checkIntervalsForSectors(sectorCounter, v_posY) ||
206  !this->checkIntervalsForSectors(sectorCounter, v_posZ)) {
207  continue;
208  }
209 
210  TrackerSectorStruct tkSector;
211  tkSector.name = sectorName;
212 
213  ReducedTrackerTreeVariables tkTreeVar;
214 
215  //Loop over all Modules
216  for (int module = 0; module < nModules; ++module) {
217  tkTree->GetEntry(module);
218 
219  if (sectorCounter == 1) {
220  tkTreeVar.subdetId = subdetId;
221  tkTreeVar.nStrips = nStrips;
222  tkTreeVar.uDirection = uDirection;
223  tkTreeVar.vDirection = vDirection;
224  tkTreeVar.wDirection = wDirection;
225  m_tkTreeVar_[rawId] = tkTreeVar;
226  }
227  //Check if modules from Sector builder equal those from TrackerTree
228  if (!this->checkModuleIds(rawId, v_rawId))
229  continue;
230  if (!this->checkModuleIds(subdetId, v_subdetId))
231  continue;
232  if (!this->checkModuleIds(layer, v_layer))
233  continue;
234  if (!this->checkModuleIds(side, v_side))
235  continue;
236  if (!this->checkModuleIds(half, v_half))
237  continue;
238  if (!this->checkModuleIds(rod, v_rod))
239  continue;
240  if (!this->checkModuleIds(ring, v_ring))
241  continue;
242  if (!this->checkModuleIds(petal, v_petal))
243  continue;
244  if (!this->checkModuleIds(blade, v_blade))
245  continue;
246  if (!this->checkModuleIds(panel, v_panel))
247  continue;
248  if (!this->checkModuleIds(outerInner, v_outerInner))
249  continue;
250  if (!this->checkModuleIds(module, v_module))
251  continue;
252  if (!this->checkModuleIds(nStrips, v_nStrips))
253  continue;
254  if (!this->checkModuleBools(isDoubleSide, v_isDoubleSide))
255  continue;
256  if (!this->checkModuleBools(isRPhi, v_isRPhi))
257  continue;
258  if (!this->checkModuleBools(isStereo, v_isStereo))
259  continue;
260  if (!this->checkModuleDirections(uDirection, v_uDirection))
261  continue;
262  if (!this->checkModuleDirections(vDirection, v_vDirection))
263  continue;
264  if (!this->checkModuleDirections(wDirection, v_wDirection))
265  continue;
266  if (!this->checkModulePositions(posR, v_posR))
267  continue;
268  if (!this->checkModulePositions(posPhi, v_posPhi))
269  continue;
270  if (!this->checkModulePositions(posEta, v_posEta))
271  continue;
272  if (!this->checkModulePositions(posX, v_posX))
273  continue;
274  if (!this->checkModulePositions(posY, v_posY))
275  continue;
276  if (!this->checkModulePositions(posZ, v_posZ))
277  continue;
278 
279  tkSector.v_rawId.push_back(rawId);
280  bool moduleSelected(false);
281  for (auto const& i_rawId : allSectors.v_rawId) {
282  if (rawId == i_rawId)
283  moduleSelected = true;
284  }
285  if (!moduleSelected)
286  allSectors.v_rawId.push_back(rawId);
287  }
288 
289  // Stops you from combining pixel and strip detector into one sector
290  bool isPixel(false);
291  bool isStrip(false);
292  for (auto const& i_rawId : tkSector.v_rawId) {
293  switch (m_tkTreeVar_[i_rawId].subdetId) {
296  isPixel = true;
297  break;
302  isStrip = true;
303  break;
304  }
305  }
306 
307  if (isPixel && isStrip) {
308  edm::LogError("SectorBuilder")
309  << "Incorrect Sector Definition: there are pixel and strip modules within one sector"
310  << "\n... sector selection is not applied, sector " << sectorCounter << " is not built";
311  continue;
312  }
313  tkSector.isPixel = isPixel;
314 
315  m_tkSector_[sectorCounter] = tkSector;
316  edm::LogInfo("SectorBuilder") << "There are " << tkSector.v_rawId.size() << " Modules in Sector " << sectorCounter;
317  }
318  noSectors = sectorCounter;
319  return;
320 }
321 
322 // Checking methods copied from ApeEstimator.cc
323 bool ApeTreeCreateDefault::checkIntervalsForSectors(const unsigned int sectorCounter,
324  const std::vector<double>& v_id) const {
325  if (v_id.empty())
326  return true;
327  if (v_id.size() % 2 == 1) {
328  edm::LogError("SectorBuilder")
329  << "Incorrect Sector Definition: Position Vectors need even number of arguments (Intervals)"
330  << "\n... sector selection is not applied, sector " << sectorCounter << " is not built";
331  return false;
332  }
333  int entry(0);
334  double intervalBegin(999.);
335  for (auto const& i_id : v_id) {
336  ++entry;
337  if (entry % 2 == 1)
338  intervalBegin = i_id;
339  if (entry % 2 == 0 && intervalBegin > i_id) {
340  edm::LogError("SectorBuilder") << "Incorrect Sector Definition (Position Vector Intervals): \t" << intervalBegin
341  << " is bigger than " << i_id << " but is expected to be smaller"
342  << "\n... sector selection is not applied, sector " << sectorCounter
343  << " is not built";
344  return false;
345  }
346  }
347  return true;
348 }
349 
350 bool ApeTreeCreateDefault::checkModuleIds(const unsigned int id, const std::vector<unsigned int>& v_id) const {
351  if (v_id.empty())
352  return true;
353  for (auto const& i_id : v_id) {
354  if (id == i_id)
355  return true;
356  }
357  return false;
358 }
359 
360 bool ApeTreeCreateDefault::checkModuleBools(const bool id, const std::vector<unsigned int>& v_id) const {
361  if (v_id.empty())
362  return true;
363  for (auto const& i_id : v_id) {
364  if (1 == i_id && id)
365  return true;
366  if (2 == i_id && !id)
367  return true;
368  }
369  return false;
370 }
371 
372 bool ApeTreeCreateDefault::checkModuleDirections(const int id, const std::vector<int>& v_id) const {
373  if (v_id.empty())
374  return true;
375  for (auto const& i_id : v_id) {
376  if (id == i_id)
377  return true;
378  }
379  return false;
380 }
381 
382 bool ApeTreeCreateDefault::checkModulePositions(const float id, const std::vector<double>& v_id) const {
383  if (v_id.empty())
384  return true;
385  int entry(0);
386  double intervalBegin(999.);
387  for (auto const& i_id : v_id) {
388  ++entry;
389  if (entry % 2 == 1)
390  intervalBegin = i_id;
391  if (entry % 2 == 0 && id >= intervalBegin && id < i_id)
392  return true;
393  }
394  return false;
395 }
396 
397 // ------------ method called to for each event ------------
399  // Same procedure as in ApeEstimatorSummary.cc minus reading of baseline tree
400 
401  // Load APEs from the GT and write them to root files similar to the ones from calculateAPE()
403  iSetup.get<TrackerAlignmentErrorExtendedRcd>().get(alignmentErrors);
404 
405  // Set up root file for default APE values
406  const std::string defaultFileName(resultFile_);
407  TFile* defaultFile = new TFile(defaultFileName.c_str(), "RECREATE");
408 
409  // Naming in the root files has to be iterTreeX to be consistent for the plotting tool
410  TTree* defaultTreeX(nullptr);
411  TTree* defaultTreeY(nullptr);
412  defaultFile->GetObject("iterTreeX;1", defaultTreeX);
413  defaultFile->GetObject("iterTreeY;1", defaultTreeY);
414  // The same for TTree containing the names of the sectors (no additional check, since always handled exactly as defaultTree)
415  TTree* sectorNameTree(nullptr);
416  defaultFile->GetObject("nameTree;1", sectorNameTree);
417 
418  edm::LogInfo("DefaultAPETree") << "APE Tree is being created";
419  defaultTreeX = new TTree("iterTreeX", "Tree for default APE x values from GT");
420  defaultTreeY = new TTree("iterTreeY", "Tree for default APE y values from GT");
421  sectorNameTree = new TTree("nameTree", "Tree with names of sectors");
422 
423  // Assign the information stored in the trees to arrays
424  std::vector<double*> a_defaultSectorX;
425  std::vector<double*> a_defaultSectorY;
426 
427  std::vector<std::string*> a_sectorName;
428  for (auto const& i_sector : m_tkSector_) {
429  const unsigned int iSector(i_sector.first);
430  const bool pixelSector(i_sector.second.isPixel);
431 
432  a_defaultSectorX.push_back(new double(-99.));
433  a_defaultSectorY.push_back(new double(-99.));
434  a_sectorName.push_back(new std::string(i_sector.second.name));
435 
436  std::stringstream ss_sector;
437  std::stringstream ss_sectorSuffixed;
438  ss_sector << "Ape_Sector_" << iSector;
439 
440  ss_sectorSuffixed << ss_sector.str() << "/D";
441  defaultTreeX->Branch(ss_sector.str().c_str(), &(*a_defaultSectorX[iSector - 1]), ss_sectorSuffixed.str().c_str());
442 
443  if (pixelSector) {
444  defaultTreeY->Branch(ss_sector.str().c_str(), &(*a_defaultSectorY[iSector - 1]), ss_sectorSuffixed.str().c_str());
445  }
446  sectorNameTree->Branch(ss_sector.str().c_str(), &(*a_sectorName[iSector - 1]), 32000, 00);
447  }
448 
449  // Loop over sectors for getting default APE
450 
451  for (auto& i_sector : m_tkSector_) {
452  double defaultApeX(0.);
453  double defaultApeY(0.);
454  unsigned int nModules(0);
455  for (auto const& i_rawId : i_sector.second.v_rawId) {
456  std::vector<AlignTransformErrorExtended> alignErrors = alignmentErrors->m_alignError;
457  for (auto const& i_alignError : alignErrors) {
458  if (i_rawId == i_alignError.rawId()) {
459  CLHEP::HepSymMatrix errMatrix = i_alignError.matrix();
460  defaultApeX += errMatrix[0][0];
461  defaultApeY += errMatrix[1][1];
462  nModules++;
463  }
464  }
465  }
466  *a_defaultSectorX[i_sector.first - 1] = defaultApeX / nModules;
467  *a_defaultSectorY[i_sector.first - 1] = defaultApeY / nModules;
468  }
469 
470  sectorNameTree->Fill();
471  sectorNameTree->Write("nameTree");
472  defaultTreeX->Fill();
473  defaultTreeX->Write("iterTreeX");
474  defaultTreeY->Fill();
475  defaultTreeY->Write("iterTreeY");
476 
477  defaultFile->Close();
478  delete defaultFile;
479  for (unsigned int i = 0; i < a_defaultSectorX.size(); i++) {
480  delete a_defaultSectorX[i];
481  delete a_defaultSectorY[i];
482  delete a_sectorName[i];
483  }
484 }
485 
486 // ------------ method called once each job just before starting event loop ------------
488 
489 // ------------ method called once each job just after ending the event loop ------------
491 
495 
496  std::vector<unsigned> emptyUnsignedIntVector;
497  std::vector<int> emptyIntVector;
498  std::vector<double> emptyDoubleVector;
499  sector.add<std::string>("name", "default");
500  sector.add<std::vector<unsigned>>("rawId", emptyUnsignedIntVector);
501  sector.add<std::vector<unsigned>>("subdetId", emptyUnsignedIntVector);
502  sector.add<std::vector<unsigned>>("layer", emptyUnsignedIntVector);
503  sector.add<std::vector<unsigned>>("side", emptyUnsignedIntVector);
504  sector.add<std::vector<unsigned>>("half", emptyUnsignedIntVector);
505  sector.add<std::vector<unsigned>>("rod", emptyUnsignedIntVector);
506  sector.add<std::vector<unsigned>>("ring", emptyUnsignedIntVector);
507  sector.add<std::vector<unsigned>>("petal", emptyUnsignedIntVector);
508  sector.add<std::vector<unsigned>>("blade", emptyUnsignedIntVector);
509  sector.add<std::vector<unsigned>>("panel", emptyUnsignedIntVector);
510  sector.add<std::vector<unsigned>>("outerInner", emptyUnsignedIntVector);
511  sector.add<std::vector<unsigned>>("module", emptyUnsignedIntVector);
512  sector.add<std::vector<unsigned>>("nStrips", emptyUnsignedIntVector);
513  sector.add<std::vector<unsigned>>("isDoubleSide", emptyUnsignedIntVector);
514  sector.add<std::vector<unsigned>>("isRPhi", emptyUnsignedIntVector);
515  sector.add<std::vector<unsigned>>("isStereo", emptyUnsignedIntVector);
516  sector.add<std::vector<int>>("uDirection", emptyIntVector);
517  sector.add<std::vector<int>>("vDirection", emptyIntVector);
518  sector.add<std::vector<int>>("wDirection", emptyIntVector);
519  sector.add<std::vector<double>>("posR", emptyDoubleVector);
520  sector.add<std::vector<double>>("posPhi", emptyDoubleVector);
521  sector.add<std::vector<double>>("posEta", emptyDoubleVector);
522  sector.add<std::vector<double>>("posX", emptyDoubleVector);
523  sector.add<std::vector<double>>("posY", emptyDoubleVector);
524  sector.add<std::vector<double>>("posZ", emptyDoubleVector);
525 
526  desc.add<std::string>("resultFile", "defaultAPE.root");
527  desc.add<std::string>("trackerTreeFile");
528  desc.addVPSet("sectors", sector);
529 
530  descriptions.add("apeTreeCreateDefault", desc);
531 }
532 
533 //define this as a plug-in
bool checkModulePositions(const float, const std::vector< double > &) const
ApeTreeCreateDefault(const edm::ParameterSet &)
ParameterDescriptionBase * addVPSet(U const &iLabel, ParameterSetDescription const &validator, std::vector< ParameterSet > const &defaults)
const std::vector< edm::ParameterSet > sectors_
std::map< unsigned int, ReducedTrackerTreeVariables > m_tkTreeVar_
std::map< unsigned int, TrackerSectorStruct > m_tkSector_
bool checkModuleBools(const bool, const std::vector< unsigned int > &) const
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
const std::string trackerTreeFile_
bool checkModuleIds(const unsigned int, const std::vector< unsigned int > &) const
ParameterDescriptionBase * add(U const &iLabel, T const &value)
std::vector< AlignTransformErrorExtended > m_alignError
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
const std::string resultFile_
void add(std::string const &label, ParameterSetDescription const &psetDescription)
HLT enums.
bool checkModuleDirections(const int, const std::vector< int > &) const
T get() const
Definition: EventSetup.h:71
bool checkIntervalsForSectors(const unsigned int sectorCounter, const std::vector< double > &) const
bool isPixel(HitType hitType)
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:281
void analyze(const edm::Event &, const edm::EventSetup &) override
Definition: vlib.h:208
std::vector< unsigned int > v_rawId