Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-02-08 03:11:57

0001 // -*- C++ -*-
0002 //
0003 // Package:    TrackerGeometryIntoNtuples
0004 // Class:      TrackerGeometryIntoNtuples
0005 //
0006 /**\class TrackerGeometryIntoNtuples TrackerGeometryIntoNtuples.cc 
0007  
0008  Description: Takes a set of alignment constants and turns them into a ROOT file
0009  
0010  Implementation:
0011  <Notes on implementation>
0012  */
0013 //
0014 // Original class TrackerGeometryIntoNtuples.cc
0015 // Original Author:  Nhan Tran
0016 //         Created:  Mon Jul 16m 16:56:34 CDT 2007
0017 // $Id: TrackerGeometryIntoNtuples.cc,v 1.14 2012/12/02 22:13:12 devdatta Exp $
0018 //
0019 // 26 May 2012
0020 // ***********
0021 // *********** Modified to add tracker module surface deformations ***********
0022 //
0023 //
0024 
0025 // system include files
0026 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0027 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0028 #include "FWCore/Framework/interface/MakerMacros.h"
0029 
0030 #include "Alignment/TrackerAlignment/interface/AlignableTracker.h"
0031 
0032 #include <algorithm>
0033 #include "TTree.h"
0034 #include "TFile.h"
0035 
0036 #include "FWCore/Framework/interface/EventSetup.h"
0037 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0038 #include "FWCore/Utilities/interface/ESGetToken.h"
0039 
0040 #include "CondFormats/Alignment/interface/DetectorGlobalPosition.h"
0041 #include "CondFormats/Alignment/interface/Alignments.h"
0042 #include "CondFormats/Alignment/interface/AlignmentSurfaceDeformations.h"
0043 
0044 #include "CondFormats/AlignmentRecord/interface/GlobalPositionRcd.h"
0045 #include "CondFormats/AlignmentRecord/interface/TrackerAlignmentRcd.h"
0046 #include "CondFormats/AlignmentRecord/interface/TrackerAlignmentErrorExtendedRcd.h"
0047 #include "CondFormats/AlignmentRecord/interface/TrackerSurfaceDeformationRcd.h"
0048 
0049 #include "CondFormats/GeometryObjects/interface/PTrackerParameters.h"
0050 #include "Geometry/Records/interface/PTrackerParametersRcd.h"
0051 
0052 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
0053 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeomBuilderFromGeometricDet.h"
0054 
0055 #include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
0056 #include "Geometry/Records/interface/TrackerTopologyRcd.h"
0057 
0058 #include "Geometry/CommonTopologies/interface/GeometryAligner.h"
0059 
0060 #include "Alignment/CommonAlignment/interface/Alignable.h"
0061 
0062 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
0063 
0064 // To access kinks and bows
0065 #include "Geometry/CommonDetUnit/interface/GeomDet.h"
0066 #include "Geometry/CommonTopologies/interface/SurfaceDeformation.h"
0067 
0068 #include "CLHEP/Matrix/SymMatrix.h"
0069 
0070 //
0071 // class decleration
0072 //
0073 
0074 class TrackerGeometryIntoNtuples : public edm::one::EDAnalyzer<> {
0075 public:
0076   explicit TrackerGeometryIntoNtuples(const edm::ParameterSet&);
0077   ~TrackerGeometryIntoNtuples() override;
0078 
0079   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0080 
0081 private:
0082   void analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) override;
0083 
0084   void addBranches();
0085 
0086   // ----------member data ---------------------------
0087   const edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> topoToken_;
0088   const edm::ESGetToken<GeometricDet, IdealGeometryRecord> geomDetToken_;
0089   const edm::ESGetToken<PTrackerParameters, PTrackerParametersRcd> ptpToken_;
0090   const edm::ESGetToken<Alignments, TrackerAlignmentRcd> aliToken_;
0091   const edm::ESGetToken<AlignmentErrorsExtended, TrackerAlignmentErrorExtendedRcd> aliErrorToken_;
0092   const edm::ESGetToken<AlignmentSurfaceDeformations, TrackerSurfaceDeformationRcd> surfDefToken_;
0093   const edm::ESGetToken<Alignments, GlobalPositionRcd> gprToken_;
0094 
0095   //std::vector<AlignTransform> m_align;
0096   AlignableTracker* theCurrentTracker;
0097 
0098   uint32_t m_rawid;
0099   double m_x, m_y, m_z;
0100   double m_alpha, m_beta, m_gamma;
0101   int m_subdetid;
0102   double m_xx, m_xy, m_yy, m_xz, m_yz, m_zz;
0103   int m_dNpar;
0104   double m_d1, m_d2, m_d3;
0105   int m_dtype;
0106   //std::vector<double>m_dpar;
0107   std::vector<double>* mp_dpar;
0108 
0109   // Deformation parameters: stored in same tree as the alignment parameters
0110   UInt_t numDeformationValues_;
0111   enum { kMaxNumPar = 20 };  // slighly above 'two bowed surfaces' limit
0112   Float_t deformationValues_[kMaxNumPar];
0113 
0114   TTree* m_tree;
0115   TTree* m_treeDeformations;
0116   TTree* m_treeErrors;
0117   std::string m_outputFile;
0118   std::string m_outputTreename;
0119   TFile* m_file;
0120 };
0121 
0122 //
0123 // constants, enums and typedefs
0124 //
0125 
0126 //
0127 // static data member definitions
0128 //
0129 
0130 //
0131 // constructors and destructor
0132 //
0133 TrackerGeometryIntoNtuples::TrackerGeometryIntoNtuples(const edm::ParameterSet& iConfig)
0134     : topoToken_(esConsumes()),
0135       geomDetToken_(esConsumes()),
0136       ptpToken_(esConsumes()),
0137       aliToken_(esConsumes()),
0138       aliErrorToken_(esConsumes()),
0139       surfDefToken_(esConsumes()),
0140       gprToken_(esConsumes()),
0141       theCurrentTracker(nullptr),
0142       m_rawid(0),
0143       m_x(0.),
0144       m_y(0.),
0145       m_z(0.),
0146       m_alpha(0.),
0147       m_beta(0.),
0148       m_gamma(0.),
0149       m_subdetid(0),
0150       m_xx(0.),
0151       m_xy(0.),
0152       m_yy(0.),
0153       m_xz(0.),
0154       m_yz(0.),
0155       m_zz(0.),
0156       m_dNpar(0),
0157       m_d1(0.),
0158       m_d2(0.),
0159       m_d3(0.),
0160       m_dtype(0),
0161       mp_dpar(nullptr) {
0162   m_outputFile = iConfig.getUntrackedParameter<std::string>("outputFile");
0163   m_outputTreename = iConfig.getUntrackedParameter<std::string>("outputTreename");
0164   m_file = new TFile(m_outputFile.c_str(), "RECREATE");
0165   m_tree = new TTree(m_outputTreename.c_str(), m_outputTreename.c_str());
0166   m_treeDeformations = new TTree("alignTreeDeformations", "alignTreeDeformations");
0167   //char errorTreeName[256];
0168   //snprintf(errorTreeName, sizeof(errorTreeName), "%sErrors", m_outputTreename);
0169   //m_treeErrors = new TTree(errorTreeName,errorTreeName);
0170   m_treeErrors = new TTree("alignTreeErrors", "alignTreeErrors");
0171 }
0172 
0173 TrackerGeometryIntoNtuples::~TrackerGeometryIntoNtuples() { delete theCurrentTracker; }
0174 
0175 void TrackerGeometryIntoNtuples::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0176   edm::ParameterSetDescription desc;
0177   desc.setComment(
0178       "Validates alignment payloads by comparing the relative position and orientations of tracker modules");
0179   desc.addUntracked<std::string>("outputFile", {});
0180   desc.addUntracked<std::string>("outputTreename", {});
0181   descriptions.addWithDefaultLabel(desc);
0182 }
0183 
0184 //
0185 // member functions
0186 //
0187 
0188 // ------------ method called to for each event  ------------
0189 void TrackerGeometryIntoNtuples::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0190   // retrieve tracker topology from geometry
0191   const TrackerTopology* const tTopo = &iSetup.getData(topoToken_);
0192 
0193   edm::LogInfo("beginJob") << "Begin Job";
0194 
0195   //accessing the initial geometry
0196   const GeometricDet* theGeometricDet = &iSetup.getData(geomDetToken_);
0197   const PTrackerParameters* ptp = &iSetup.getData(ptpToken_);
0198 
0199   TrackerGeomBuilderFromGeometricDet trackerBuilder;
0200   //currernt tracker
0201   TrackerGeometry* theCurTracker = trackerBuilder.build(theGeometricDet, *ptp, tTopo);
0202 
0203   //build the tracker
0204   const Alignments* alignments = &iSetup.getData(aliToken_);
0205   const AlignmentErrorsExtended* alignmentErrors = &iSetup.getData(aliErrorToken_);
0206   const AlignmentSurfaceDeformations* surfaceDeformations = &iSetup.getData(surfDefToken_);
0207 
0208   //apply the latest alignments
0209   const Alignments* globalPositionRcd = &iSetup.getData(gprToken_);
0210   GeometryAligner aligner;
0211   aligner.applyAlignments<TrackerGeometry>(&(*theCurTracker),
0212                                            alignments,
0213                                            alignmentErrors,
0214                                            align::DetectorGlobalPosition(*globalPositionRcd, DetId(DetId::Tracker)));
0215   aligner.attachSurfaceDeformations<TrackerGeometry>(&(*theCurTracker), &(*surfaceDeformations));
0216 
0217   theCurrentTracker = new AlignableTracker(&(*theCurTracker), tTopo);
0218 
0219   Alignments* theAlignments = theCurrentTracker->alignments();
0220   //AlignmentErrorsExtended* theAlignmentErrorsExtended = theCurrentTracker->alignmentErrors();
0221 
0222   //alignments
0223   addBranches();
0224   for (std::vector<AlignTransform>::const_iterator i = theAlignments->m_align.begin();
0225        i != theAlignments->m_align.end();
0226        ++i) {
0227     m_rawid = i->rawId();
0228     CLHEP::Hep3Vector translation = i->translation();
0229     m_x = translation.x();
0230     m_y = translation.y();
0231     m_z = translation.z();
0232 
0233     CLHEP::HepRotation rotation = i->rotation();
0234     m_alpha = rotation.getPhi();
0235     m_beta = rotation.getTheta();
0236     m_gamma = rotation.getPsi();
0237     m_tree->Fill();
0238 
0239     //DetId detid(m_rawid);
0240     //if (detid.subdetId() > 2){
0241     //
0242     //std::cout << " panel: " << tTopo->pxfPanel( m_rawid ) << ", module: " << tTopo->pxfModule( m_rawid ) << std::endl;
0243     //if ((tTopo->pxfPanel( m_rawid ) == 1) && (tTopo->pxfModule( m_rawid ) == 4)) std::cout << m_rawid << ", ";
0244     //std::cout << m_rawid << std::setprecision(9) <<  " " << m_x << " " << m_y << " " << m_z;
0245     //std::cout << std::setprecision(9) << " " << m_alpha << " " << m_beta << " " << m_gamma << std::endl;
0246     //}
0247   }
0248 
0249   delete theAlignments;
0250 
0251   std::vector<AlignTransformErrorExtended> alignErrors = alignmentErrors->m_alignError;
0252   for (std::vector<AlignTransformErrorExtended>::const_iterator i = alignErrors.begin(); i != alignErrors.end(); ++i) {
0253     m_rawid = i->rawId();
0254     CLHEP::HepSymMatrix errMatrix = i->matrix();
0255     DetId detid(m_rawid);
0256     m_subdetid = detid.subdetId();
0257     m_xx = errMatrix[0][0];
0258     m_xy = errMatrix[0][1];
0259     m_xz = errMatrix[0][2];
0260     m_yy = errMatrix[1][1];
0261     m_yz = errMatrix[1][2];
0262     m_zz = errMatrix[2][2];
0263     m_treeErrors->Fill();
0264   }
0265 
0266   // Get GeomDetUnits for the current tracker
0267   auto const& detUnits = theCurTracker->detUnits();
0268   int detUnit(0);
0269   //\\for (unsigned int iDet = 0; iDet < detUnits.size(); ++iDet) {
0270   for (auto iunit = detUnits.begin(); iunit != detUnits.end(); ++iunit) {
0271     DetId detid = (*iunit)->geographicalId();
0272     m_rawid = detid.rawId();
0273     m_subdetid = detid.subdetId();
0274 
0275     ++detUnit;
0276     //\\GeomDetUnit* geomDetUnit = detUnits.at(iDet) ;
0277     auto geomDetUnit = *iunit;
0278 
0279     // Get SurfaceDeformation for this GeomDetUnit
0280     if (geomDetUnit->surfaceDeformation()) {
0281       std::vector<double> surfaceDeformParams = (geomDetUnit->surfaceDeformation())->parameters();
0282       //edm::LogInfo("surfaceDeformParamsSize") << " surfaceDeformParams size  = " << surfaceDeformParams.size() << std::endl ;
0283       m_dNpar = surfaceDeformParams.size();
0284       m_dtype = (geomDetUnit->surfaceDeformation())->type();
0285       m_d1 = surfaceDeformParams.at(0);
0286       m_d2 = surfaceDeformParams.at(1);
0287       m_d3 = surfaceDeformParams.at(2);
0288       mp_dpar->clear();
0289       for (std::vector<double>::const_iterator it = surfaceDeformParams.begin(); it != surfaceDeformParams.end();
0290            ++it) {
0291         mp_dpar->push_back((*it));
0292         //edm::LogInfo("surfaceDeformParamsContent") << " surfaceDeformParam = " << (*it) << std::endl ;
0293       }
0294       m_treeDeformations->Fill();
0295     }
0296   }
0297 
0298   //write out
0299   m_file->cd();
0300   m_tree->Write();
0301   m_treeDeformations->Write();
0302   m_treeErrors->Write();
0303   m_file->Close();
0304 }
0305 
0306 void TrackerGeometryIntoNtuples::addBranches() {
0307   m_tree->Branch("rawid", &m_rawid, "rawid/I");
0308   m_tree->Branch("x", &m_x, "x/D");
0309   m_tree->Branch("y", &m_y, "y/D");
0310   m_tree->Branch("z", &m_z, "z/D");
0311   m_tree->Branch("alpha", &m_alpha, "alpha/D");
0312   m_tree->Branch("beta", &m_beta, "beta/D");
0313   m_tree->Branch("gamma", &m_gamma, "gamma/D");
0314 
0315   m_treeDeformations->Branch("irawid", &m_rawid, "irawid/I");
0316   m_treeDeformations->Branch("subdetid", &m_subdetid, "subdetid/I");
0317   m_treeDeformations->Branch("dNpar", &m_dNpar, "dNpar/I");
0318   //m_treeDeformations->Branch("d1", &m_d1, "d1/D");
0319   //m_treeDeformations->Branch("d2", &m_d2, "d2/D");
0320   //m_treeDeformations->Branch("d3", &m_d3, "d3/D");
0321   m_treeDeformations->Branch("dtype", &m_dtype);
0322   m_treeDeformations->Branch("dpar", "std::vector<double>", &mp_dpar);
0323 
0324   m_treeErrors->Branch("rawid", &m_rawid, "rawid/I");
0325   m_treeErrors->Branch("subdetid", &m_subdetid, "subdetid/I");
0326   m_treeErrors->Branch("xx", &m_xx, "xx/D");
0327   m_treeErrors->Branch("yy", &m_yy, "yy/D");
0328   m_treeErrors->Branch("zz", &m_zz, "zz/D");
0329   m_treeErrors->Branch("xy", &m_xy, "xy/D");
0330   m_treeErrors->Branch("xz", &m_xz, "xz/D");
0331   m_treeErrors->Branch("yz", &m_yz, "yz/D");
0332 
0333   //m_tree->Branch("NumDeform",    &numDeformationValues_, "NumDeform/i");
0334   //m_tree->Branch("DeformValues", deformationValues_,     "DeformValues[NumDeform]/F");
0335 }
0336 
0337 //define this as a plug-in
0338 DEFINE_FWK_MODULE(TrackerGeometryIntoNtuples);