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