CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_1/src/Alignment/CommonAlignmentMonitor/plugins/AlignmentMonitorAsAnalyzer.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    AlignmentMonitorAsAnalyzer
00004 // Class:      AlignmentMonitorAsAnalyzer
00005 // 
00013 //
00014 // Original Author:  Jim Pivarski
00015 //         Created:  Sat Apr 26 12:36:13 CDT 2008
00016 // $Id: AlignmentMonitorAsAnalyzer.cc,v 1.9 2012/07/13 09:18:40 yana Exp $
00017 //
00018 //
00019 
00020 
00021 // system include files
00022 #include <memory>
00023 
00024 // user include files
00025 #include "FWCore/Framework/interface/Frameworkfwd.h"
00026 #include "FWCore/Framework/interface/EDAnalyzer.h"
00027 #include "FWCore/Framework/interface/Event.h"
00028 #include "FWCore/Framework/interface/MakerMacros.h"
00029 #include "FWCore/Framework/interface/ESHandle.h"
00030 #include "FWCore/Framework/interface/ESTransientHandle.h"
00031 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00032 
00033 #include "Alignment/CommonAlignmentMonitor/interface/AlignmentMonitorBase.h"
00034 #include "Alignment/CommonAlignmentMonitor/interface/AlignmentMonitorPluginFactory.h"
00035 #include "TrackingTools/PatternTools/interface/TrajTrackAssociation.h"
00036 #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentParameterStore.h"
00037 #include "Alignment/CommonAlignment/interface/Alignable.h"
00038 
00039 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
00040 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeomBuilderFromGeometricDet.h"
00041 #include "Geometry/DTGeometry/interface/DTGeometry.h"
00042 #include "Geometry/CSCGeometry/interface/CSCGeometry.h"
00043 #include "Geometry/Records/interface/MuonNumberingRecord.h"
00044 #include "Geometry/MuonNumbering/interface/MuonDDDConstants.h"
00045 #include "Geometry/DTGeometryBuilder/src/DTGeometryBuilderFromDDD.h"
00046 #include "Geometry/CSCGeometryBuilder/src/CSCGeometryBuilderFromDDD.h"
00047 #include "Geometry/TrackingGeometryAligner/interface/GeometryAligner.h"
00048 #include "CondFormats/AlignmentRecord/interface/TrackerAlignmentRcd.h"
00049 #include "CondFormats/AlignmentRecord/interface/TrackerAlignmentErrorRcd.h"
00050 #include "CondFormats/AlignmentRecord/interface/DTAlignmentRcd.h"
00051 #include "CondFormats/AlignmentRecord/interface/DTAlignmentErrorRcd.h"
00052 #include "CondFormats/AlignmentRecord/interface/CSCAlignmentRcd.h"
00053 #include "CondFormats/AlignmentRecord/interface/CSCAlignmentErrorRcd.h"
00054 #include "CondFormats/AlignmentRecord/interface/GlobalPositionRcd.h"
00055 #include "CondFormats/Alignment/interface/DetectorGlobalPosition.h"
00056 //
00057 // class decleration
00058 //
00059 
00060 class AlignmentMonitorAsAnalyzer : public edm::EDAnalyzer {
00061    public:
00062       explicit AlignmentMonitorAsAnalyzer(const edm::ParameterSet&);
00063       ~AlignmentMonitorAsAnalyzer();
00064 
00065       typedef std::pair<const Trajectory*, const reco::Track*> ConstTrajTrackPair; 
00066       typedef std::vector<ConstTrajTrackPair> ConstTrajTrackPairCollection;
00067 
00068    private:
00069       virtual void beginJob();
00070       virtual void analyze(const edm::Event&, const edm::EventSetup&);
00071       virtual void endJob();
00072 
00073       // ----------member data ---------------------------
00074       edm::InputTag m_tjTag;
00075       edm::ParameterSet m_aliParamStoreCfg;
00076   const edm::ParameterSet m_pSet;
00077 
00078       AlignableTracker *m_alignableTracker;
00079       AlignableMuon *m_alignableMuon;
00080       AlignmentParameterStore *m_alignmentParameterStore;
00081 
00082       std::vector<AlignmentMonitorBase*> m_monitors;
00083       const edm::EventSetup *m_lastSetup;
00084 
00085       bool m_firstEvent;
00086 };
00087 
00088 //
00089 // constants, enums and typedefs
00090 //
00091 
00092 //
00093 // static data member definitions
00094 //
00095 
00096 //
00097 // constructors and destructor
00098 //
00099 AlignmentMonitorAsAnalyzer::AlignmentMonitorAsAnalyzer(const edm::ParameterSet& iConfig)
00100    : m_tjTag(iConfig.getParameter<edm::InputTag>("tjTkAssociationMapTag"))
00101      , m_aliParamStoreCfg(iConfig.getParameter<edm::ParameterSet>("ParameterStore")),
00102      m_pSet(iConfig)
00103    , m_alignableTracker(NULL)
00104    , m_alignableMuon(NULL)
00105    , m_alignmentParameterStore(NULL)
00106 {
00107    std::vector<std::string> monitors = iConfig.getUntrackedParameter<std::vector<std::string> >( "monitors" );
00108 
00109    for (std::vector<std::string>::const_iterator miter = monitors.begin();  miter != monitors.end();  ++miter) {
00110       AlignmentMonitorBase* newMonitor = AlignmentMonitorPluginFactory::get()->create(*miter, iConfig.getUntrackedParameter<edm::ParameterSet>(*miter));
00111 
00112       if (!newMonitor) throw cms::Exception("BadConfig") << "Couldn't find monitor named " << *miter;
00113 
00114       m_monitors.push_back(newMonitor);
00115    }
00116 }
00117 
00118 
00119 AlignmentMonitorAsAnalyzer::~AlignmentMonitorAsAnalyzer()
00120 {
00121    delete m_alignableTracker;
00122    delete m_alignableMuon;
00123    delete m_alignmentParameterStore;
00124 }
00125 
00126 
00127 //
00128 // member functions
00129 //
00130 
00131 // ------------ method called to for each event  ------------
00132 void
00133 AlignmentMonitorAsAnalyzer::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
00134 {
00135    if (m_firstEvent) {
00136       GeometryAligner aligner;
00137     
00138       edm::ESTransientHandle<DDCompactView> cpv;
00139       iSetup.get<IdealGeometryRecord>().get( cpv );
00140       
00141       edm::ESHandle<GeometricDet> theGeometricDet;
00142       iSetup.get<IdealGeometryRecord>().get( theGeometricDet );
00143       TrackerGeomBuilderFromGeometricDet trackerBuilder;
00144       boost::shared_ptr<TrackerGeometry> theTracker(trackerBuilder.build(&(*theGeometricDet), m_pSet ));
00145       
00146       edm::ESHandle<MuonDDDConstants> mdc;
00147       iSetup.get<MuonNumberingRecord>().get(mdc);
00148       DTGeometryBuilderFromDDD DTGeometryBuilder;
00149       CSCGeometryBuilderFromDDD CSCGeometryBuilder;
00150       boost::shared_ptr<DTGeometry> theMuonDT(new DTGeometry);
00151       DTGeometryBuilder.build(theMuonDT, &(*cpv), *mdc);
00152       boost::shared_ptr<CSCGeometry> theMuonCSC(new CSCGeometry);
00153       CSCGeometryBuilder.build(theMuonCSC, &(*cpv), *mdc);
00154       
00155       edm::ESHandle<Alignments> globalPositionRcd;
00156       iSetup.get<GlobalPositionRcd>().get(globalPositionRcd);
00157       
00158       edm::ESHandle<Alignments> alignments;
00159       iSetup.get<TrackerAlignmentRcd>().get( alignments );
00160       edm::ESHandle<AlignmentErrors> alignmentErrors;
00161       iSetup.get<TrackerAlignmentErrorRcd>().get( alignmentErrors );
00162       aligner.applyAlignments<TrackerGeometry>( &(*theTracker), &(*alignments), &(*alignmentErrors),
00163                                                 align::DetectorGlobalPosition(*globalPositionRcd, DetId(DetId::Tracker)) );
00164       
00165       edm::ESHandle<Alignments> dtAlignments;
00166       iSetup.get<DTAlignmentRcd>().get( dtAlignments );
00167       edm::ESHandle<AlignmentErrors> dtAlignmentErrors;
00168       iSetup.get<DTAlignmentErrorRcd>().get( dtAlignmentErrors );
00169       aligner.applyAlignments<DTGeometry>( &(*theMuonDT), &(*dtAlignments), &(*dtAlignmentErrors),
00170                                            align::DetectorGlobalPosition(*globalPositionRcd, DetId(DetId::Muon)) );
00171       
00172       edm::ESHandle<Alignments> cscAlignments;
00173       iSetup.get<CSCAlignmentRcd>().get( cscAlignments );
00174       edm::ESHandle<AlignmentErrors> cscAlignmentErrors;
00175       iSetup.get<CSCAlignmentErrorRcd>().get( cscAlignmentErrors );
00176       aligner.applyAlignments<CSCGeometry>( &(*theMuonCSC), &(*cscAlignments), &(*cscAlignmentErrors),
00177                                             align::DetectorGlobalPosition(*globalPositionRcd, DetId(DetId::Muon)) );
00178       
00179       // within an analyzer, modules can't expect to see any selected alignables!
00180       std::vector<Alignable*> empty_alignables;
00181       
00182       m_alignableTracker = new AlignableTracker( &(*theTracker) );
00183       m_alignableMuon = new AlignableMuon( &(*theMuonDT), &(*theMuonCSC) );
00184       m_alignmentParameterStore = new AlignmentParameterStore(empty_alignables, m_aliParamStoreCfg);
00185       
00186       for (std::vector<AlignmentMonitorBase*>::const_iterator monitor = m_monitors.begin();  monitor != m_monitors.end();  ++monitor) {
00187         (*monitor)->beginOfJob(m_alignableTracker, m_alignableMuon, m_alignmentParameterStore);
00188       }
00189       for (std::vector<AlignmentMonitorBase*>::const_iterator monitor = m_monitors.begin();  monitor != m_monitors.end();  ++monitor) {
00190         (*monitor)->startingNewLoop();
00191       }
00192       
00193       m_firstEvent = false;
00194    }
00195 
00196    // Retrieve trajectories and tracks from the event
00197    edm::Handle<TrajTrackAssociationCollection> trajTracksMap;
00198    iEvent.getByLabel(m_tjTag, trajTracksMap);
00199    
00200    // Form pairs of trajectories and tracks
00201    ConstTrajTrackPairCollection trajTracks;
00202    for (TrajTrackAssociationCollection::const_iterator iPair = trajTracksMap->begin();  iPair != trajTracksMap->end();  ++iPair) {
00203       trajTracks.push_back(ConstTrajTrackPair(&(*(*iPair).key), &(*(*iPair).val)));
00204    }
00205    
00206    // Run the monitors
00207    for (std::vector<AlignmentMonitorBase*>::const_iterator monitor = m_monitors.begin();  monitor != m_monitors.end();  ++monitor) {
00208       (*monitor)->duringLoop(iEvent, iSetup, trajTracks);
00209    }
00210 
00211    // Keep this for endOfLoop (why does endOfLoop want iSetup???)
00212    m_lastSetup = &iSetup;
00213 }
00214 
00215 
00216 // ------------ method called once each job just before starting event loop  ------------
00217 void 
00218 AlignmentMonitorAsAnalyzer::beginJob()
00219 {
00220    m_firstEvent = true;
00221 }
00222 
00223 // ------------ method called once each job just after ending the event loop  ------------
00224 void 
00225 AlignmentMonitorAsAnalyzer::endJob() 
00226 {
00227    for (std::vector<AlignmentMonitorBase*>::const_iterator monitor = m_monitors.begin();  monitor != m_monitors.end();  ++monitor) {
00228       (*monitor)->endOfLoop(*m_lastSetup);
00229    }
00230    for (std::vector<AlignmentMonitorBase*>::const_iterator monitor = m_monitors.begin();  monitor != m_monitors.end();  ++monitor) {
00231       (*monitor)->endOfJob();
00232    }
00233 }
00234 
00235 //define this as a plug-in
00236 DEFINE_FWK_MODULE(AlignmentMonitorAsAnalyzer);