Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:45:56

0001 // -*- C++ -*-
0002 //
0003 // Package:    Tracker_OldtoNewConverter.cc
0004 // Class:      Tracker_OldtoNewConverter
0005 //
0006 /**\class MuonGeometryIntoNtuples MuonGeometryIntoNtuples.cc Alignment/MuonGeometryIntoNtuples/src/MuonGeometryIntoNtuples.cc
0007 
0008  Description: <one line class summary>
0009 
0010  Implementation:
0011      <Notes on implementation>
0012 */
0013 //
0014 // Original Author:  Nhan Tran
0015 //         Created:  Mon Jul 16m 16:56:34 CDT 2007
0016 // $Id: Tracker_OldtoNewConverter.cc,v 1.2 2010/01/04 18:24:37 mussgill Exp $
0017 //
0018 //
0019 
0020 // system include files
0021 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0022 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0023 #include "FWCore/Framework/interface/MakerMacros.h"
0024 
0025 #include <algorithm>
0026 #include "TTree.h"
0027 #include "TFile.h"
0028 
0029 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0030 
0031 #include <fstream>
0032 #include <iostream>
0033 
0034 //
0035 // class decleration
0036 //
0037 
0038 class Tracker_OldtoNewConverter : public edm::one::EDAnalyzer<> {
0039 public:
0040   explicit Tracker_OldtoNewConverter(const edm::ParameterSet&);
0041   ~Tracker_OldtoNewConverter() override;
0042 
0043 private:
0044   void beginJob() override;
0045   void analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) override;
0046   void endJob() override;
0047 
0048   void createMap();
0049   void addBranches();
0050 
0051   // ----------member data ---------------------------
0052 
0053   std::string m_conversionType;
0054   std::string m_textFile;
0055   std::string m_inputFile;
0056   std::string m_outputFile;
0057   std::string m_treeName;
0058 
0059   std::map<uint32_t, uint32_t> theMap;
0060 
0061   TFile* m_inputTFile;
0062   TFile* m_outputTFile;
0063   TTree* m_inputTree;
0064   TTree* m_outputTree;
0065 
0066   uint32_t rawid_i, rawid_f;
0067   //int rawid_i, rawid_f;
0068   double x_i, y_i, z_i, a_i, b_i, c_i;
0069   double x_f, y_f, z_f, a_f, b_f, c_f;
0070 };
0071 
0072 //
0073 // constants, enums and typedefs
0074 //
0075 
0076 //
0077 // static data member definitions
0078 //
0079 
0080 //
0081 // constructors and destructor
0082 //
0083 Tracker_OldtoNewConverter::Tracker_OldtoNewConverter(const edm::ParameterSet& iConfig)
0084     : m_inputTFile(nullptr),
0085       m_outputTFile(nullptr),
0086       m_inputTree(nullptr),
0087       m_outputTree(nullptr),
0088       rawid_i(0),
0089       rawid_f(0),
0090       x_i(0.),
0091       y_i(0.),
0092       z_i(0.),
0093       a_i(0.),
0094       b_i(0.),
0095       c_i(0.),
0096       x_f(0.),
0097       y_f(0.),
0098       z_f(0.),
0099       a_f(0.),
0100       b_f(0.),
0101       c_f(0.) {
0102   m_conversionType = iConfig.getUntrackedParameter<std::string>("conversionType");
0103   m_inputFile = iConfig.getUntrackedParameter<std::string>("inputFile");
0104   m_outputFile = iConfig.getUntrackedParameter<std::string>("outputFile");
0105   m_textFile = iConfig.getUntrackedParameter<std::string>("textFile");
0106   m_treeName = iConfig.getUntrackedParameter<std::string>("treeName");
0107 }
0108 
0109 Tracker_OldtoNewConverter::~Tracker_OldtoNewConverter() {}
0110 
0111 //
0112 // member functions
0113 //
0114 
0115 // ------------ method called to for each event  ------------
0116 void Tracker_OldtoNewConverter::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {}
0117 
0118 // ------------ method called once each job just before starting event loop  ------------
0119 void Tracker_OldtoNewConverter::beginJob() {
0120   m_inputTFile = new TFile(m_inputFile.c_str());
0121   m_outputTFile = new TFile(m_outputFile.c_str(), "RECREATE");
0122 
0123   m_inputTree = (TTree*)m_inputTFile->Get(m_treeName.c_str());
0124   m_outputTree = new TTree(m_treeName.c_str(), m_treeName.c_str());
0125 
0126   createMap();
0127   addBranches();
0128 
0129   uint32_t nEntries = m_inputTree->GetEntries();
0130   uint32_t iter = 0;
0131   for (uint32_t i = 0; i < nEntries; ++i) {
0132     m_inputTree->GetEntry(i);
0133     std::map<uint32_t, uint32_t>::const_iterator it = theMap.find(rawid_i);
0134 
0135     if (it == theMap.end()) {
0136       edm::LogInfo("ERROR") << "Error: couldn't find rawId: " << rawid_i;
0137       iter++;
0138     } else {
0139       rawid_f = (it)->second;
0140       x_f = x_i;
0141       y_f = y_i;
0142       z_f = z_i;
0143       a_f = a_i;
0144       b_f = b_i;
0145       c_f = c_i;
0146       m_outputTree->Fill();
0147     }
0148   }
0149   edm::LogInfo("errors") << "Couldn't find: " << iter;
0150   m_outputTFile->cd();
0151   m_outputTree->Write();
0152   m_outputTFile->Close();
0153 }
0154 
0155 void Tracker_OldtoNewConverter::createMap() {
0156   std::ifstream myfile(m_textFile.c_str());
0157   if (!myfile.is_open())
0158     throw cms::Exception("FileAccess") << "Unable to open input text file";
0159 
0160   uint32_t oldid;
0161   uint32_t newid;
0162 
0163   uint32_t ctr = 0;
0164   while (!myfile.eof() && myfile.good()) {
0165     myfile >> oldid >> newid;
0166 
0167     //depends on conversion type: OldtoNew or NewtoOld
0168     std::pair<uint32_t, uint32_t> pairType;
0169     if (m_conversionType == "OldtoNew") {
0170       pairType.first = oldid;
0171       pairType.second = newid;
0172     }
0173     if (m_conversionType == "NewtoOld") {
0174       pairType.first = newid;
0175       pairType.second = oldid;
0176     }
0177 
0178     theMap.insert(pairType);
0179 
0180     if (myfile.fail())
0181       break;
0182 
0183     ctr++;
0184   }
0185   edm::LogInfo("Check") << ctr << " alignables read.";
0186 }
0187 
0188 void Tracker_OldtoNewConverter::addBranches() {
0189   m_inputTree->SetBranchAddress("rawid", &rawid_i);
0190   m_inputTree->SetBranchAddress("x", &x_i);
0191   m_inputTree->SetBranchAddress("y", &y_i);
0192   m_inputTree->SetBranchAddress("z", &z_i);
0193   m_inputTree->SetBranchAddress("alpha", &a_i);
0194   m_inputTree->SetBranchAddress("beta", &b_i);
0195   m_inputTree->SetBranchAddress("gamma", &c_i);
0196 
0197   m_outputTree->Branch("rawid", &rawid_f, "rawid/I");
0198   m_outputTree->Branch("x", &x_f, "x/D");
0199   m_outputTree->Branch("y", &y_f, "y/D");
0200   m_outputTree->Branch("z", &z_f, "z/D");
0201   m_outputTree->Branch("alpha", &a_f, "alpha/D");
0202   m_outputTree->Branch("beta", &b_f, "beta/D");
0203   m_outputTree->Branch("gamma", &c_f, "gamma/D");
0204 }
0205 
0206 // ------------ method called once each job just after ending the event loop  ------------
0207 void Tracker_OldtoNewConverter::endJob() {}
0208 
0209 //define this as a plug-in
0210 DEFINE_FWK_MODULE(Tracker_OldtoNewConverter);