CMS 3D CMS Logo

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