CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/Alignment/OfflineValidation/plugins/TrackerGeometryIntoNtuples.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    TrackerGeometryIntoNtuples
00004 // Class:      TrackerGeometryIntoNtuples
00005 // 
00013 //
00014 // Original class TrackerGeometryIntoNtuples.cc 
00015 // Original Author:  Nhan Tran
00016 //         Created:  Mon Jul 16m 16:56:34 CDT 2007
00017 // $Id: TrackerGeometryIntoNtuples.cc,v 1.14 2012/12/02 22:13:12 devdatta Exp $
00018 //
00019 // 26 May 2012 
00020 // ***********
00021 // *********** Modified to add tracker module surface deformations ***********
00022 //
00023 //
00024 
00025 // system include files
00026 #include "FWCore/Framework/interface/EDAnalyzer.h"
00027 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00028 #include "FWCore/Framework/interface/MakerMacros.h"
00029 
00030 #include "Alignment/TrackerAlignment/interface/AlignableTracker.h"
00031 
00032 #include <algorithm>
00033 #include "TTree.h"
00034 #include "TFile.h"
00035 
00036 #include "FWCore/Framework/interface/ESHandle.h"
00037 #include "FWCore/Framework/interface/EventSetup.h"
00038 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00039 
00040 #include "CondFormats/Alignment/interface/DetectorGlobalPosition.h"
00041 #include "CondFormats/Alignment/interface/Alignments.h"
00042 #include "CondFormats/Alignment/interface/AlignmentSurfaceDeformations.h" 
00043 
00044 #include "CondFormats/AlignmentRecord/interface/GlobalPositionRcd.h"
00045 #include "CondFormats/AlignmentRecord/interface/TrackerAlignmentRcd.h"
00046 #include "CondFormats/AlignmentRecord/interface/TrackerAlignmentErrorRcd.h"
00047 #include "CondFormats/AlignmentRecord/interface/TrackerSurfaceDeformationRcd.h" 
00048 
00049 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
00050 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeomBuilderFromGeometricDet.h"
00051 
00052 #include "Geometry/Records/interface/IdealGeometryRecord.h"
00053 
00054 #include "Geometry/TrackingGeometryAligner/interface/GeometryAligner.h"
00055 
00056 #include "Alignment/CommonAlignment/interface/Alignable.h"
00057 
00058 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
00059 
00060 // To access kinks and bows 
00061 #include "Geometry/CommonDetUnit/interface/GeomDet.h" 
00062 #include "Geometry/CommonDetUnit/interface/GeomDetUnit.h"
00063 #include "Geometry/CommonTopologies/interface/SurfaceDeformation.h"
00064 
00065 #include "CLHEP/Matrix/SymMatrix.h"
00066 
00067 //
00068 // class decleration
00069 //
00070 
00071 class TrackerGeometryIntoNtuples : public edm::EDAnalyzer {
00072 public:
00073         explicit TrackerGeometryIntoNtuples(const edm::ParameterSet&);
00074         ~TrackerGeometryIntoNtuples();
00075         
00076         
00077 private:
00078         virtual void analyze(const edm::Event &iEvent, const edm::EventSetup &iSetup);
00079         
00080         void addBranches();
00081         
00082         // ----------member data ---------------------------
00083         const edm::ParameterSet theParameterSet; 
00084         //std::vector<AlignTransform> m_align;
00085         AlignableTracker* theCurrentTracker ;
00086         
00087         uint32_t m_rawid;
00088         double m_x, m_y, m_z;
00089         double m_alpha, m_beta, m_gamma;
00090         int m_subdetid;
00091         double m_xx, m_xy, m_yy, m_xz, m_yz, m_zz;
00092         int m_dNpar ; 
00093         double m_d1,m_d2, m_d3; 
00094         int m_dtype ; 
00095         //std::vector<double>m_dpar;
00096         std::vector<double>* mp_dpar;
00097 
00098         // Deformation parameters: stored in same tree as the alignment parameters 
00099         UInt_t numDeformationValues_;
00100         enum {kMaxNumPar = 20}; // slighly above 'two bowed surfaces' limit
00101         Float_t deformationValues_[kMaxNumPar]; 
00102         
00103         TTree *m_tree;
00104         TTree *m_treeDeformations;
00105         TTree *m_treeErrors;
00106         std::string m_outputFile;
00107         std::string m_outputTreename;
00108         TFile *m_file;
00109 };
00110 
00111 //
00112 // constants, enums and typedefs
00113 //
00114 
00115 //
00116 // static data member definitions
00117 //
00118 
00119 //
00120 // constructors and destructor
00121 //
00122 TrackerGeometryIntoNtuples::TrackerGeometryIntoNtuples(const edm::ParameterSet& iConfig) :
00123   theParameterSet( iConfig ),   
00124   theCurrentTracker(0),
00125   m_rawid(0),
00126   m_x(0.), m_y(0.), m_z(0.),
00127   m_alpha(0.), m_beta(0.), m_gamma(0.),
00128   m_subdetid(0),
00129   m_xx(0.), m_xy(0.), m_yy(0.), m_xz(0.), m_yz(0.), m_zz(0.), 
00130   m_dNpar(0), 
00131   m_d1(0.), m_d2(0.), m_d3(0.),
00132   m_dtype(0), 
00133   mp_dpar(0)    
00134 {
00135         m_outputFile = iConfig.getUntrackedParameter< std::string > ("outputFile");
00136         m_outputTreename = iConfig.getUntrackedParameter< std::string > ("outputTreename");
00137         m_file = new TFile(m_outputFile.c_str(),"RECREATE");
00138         m_tree = new TTree(m_outputTreename.c_str(),m_outputTreename.c_str());
00139         m_treeDeformations = new TTree("alignTreeDeformations","alignTreeDeformations"); 
00140         //char errorTreeName[256];
00141         //snprintf(errorTreeName, sizeof(errorTreeName), "%sErrors", m_outputTreename);
00142         //m_treeErrors = new TTree(errorTreeName,errorTreeName);
00143         m_treeErrors = new TTree("alignTreeErrors","alignTreeErrors");
00144         
00145 }
00146 
00147 
00148 TrackerGeometryIntoNtuples::~TrackerGeometryIntoNtuples()
00149 {
00150   delete theCurrentTracker;
00151 }
00152 
00153 
00154 //
00155 // member functions
00156 //
00157 
00158 // ------------ method called to for each event  ------------
00159 void TrackerGeometryIntoNtuples::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
00160 {
00161         edm::LogInfo("beginJob") << "Begin Job" << std::endl;
00162         
00163         //accessing the initial geometry
00164         edm::ESHandle<GeometricDet> theGeometricDet;
00165         iSetup.get<IdealGeometryRecord>().get(theGeometricDet);
00166         TrackerGeomBuilderFromGeometricDet trackerBuilder;
00167         //currernt tracker
00168         TrackerGeometry* theCurTracker = trackerBuilder.build(&*theGeometricDet,theParameterSet); 
00169         
00170         //build the tracker
00171         edm::ESHandle<Alignments> alignments;
00172         edm::ESHandle<AlignmentErrors> alignmentErrors;
00173         edm::ESHandle<AlignmentSurfaceDeformations> surfaceDeformations;
00174         
00175         iSetup.get<TrackerAlignmentRcd>().get(alignments);
00176         iSetup.get<TrackerAlignmentErrorRcd>().get(alignmentErrors);
00177         iSetup.get<TrackerSurfaceDeformationRcd>().get(surfaceDeformations);
00178         
00179         //apply the latest alignments
00180         edm::ESHandle<Alignments> globalPositionRcd;
00181         iSetup.get<TrackerDigiGeometryRecord>().getRecord<GlobalPositionRcd>().get(globalPositionRcd);
00182         GeometryAligner aligner;
00183         aligner.applyAlignments<TrackerGeometry>( &(*theCurTracker), &(*alignments), &(*alignmentErrors),
00184                                                                                          align::DetectorGlobalPosition(*globalPositionRcd, DetId(DetId::Tracker)));
00185         aligner.attachSurfaceDeformations<TrackerGeometry>( &(*theCurTracker), &(*surfaceDeformations)) ; 
00186         
00187         
00188         theCurrentTracker = new AlignableTracker(&(*theCurTracker));    
00189 
00190         Alignments* theAlignments = theCurrentTracker->alignments();
00191         //AlignmentErrors* theAlignmentErrors = theCurrentTracker->alignmentErrors();   
00192         
00193         //alignments
00194         addBranches();
00195         for (std::vector<AlignTransform>::const_iterator i = theAlignments->m_align.begin(); i != theAlignments->m_align.end(); ++i){
00196                 
00197                 m_rawid = i->rawId();
00198                 CLHEP::Hep3Vector translation = i->translation();
00199                 m_x = translation.x();
00200                 m_y = translation.y();
00201                 m_z = translation.z();
00202                 
00203         
00204                 CLHEP::HepRotation rotation = i->rotation();
00205                 m_alpha = rotation.getPhi();
00206                 m_beta = rotation.getTheta();
00207                 m_gamma = rotation.getPsi();
00208                 m_tree->Fill();
00209                 
00210                 //DetId detid(m_rawid);
00211                 //if (detid.subdetId() > 2){
00212                 //PXFDetId pxfid( m_rawid );
00213                 //std::cout << " panel: " << pxfid.panel() << ", module: " << pxfid.module() << std::endl;
00214                 //if ((pxfid.panel() == 1) && (pxfid.module() == 4)) std::cout << m_rawid << ", ";
00215                 //std::cout << m_rawid << std::setprecision(9) <<  " " << m_x << " " << m_y << " " << m_z;
00216                 //std::cout << std::setprecision(9) << " " << m_alpha << " " << m_beta << " " << m_gamma << std::endl;  
00217                 //}
00218                 
00219         }
00220         
00221         delete theAlignments;
00222 
00223         std::vector<AlignTransformError> alignErrors = alignmentErrors->m_alignError;
00224         for (std::vector<AlignTransformError>::const_iterator i = alignErrors.begin(); i != alignErrors.end(); ++i){
00225 
00226                 m_rawid = i->rawId();
00227                 CLHEP::HepSymMatrix errMatrix = i->matrix();
00228                 DetId detid(m_rawid);
00229                 m_subdetid = detid.subdetId();
00230                 m_xx = errMatrix[0][0];
00231                 m_xy = errMatrix[0][1];
00232                 m_xz = errMatrix[0][2];
00233                 m_yy = errMatrix[1][1];
00234                 m_yz = errMatrix[1][2];
00235                 m_zz = errMatrix[2][2];
00236                 m_treeErrors->Fill();
00237         }
00238 
00239         // Get GeomDetUnits for the current tracker 
00240         std::vector<GeomDetUnit*>detUnits =  theCurTracker->detUnits() ; 
00241         int detUnit(0) ;
00242         //\\for (unsigned int iDet = 0; iDet < detUnits.size(); ++iDet) {
00243         for (std::vector<GeomDetUnit*>::const_iterator iunit = detUnits.begin(); iunit != detUnits.end(); ++iunit) { 
00244 
00245           DetId detid = (*iunit)->geographicalId(); 
00246           m_rawid = detid.rawId() ; 
00247           m_subdetid = detid.subdetId();
00248 
00249           ++detUnit ;            
00250           //\\GeomDetUnit* geomDetUnit = detUnits.at(iDet) ; 
00251           GeomDetUnit* geomDetUnit = *iunit ; 
00252 
00253           // Get SurfaceDeformation for this GeomDetUnit 
00254           if ( geomDetUnit->surfaceDeformation() ) {
00255             std::vector<double> surfaceDeformParams = (geomDetUnit->surfaceDeformation())->parameters() ; 
00256             //edm::LogInfo("surfaceDeformParamsSize") << " surfaceDeformParams size  = " << surfaceDeformParams.size() << std::endl ; 
00257             m_dNpar = surfaceDeformParams.size() ; 
00258             m_dtype = (geomDetUnit->surfaceDeformation())->type() ; 
00259             m_d1 = surfaceDeformParams.at(0) ; 
00260             m_d2 = surfaceDeformParams.at(1) ; 
00261             m_d3 = surfaceDeformParams.at(2) ; 
00262             mp_dpar->clear() ; 
00263             for (std::vector<double>::const_iterator it = surfaceDeformParams.begin(); it != surfaceDeformParams.end(); ++it) { 
00264               mp_dpar->push_back((*it)) ; 
00265               //edm::LogInfo("surfaceDeformParamsContent") << " surfaceDeformParam = " << (*it) << std::endl ;  
00266             }
00267             m_treeDeformations->Fill() ; 
00268           }
00269         }
00270         
00271         //write out 
00272         m_file->cd();
00273         m_tree->Write();
00274         m_treeDeformations->Write();
00275         m_treeErrors->Write();
00276         m_file->Close();
00277 }
00278 
00279 
00280 void TrackerGeometryIntoNtuples::addBranches() {
00281         
00282         m_tree->Branch("rawid", &m_rawid, "rawid/I");
00283         m_tree->Branch("x", &m_x, "x/D");
00284         m_tree->Branch("y", &m_y, "y/D");
00285         m_tree->Branch("z", &m_z, "z/D");
00286         m_tree->Branch("alpha", &m_alpha, "alpha/D");
00287         m_tree->Branch("beta", &m_beta, "beta/D");
00288         m_tree->Branch("gamma", &m_gamma, "gamma/D");
00289         
00290         m_treeDeformations->Branch("irawid", &m_rawid, "irawid/I");
00291         m_treeDeformations->Branch("subdetid", &m_subdetid, "subdetid/I");
00292         m_treeDeformations->Branch("dNpar", &m_dNpar, "dNpar/I"); 
00293         //m_treeDeformations->Branch("d1", &m_d1, "d1/D");
00294         //m_treeDeformations->Branch("d2", &m_d2, "d2/D");
00295         //m_treeDeformations->Branch("d3", &m_d3, "d3/D");
00296         m_treeDeformations->Branch("dtype", &m_dtype);
00297         m_treeDeformations->Branch("dpar", "std::vector<double>",  &mp_dpar);
00298         
00299         m_treeErrors->Branch("rawid", &m_rawid, "rawid/I");
00300         m_treeErrors->Branch("subdetid", &m_subdetid, "subdetid/I");
00301         m_treeErrors->Branch("xx", &m_xx, "xx/D");
00302         m_treeErrors->Branch("yy", &m_yy, "yy/D");
00303         m_treeErrors->Branch("zz", &m_zz, "zz/D");
00304         m_treeErrors->Branch("xy", &m_xy, "xy/D");
00305         m_treeErrors->Branch("xz", &m_xz, "xz/D");
00306         m_treeErrors->Branch("yz", &m_yz, "yz/D");
00307                 
00308 
00309         //m_tree->Branch("NumDeform",    &numDeformationValues_, "NumDeform/i");
00310         //m_tree->Branch("DeformValues", deformationValues_,     "DeformValues[NumDeform]/F");
00311 
00312 }
00313 
00314 
00315 //define this as a plug-in
00316 DEFINE_FWK_MODULE(TrackerGeometryIntoNtuples);