Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:57:09

0001 // -*- C++ -*-
0002 //
0003 // Package:    Tracker_OldtoNewConverter.cc
0004 // Class:      Tracker_OldtoNewConverter
0005 //
0006 /**\class Tracker_OldtoNewConverter Tracker_OldtoNewConverter.cc Alignment/OfflineValidation/plugins/Tracker_OldtoNewConverter.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 = default;
0042 
0043   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0044 
0045 private:
0046   void beginJob() override;
0047   void analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) override;
0048   void endJob() override;
0049 
0050   void createMap();
0051   void addBranches();
0052 
0053   // ----------member data ---------------------------
0054 
0055   std::string m_conversionType;
0056   std::string m_textFile;
0057   std::string m_inputFile;
0058   std::string m_outputFile;
0059   std::string m_treeName;
0060 
0061   std::map<uint32_t, uint32_t> theMap;
0062 
0063   TFile* m_inputTFile;
0064   TFile* m_outputTFile;
0065   TTree* m_inputTree;
0066   TTree* m_outputTree;
0067 
0068   uint32_t rawid_i, rawid_f;
0069   //int rawid_i, rawid_f;
0070   double x_i, y_i, z_i, a_i, b_i, c_i;
0071   double x_f, y_f, z_f, a_f, b_f, c_f;
0072 };
0073 
0074 //
0075 // constants, enums and typedefs
0076 //
0077 
0078 //
0079 // static data member definitions
0080 //
0081 
0082 //
0083 // constructors and destructor
0084 //
0085 Tracker_OldtoNewConverter::Tracker_OldtoNewConverter(const edm::ParameterSet& iConfig)
0086     : m_inputTFile(nullptr),
0087       m_outputTFile(nullptr),
0088       m_inputTree(nullptr),
0089       m_outputTree(nullptr),
0090       rawid_i(0),
0091       rawid_f(0),
0092       x_i(0.),
0093       y_i(0.),
0094       z_i(0.),
0095       a_i(0.),
0096       b_i(0.),
0097       c_i(0.),
0098       x_f(0.),
0099       y_f(0.),
0100       z_f(0.),
0101       a_f(0.),
0102       b_f(0.),
0103       c_f(0.) {
0104   m_conversionType = iConfig.getUntrackedParameter<std::string>("conversionType");
0105   m_inputFile = iConfig.getUntrackedParameter<std::string>("inputFile");
0106   m_outputFile = iConfig.getUntrackedParameter<std::string>("outputFile");
0107   m_textFile = iConfig.getUntrackedParameter<std::string>("textFile");
0108   m_treeName = iConfig.getUntrackedParameter<std::string>("treeName");
0109 }
0110 
0111 //
0112 // member functions
0113 //
0114 
0115 void Tracker_OldtoNewConverter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0116   edm::ParameterSetDescription desc;
0117   desc.setComment("Converts tracker geometry comparison trees");
0118   desc.addUntracked<std::string>("conversionType", {});
0119   desc.addUntracked<std::string>("inputFile", {});
0120   desc.addUntracked<std::string>("outputFile", {});
0121   desc.addUntracked<std::string>("textFile", {});
0122   desc.addUntracked<std::string>("treeName", {});
0123   descriptions.addWithDefaultLabel(desc);
0124 }
0125 
0126 // ------------ method called to for each event  ------------
0127 void Tracker_OldtoNewConverter::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {}
0128 
0129 // ------------ method called once each job just before starting event loop  ------------
0130 void Tracker_OldtoNewConverter::beginJob() {
0131   m_inputTFile = new TFile(m_inputFile.c_str());
0132   m_outputTFile = new TFile(m_outputFile.c_str(), "RECREATE");
0133 
0134   m_inputTree = (TTree*)m_inputTFile->Get(m_treeName.c_str());
0135   m_outputTree = new TTree(m_treeName.c_str(), m_treeName.c_str());
0136 
0137   createMap();
0138   addBranches();
0139 
0140   uint32_t nEntries = m_inputTree->GetEntries();
0141   uint32_t iter = 0;
0142   for (uint32_t i = 0; i < nEntries; ++i) {
0143     m_inputTree->GetEntry(i);
0144     std::map<uint32_t, uint32_t>::const_iterator it = theMap.find(rawid_i);
0145 
0146     if (it == theMap.end()) {
0147       edm::LogInfo("ERROR") << "Error: couldn't find rawId: " << rawid_i;
0148       iter++;
0149     } else {
0150       rawid_f = (it)->second;
0151       x_f = x_i;
0152       y_f = y_i;
0153       z_f = z_i;
0154       a_f = a_i;
0155       b_f = b_i;
0156       c_f = c_i;
0157       m_outputTree->Fill();
0158     }
0159   }
0160   edm::LogInfo("errors") << "Couldn't find: " << iter;
0161   m_outputTFile->cd();
0162   m_outputTree->Write();
0163   m_outputTFile->Close();
0164 }
0165 
0166 void Tracker_OldtoNewConverter::createMap() {
0167   std::ifstream myfile(m_textFile.c_str());
0168   if (!myfile.is_open())
0169     throw cms::Exception("FileAccess") << "Unable to open input text file";
0170 
0171   uint32_t oldid;
0172   uint32_t newid;
0173 
0174   uint32_t ctr = 0;
0175   while (!myfile.eof() && myfile.good()) {
0176     myfile >> oldid >> newid;
0177 
0178     //depends on conversion type: OldtoNew or NewtoOld
0179     std::pair<uint32_t, uint32_t> pairType;
0180     if (m_conversionType == "OldtoNew") {
0181       pairType.first = oldid;
0182       pairType.second = newid;
0183     }
0184     if (m_conversionType == "NewtoOld") {
0185       pairType.first = newid;
0186       pairType.second = oldid;
0187     }
0188 
0189     theMap.insert(pairType);
0190 
0191     if (myfile.fail())
0192       break;
0193 
0194     ctr++;
0195   }
0196   edm::LogInfo("Check") << ctr << " alignables read.";
0197 }
0198 
0199 void Tracker_OldtoNewConverter::addBranches() {
0200   m_inputTree->SetBranchAddress("rawid", &rawid_i);
0201   m_inputTree->SetBranchAddress("x", &x_i);
0202   m_inputTree->SetBranchAddress("y", &y_i);
0203   m_inputTree->SetBranchAddress("z", &z_i);
0204   m_inputTree->SetBranchAddress("alpha", &a_i);
0205   m_inputTree->SetBranchAddress("beta", &b_i);
0206   m_inputTree->SetBranchAddress("gamma", &c_i);
0207 
0208   m_outputTree->Branch("rawid", &rawid_f, "rawid/I");
0209   m_outputTree->Branch("x", &x_f, "x/D");
0210   m_outputTree->Branch("y", &y_f, "y/D");
0211   m_outputTree->Branch("z", &z_f, "z/D");
0212   m_outputTree->Branch("alpha", &a_f, "alpha/D");
0213   m_outputTree->Branch("beta", &b_f, "beta/D");
0214   m_outputTree->Branch("gamma", &c_f, "gamma/D");
0215 }
0216 
0217 // ------------ method called once each job just after ending the event loop  ------------
0218 void Tracker_OldtoNewConverter::endJob() {}
0219 
0220 //define this as a plug-in
0221 DEFINE_FWK_MODULE(Tracker_OldtoNewConverter);