CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/Alignment/OfflineValidation/plugins/Tracker_OldtoNewConverter.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    Tracker_OldtoNewConverter.cc
00004 // Class:      Tracker_OldtoNewConverter
00005 // 
00013 //
00014 // Original Author:  Nhan Tran
00015 //         Created:  Mon Jul 16m 16:56:34 CDT 2007
00016 // $Id: Tracker_OldtoNewConverter.cc,v 1.2 2010/01/04 18:24:37 mussgill Exp $
00017 //
00018 //
00019 
00020 // system include files
00021 #include "FWCore/Framework/interface/EDAnalyzer.h"
00022 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00023 #include "FWCore/Framework/interface/MakerMacros.h"
00024 
00025 
00026 #include <algorithm>
00027 #include "TTree.h"
00028 #include "TFile.h"
00029 
00030 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00031 
00032 
00033 #include <fstream>
00034 #include <iostream>
00035 
00036 //
00037 // class decleration
00038 //
00039 
00040 class Tracker_OldtoNewConverter : public edm::EDAnalyzer {
00041 public:
00042         explicit Tracker_OldtoNewConverter(const edm::ParameterSet&);
00043         ~Tracker_OldtoNewConverter();
00044         
00045         
00046 private:
00047         virtual void beginJob();
00048         virtual void analyze(const edm::Event &iEvent, const edm::EventSetup &iSetup);
00049         virtual void endJob() ;
00050         
00051         void createMap();
00052         void addBranches();
00053         
00054         // ----------member data ---------------------------
00055 
00056         std::string m_conversionType;
00057         std::string m_textFile;
00058         std::string m_inputFile;
00059         std::string m_outputFile;
00060         std::string m_treeName;
00061         
00062         std::map< uint32_t, uint32_t > theMap;
00063 
00064         TFile* m_inputTFile;
00065         TFile* m_outputTFile;
00066         TTree* m_inputTree;
00067         TTree* m_outputTree;
00068         
00069         
00070         uint32_t rawid_i, rawid_f;
00071         //int rawid_i, rawid_f;
00072         double x_i, y_i, z_i, a_i, b_i, c_i;
00073         double x_f, y_f, z_f, a_f, b_f, c_f;
00074         
00075         
00076 };
00077 
00078 //
00079 // constants, enums and typedefs
00080 //
00081 
00082 //
00083 // static data member definitions
00084 //
00085 
00086 //
00087 // constructors and destructor
00088 //
00089 Tracker_OldtoNewConverter::Tracker_OldtoNewConverter(const edm::ParameterSet& iConfig)
00090 {
00091         m_conversionType = iConfig.getUntrackedParameter< std::string > ("conversionType");
00092         m_inputFile = iConfig.getUntrackedParameter< std::string > ("inputFile");
00093         m_outputFile = iConfig.getUntrackedParameter< std::string > ("outputFile");
00094         m_textFile = iConfig.getUntrackedParameter< std::string > ("textFile");
00095         m_treeName = iConfig.getUntrackedParameter< std::string > ("treeName");
00096 }
00097 
00098 
00099 Tracker_OldtoNewConverter::~Tracker_OldtoNewConverter()
00100 {}
00101 
00102 
00103 //
00104 // member functions
00105 //
00106 
00107 // ------------ method called to for each event  ------------
00108 void Tracker_OldtoNewConverter::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
00109 {}
00110 
00111 
00112 // ------------ method called once each job just before starting event loop  ------------
00113 void Tracker_OldtoNewConverter::beginJob()
00114 {
00115         
00116         m_inputTFile = new TFile(m_inputFile.c_str());
00117         m_outputTFile = new TFile(m_outputFile.c_str(),"RECREATE");
00118 
00119         m_inputTree = (TTree*) m_inputTFile->Get(m_treeName.c_str());
00120         m_outputTree = new TTree(m_treeName.c_str(), m_treeName.c_str());
00121         
00122         createMap();
00123         addBranches();
00124 
00125         uint32_t nEntries = m_inputTree->GetEntries();
00126         uint32_t iter = 0;
00127         for (uint32_t i = 0; i < nEntries; ++i){
00128                 m_inputTree->GetEntry(i);
00129                 std::map< uint32_t, uint32_t >::const_iterator it = theMap.find(rawid_i);
00130                 
00131                 if (it == theMap.end()){
00132                         edm::LogInfo("ERROR") << "Error: couldn't find rawId: " << rawid_i;
00133                         iter++;
00134                 }
00135                 else{
00136                         rawid_f = (it)->second;
00137                         x_f = x_i;
00138                         y_f = y_i;
00139                         z_f = z_i;
00140                         a_f = a_i;
00141                         b_f = b_i;
00142                         c_f = c_i;
00143                         m_outputTree->Fill();
00144                 }
00145 
00146         }
00147         edm::LogInfo("errors") << "Couldn't find: " << iter;
00148         m_outputTFile->cd();
00149         m_outputTree->Write();
00150         m_outputTFile->Close();
00151 }
00152 
00153 
00154 
00155 void Tracker_OldtoNewConverter::createMap() {
00156 
00157         std::ifstream myfile( m_textFile.c_str() );
00158         if (!myfile.is_open())
00159                 throw cms::Exception("FileAccess") << "Unable to open input text file";
00160 
00161         uint32_t oldid;
00162         uint32_t newid;
00163 
00164         uint32_t ctr = 0;
00165         while( !myfile.eof() && myfile.good() ){
00166 
00167                 myfile >> oldid >> newid;
00168 
00169                 //depends on conversion type: OldtoNew or NewtoOld
00170                 std::pair< uint32_t, uint32_t > pairType;
00171                 if (m_conversionType == "OldtoNew") {pairType.first = oldid; pairType.second = newid;}
00172                 if (m_conversionType == "NewtoOld") {pairType.first = newid; pairType.second = oldid;}
00173 
00174 
00175                 theMap.insert( pairType );
00176 
00177                 if (myfile.fail()) break;
00178 
00179                 ctr++;
00180         }
00181         edm::LogInfo("Check") << ctr << " alignables read.";
00182 }
00183 
00184 void Tracker_OldtoNewConverter::addBranches(){
00185 
00186         m_inputTree->SetBranchAddress("rawid", &rawid_i);
00187         m_inputTree->SetBranchAddress("x", &x_i);
00188         m_inputTree->SetBranchAddress("y", &y_i);
00189         m_inputTree->SetBranchAddress("z", &z_i);
00190         m_inputTree->SetBranchAddress("alpha", &a_i);
00191         m_inputTree->SetBranchAddress("beta", &b_i);
00192         m_inputTree->SetBranchAddress("gamma", &c_i);
00193 
00194         m_outputTree->Branch("rawid", &rawid_f, "rawid/I");
00195         m_outputTree->Branch("x", &x_f, "x/D");
00196         m_outputTree->Branch("y", &y_f, "y/D");
00197         m_outputTree->Branch("z", &z_f, "z/D");
00198         m_outputTree->Branch("alpha", &a_f, "alpha/D");
00199         m_outputTree->Branch("beta", &b_f, "beta/D");
00200         m_outputTree->Branch("gamma", &c_f, "gamma/D");
00201 
00202 }
00203 
00204 
00205 // ------------ method called once each job just after ending the event loop  ------------
00206 void Tracker_OldtoNewConverter::endJob() {
00207 }
00208 
00209 
00210 
00211 //define this as a plug-in
00212 DEFINE_FWK_MODULE(Tracker_OldtoNewConverter);