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 = default;
42 
43  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
44 
45 private:
46  void beginJob() override;
47  void analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) override;
48  void endJob() override;
49 
50  void createMap();
51  void addBranches();
52 
53  // ----------member data ---------------------------
54 
60 
61  std::map<uint32_t, uint32_t> theMap;
62 
63  TFile* m_inputTFile;
64  TFile* m_outputTFile;
65  TTree* m_inputTree;
66  TTree* m_outputTree;
67 
68  uint32_t rawid_i, rawid_f;
69  //int rawid_i, rawid_f;
70  double x_i, y_i, z_i, a_i, b_i, c_i;
71  double x_f, y_f, z_f, a_f, b_f, c_f;
72 };
73 
74 //
75 // constants, enums and typedefs
76 //
77 
78 //
79 // static data member definitions
80 //
81 
82 //
83 // constructors and destructor
84 //
86  : m_inputTFile(nullptr),
87  m_outputTFile(nullptr),
88  m_inputTree(nullptr),
89  m_outputTree(nullptr),
90  rawid_i(0),
91  rawid_f(0),
92  x_i(0.),
93  y_i(0.),
94  z_i(0.),
95  a_i(0.),
96  b_i(0.),
97  c_i(0.),
98  x_f(0.),
99  y_f(0.),
100  z_f(0.),
101  a_f(0.),
102  b_f(0.),
103  c_f(0.) {
104  m_conversionType = iConfig.getUntrackedParameter<std::string>("conversionType");
105  m_inputFile = iConfig.getUntrackedParameter<std::string>("inputFile");
106  m_outputFile = iConfig.getUntrackedParameter<std::string>("outputFile");
107  m_textFile = iConfig.getUntrackedParameter<std::string>("textFile");
108  m_treeName = iConfig.getUntrackedParameter<std::string>("treeName");
109 }
110 
111 //
112 // member functions
113 //
114 
117  desc.setComment("Converts tracker geometry comparison trees");
118  desc.addUntracked<std::string>("conversionType", {});
119  desc.addUntracked<std::string>("inputFile", {});
120  desc.addUntracked<std::string>("outputFile", {});
121  desc.addUntracked<std::string>("textFile", {});
122  desc.addUntracked<std::string>("treeName", {});
123  descriptions.addWithDefaultLabel(desc);
124 }
125 
126 // ------------ method called to for each event ------------
128 
129 // ------------ method called once each job just before starting event loop ------------
131  m_inputTFile = new TFile(m_inputFile.c_str());
132  m_outputTFile = new TFile(m_outputFile.c_str(), "RECREATE");
133 
134  m_inputTree = (TTree*)m_inputTFile->Get(m_treeName.c_str());
135  m_outputTree = new TTree(m_treeName.c_str(), m_treeName.c_str());
136 
137  createMap();
138  addBranches();
139 
140  uint32_t nEntries = m_inputTree->GetEntries();
141  uint32_t iter = 0;
142  for (uint32_t i = 0; i < nEntries; ++i) {
143  m_inputTree->GetEntry(i);
144  std::map<uint32_t, uint32_t>::const_iterator it = theMap.find(rawid_i);
145 
146  if (it == theMap.end()) {
147  edm::LogInfo("ERROR") << "Error: couldn't find rawId: " << rawid_i;
148  iter++;
149  } else {
150  rawid_f = (it)->second;
151  x_f = x_i;
152  y_f = y_i;
153  z_f = z_i;
154  a_f = a_i;
155  b_f = b_i;
156  c_f = c_i;
157  m_outputTree->Fill();
158  }
159  }
160  edm::LogInfo("errors") << "Couldn't find: " << iter;
161  m_outputTFile->cd();
162  m_outputTree->Write();
163  m_outputTFile->Close();
164 }
165 
167  std::ifstream myfile(m_textFile.c_str());
168  if (!myfile.is_open())
169  throw cms::Exception("FileAccess") << "Unable to open input text file";
170 
171  uint32_t oldid;
172  uint32_t newid;
173 
174  uint32_t ctr = 0;
175  while (!myfile.eof() && myfile.good()) {
176  myfile >> oldid >> newid;
177 
178  //depends on conversion type: OldtoNew or NewtoOld
179  std::pair<uint32_t, uint32_t> pairType;
180  if (m_conversionType == "OldtoNew") {
181  pairType.first = oldid;
182  pairType.second = newid;
183  }
184  if (m_conversionType == "NewtoOld") {
185  pairType.first = newid;
186  pairType.second = oldid;
187  }
188 
189  theMap.insert(pairType);
190 
191  if (myfile.fail())
192  break;
193 
194  ctr++;
195  }
196  edm::LogInfo("Check") << ctr << " alignables read.";
197 }
198 
200  m_inputTree->SetBranchAddress("rawid", &rawid_i);
201  m_inputTree->SetBranchAddress("x", &x_i);
202  m_inputTree->SetBranchAddress("y", &y_i);
203  m_inputTree->SetBranchAddress("z", &z_i);
204  m_inputTree->SetBranchAddress("alpha", &a_i);
205  m_inputTree->SetBranchAddress("beta", &b_i);
206  m_inputTree->SetBranchAddress("gamma", &c_i);
207 
208  m_outputTree->Branch("rawid", &rawid_f, "rawid/I");
209  m_outputTree->Branch("x", &x_f, "x/D");
210  m_outputTree->Branch("y", &y_f, "y/D");
211  m_outputTree->Branch("z", &z_f, "z/D");
212  m_outputTree->Branch("alpha", &a_f, "alpha/D");
213  m_outputTree->Branch("beta", &b_f, "beta/D");
214  m_outputTree->Branch("gamma", &c_f, "gamma/D");
215 }
216 
217 // ------------ method called once each job just after ending the event loop ------------
219 
220 //define this as a plug-in
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
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 &)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
std::map< uint32_t, uint32_t > theMap
~Tracker_OldtoNewConverter() override=default
Log< level::Info, false > LogInfo
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)