CMS 3D CMS Logo

Tracker_OldtoNewConverter.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Tracker_OldtoNewConverter.cc
4 // Class: Tracker_OldtoNewConverter
5 //
13 //
14 // Original Author: Nhan Tran
15 // Created: Mon Jul 16m 16:56:34 CDT 2007
16 // $Id: Tracker_OldtoNewConverter.cc,v 1.2 2010/01/04 18:24:37 mussgill Exp $
17 //
18 //
19 
20 // system include files
24 
25 #include <algorithm>
26 #include "TTree.h"
27 #include "TFile.h"
28 
30 
31 #include <fstream>
32 #include <iostream>
33 
34 //
35 // class decleration
36 //
37 
39 public:
41  ~Tracker_OldtoNewConverter() override;
42 
43 private:
44  void beginJob() override;
45  void analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) override;
46  void endJob() override;
47 
48  void createMap();
49  void addBranches();
50 
51  // ----------member data ---------------------------
52 
58 
59  std::map<uint32_t, uint32_t> theMap;
60 
61  TFile* m_inputTFile;
62  TFile* m_outputTFile;
63  TTree* m_inputTree;
64  TTree* m_outputTree;
65 
66  uint32_t rawid_i, rawid_f;
67  //int rawid_i, rawid_f;
68  double x_i, y_i, z_i, a_i, b_i, c_i;
69  double x_f, y_f, z_f, a_f, b_f, c_f;
70 };
71 
72 //
73 // constants, enums and typedefs
74 //
75 
76 //
77 // static data member definitions
78 //
79 
80 //
81 // constructors and destructor
82 //
84  : m_inputTFile(nullptr),
85  m_outputTFile(nullptr),
86  m_inputTree(nullptr),
87  m_outputTree(nullptr),
88  rawid_i(0),
89  rawid_f(0),
90  x_i(0.),
91  y_i(0.),
92  z_i(0.),
93  a_i(0.),
94  b_i(0.),
95  c_i(0.),
96  x_f(0.),
97  y_f(0.),
98  z_f(0.),
99  a_f(0.),
100  b_f(0.),
101  c_f(0.) {
102  m_conversionType = iConfig.getUntrackedParameter<std::string>("conversionType");
103  m_inputFile = iConfig.getUntrackedParameter<std::string>("inputFile");
104  m_outputFile = iConfig.getUntrackedParameter<std::string>("outputFile");
105  m_textFile = iConfig.getUntrackedParameter<std::string>("textFile");
106  m_treeName = iConfig.getUntrackedParameter<std::string>("treeName");
107 }
108 
110 
111 //
112 // member functions
113 //
114 
115 // ------------ method called to for each event ------------
117 
118 // ------------ method called once each job just before starting event loop ------------
120  m_inputTFile = new TFile(m_inputFile.c_str());
121  m_outputTFile = new TFile(m_outputFile.c_str(), "RECREATE");
122 
123  m_inputTree = (TTree*)m_inputTFile->Get(m_treeName.c_str());
124  m_outputTree = new TTree(m_treeName.c_str(), m_treeName.c_str());
125 
126  createMap();
127  addBranches();
128 
129  uint32_t nEntries = m_inputTree->GetEntries();
130  uint32_t iter = 0;
131  for (uint32_t i = 0; i < nEntries; ++i) {
132  m_inputTree->GetEntry(i);
133  std::map<uint32_t, uint32_t>::const_iterator it = theMap.find(rawid_i);
134 
135  if (it == theMap.end()) {
136  edm::LogInfo("ERROR") << "Error: couldn't find rawId: " << rawid_i;
137  iter++;
138  } else {
139  rawid_f = (it)->second;
140  x_f = x_i;
141  y_f = y_i;
142  z_f = z_i;
143  a_f = a_i;
144  b_f = b_i;
145  c_f = c_i;
146  m_outputTree->Fill();
147  }
148  }
149  edm::LogInfo("errors") << "Couldn't find: " << iter;
150  m_outputTFile->cd();
151  m_outputTree->Write();
152  m_outputTFile->Close();
153 }
154 
156  std::ifstream myfile(m_textFile.c_str());
157  if (!myfile.is_open())
158  throw cms::Exception("FileAccess") << "Unable to open input text file";
159 
160  uint32_t oldid;
161  uint32_t newid;
162 
163  uint32_t ctr = 0;
164  while (!myfile.eof() && myfile.good()) {
165  myfile >> oldid >> newid;
166 
167  //depends on conversion type: OldtoNew or NewtoOld
168  std::pair<uint32_t, uint32_t> pairType;
169  if (m_conversionType == "OldtoNew") {
170  pairType.first = oldid;
171  pairType.second = newid;
172  }
173  if (m_conversionType == "NewtoOld") {
174  pairType.first = newid;
175  pairType.second = oldid;
176  }
177 
178  theMap.insert(pairType);
179 
180  if (myfile.fail())
181  break;
182 
183  ctr++;
184  }
185  edm::LogInfo("Check") << ctr << " alignables read.";
186 }
187 
189  m_inputTree->SetBranchAddress("rawid", &rawid_i);
190  m_inputTree->SetBranchAddress("x", &x_i);
191  m_inputTree->SetBranchAddress("y", &y_i);
192  m_inputTree->SetBranchAddress("z", &z_i);
193  m_inputTree->SetBranchAddress("alpha", &a_i);
194  m_inputTree->SetBranchAddress("beta", &b_i);
195  m_inputTree->SetBranchAddress("gamma", &c_i);
196 
197  m_outputTree->Branch("rawid", &rawid_f, "rawid/I");
198  m_outputTree->Branch("x", &x_f, "x/D");
199  m_outputTree->Branch("y", &y_f, "y/D");
200  m_outputTree->Branch("z", &z_f, "z/D");
201  m_outputTree->Branch("alpha", &a_f, "alpha/D");
202  m_outputTree->Branch("beta", &b_f, "beta/D");
203  m_outputTree->Branch("gamma", &c_f, "gamma/D");
204 }
205 
206 // ------------ method called once each job just after ending the event loop ------------
208 
209 //define this as a plug-in
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
T getUntrackedParameter(std::string const &, T const &) const
U second(std::pair< T, U > const &p)
int iEvent
Definition: GenABIO.cc:224
void analyze(const edm::Event &iEvent, const edm::EventSetup &iSetup) override
Tracker_OldtoNewConverter(const edm::ParameterSet &)
std::map< uint32_t, uint32_t > theMap
Log< level::Info, false > LogInfo