Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-08-13 05:00:12

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