CMS 3D CMS Logo

MaskedRctInputDigiProducer.cc
Go to the documentation of this file.
2 
3 #include <fstream>
4 #include <iostream>
5 #include <string>
6 #include <vector>
7 using std::endl;
8 using std::ifstream;
9 using std::vector;
10 
11 //
12 // constructors and destructor
13 //
14 
16  const edm::ParameterSet &iConfig)
17  : useEcal_(iConfig.getParameter<bool>("useEcal")),
18  useHcal_(iConfig.getParameter<bool>("useHcal")),
19  ecalDigisLabel_(iConfig.getParameter<edm::InputTag>("ecalDigisLabel")),
20  hcalDigisLabel_(iConfig.getParameter<edm::InputTag>("hcalDigisLabel")),
21  maskFile_(iConfig.getParameter<edm::FileInPath>("maskFile")) {
22  // register your products
23  /* Examples
24  produces<ExampleData2>();
25 
26  //if do put with a label
27  produces<ExampleData2>("label");
28  */
29 
30  produces<EcalTrigPrimDigiCollection>();
31  produces<HcalTrigPrimDigiCollection>();
32 
33  // now do what ever other initialization is needed
34 }
35 
37 
38  // do anything here that needs to be done at desctruction time
39  // (e.g. close files, deallocate resources etc.)
40 }
41 
42 //
43 // member functions
44 //
45 
46 // ------------ method called to produce the data ------------
48  const edm::EventSetup &iSetup) {
49  using namespace edm;
50  /* This is an event example
51  //Read 'ExampleData' from the Event
52  Handle<ExampleData> pIn;
53  iEvent.getByLabel("example",pIn);
54 
55  //Use the ExampleData to create an ExampleData2 which
56  // is put into the Event
57  std::unique_ptr<ExampleData2> pOut(new ExampleData2(*pIn));
58  iEvent.put(std::move(pOut));
59  */
60 
63 
64  if (useEcal_) {
65  iEvent.getByLabel(ecalDigisLabel_, ecal);
66  }
67  if (useHcal_) {
68  iEvent.getByLabel(hcalDigisLabel_, hcal);
69  }
70 
73  if (ecal.isValid()) {
74  ecalColl = *ecal;
75  }
76  if (hcal.isValid()) {
77  hcalColl = *hcal;
78  }
79 
80  /* this is an EventSetup example
81  //Read SetupData from the SetupRecord in the EventSetup
82  ESHandle<SetupData> pSetup;
83  iSetup.get<SetupRecord>().get(pSetup);
84  */
85 
86  ifstream maskFileStream(maskFile_.fullPath().c_str());
87  if (!maskFileStream.is_open()) {
88  throw cms::Exception("FileInPathError")
89  << "RCT mask file not opened" << std::endl;
90  ;
91  }
92 
93  // read and process file (transform mask to new position in phi to match
94  // digis)
95  // char junk[256];
96  std::string junk;
97  // char junk[32];
98  do {
99  // maskFileStream.getline(junk, 256);
100  maskFileStream >> junk;
101  } while (junk != "ECAL:");
102 
103  // std::vector<std::vector<std::vector<unsigned short> > >
104  // temp(2,std::vector<std::vector<unsigned short> >(72,std::vector<unsigned
105  // short>(28,1)));
106  std::vector<std::vector<std::vector<unsigned short>>> ecalMask(
107  2, std::vector<std::vector<unsigned short>>(
108  72, std::vector<unsigned short>(28, 1)));
109  std::vector<std::vector<std::vector<unsigned short>>> hcalMask(
110  2, std::vector<std::vector<unsigned short>>(
111  72, std::vector<unsigned short>(28, 1)));
112  std::vector<std::vector<std::vector<unsigned short>>> hfMask(
113  2, std::vector<std::vector<unsigned short>>(
114  18, std::vector<unsigned short>(8, 1)));
115 
116  // read ECAL mask first
117  // loop through rct iphi
118  for (int i = 1; i <= 72; i++) {
119  int phi_index = (72 + 18 - i) % 72; // transfrom from RCT coords
120  if (phi_index == 0) {
121  phi_index = 72;
122  }
123  // std::cout << "ecal phi index is " << phi_index << std::endl;
124  for (int j = 28; j >= 1; j--) {
125  maskFileStream >> junk;
126  if (junk == "-") {
127  } else if ((junk == "X") || (junk == "x")) {
128  ecalMask.at(0).at(phi_index - 1).at(j - 1) = 0;
129  } else {
130  std::cerr << "RCT mask producer: error -- unrecognized character"
131  << std::endl;
132  }
133  }
134  for (int j = 1; j <= 28; j++) {
135  maskFileStream >> junk;
136  if (junk == "-") {
137  } else if ((junk == "X") || (junk == "x")) {
138  ecalMask.at(1).at(phi_index - 1).at(j - 1) = 0;
139  } else {
140  std::cerr << "RCT mask producer: error -- unrecognized character"
141  << std::endl;
142  }
143  }
144  }
145  // std::cout << "done reading ECAL" << std::endl;
146 
147  maskFileStream >> junk;
148  if (junk != "HCAL:") {
149  throw cms::Exception("FileInPathError")
150  << "RCT mask producer: error reading ECAL mask" << std::endl;
151  ;
152  }
153 
154  // std::cout << "Starting HCAL read" << std::endl;
155 
156  // now read HCAL mask
157  // loop through rct iphi
158  for (int i = 1; i <= 72; i++) {
159  int phi_index = (72 + 18 - i) % 72; // transfrom from RCT coords
160  if (phi_index == 0) {
161  phi_index = 72;
162  }
163  // std::cout << "hcal phi index is " << phi_index << std::endl;
164  for (int j = 28; j >= 1; j--) {
165  maskFileStream >> junk;
166  if (junk == "-") {
167  } else if ((junk == "X") || (junk == "x")) {
168  hcalMask.at(0).at(phi_index - 1).at(j - 1) = 0;
169  } else {
170  std::cerr << "RCT mask producer: error -- unrecognized character"
171  << std::endl;
172  }
173  }
174  for (int j = 1; j <= 28; j++) {
175  maskFileStream >> junk;
176  if (junk == "-") {
177  } else if ((junk == "X") || (junk == "x")) {
178  hcalMask.at(1).at(phi_index - 1).at(j - 1) = 0;
179  } else {
180  std::cerr << "RCT mask producer: error -- unrecognized character"
181  << std::endl;
182  }
183  }
184  }
185 
186  maskFileStream >> junk;
187  if (junk != "HF:") {
188  throw cms::Exception("FileInPathError")
189  << "RCT mask producer: error reading HCAL mask" << std::endl;
190  ;
191  }
192 
193  // loop through the hf phi columns in file
194  for (int i = 0; i < 18; i++) {
195  // std::cout << "iphi for hf file read is " << i << std::endl;
196  for (int j = 4; j >= 1; j--) {
197  if (maskFileStream >> junk) {
198  } else {
199  std::cerr << "RCT mask producer: error reading HF mask" << std::endl;
200  }
201  if (junk == "-") {
202  } else if ((junk == "X") || (junk == "x")) {
203  hfMask.at(0).at(i).at(j - 1) =
204  0; // just save iphi as 0-17, transform later
205  } else {
206  std::cerr << "RCT mask producer: error -- unrecognized character"
207  << std::endl;
208  }
209  }
210  for (int j = 1; j <= 4; j++) {
211  if (maskFileStream >> junk) {
212  } else {
213  std::cerr << "RCT mask producer: error reading HF mask" << std::endl;
214  }
215  if (junk == "-") {
216  } else if ((junk == "X") || (junk == "x")) {
217  hfMask.at(1).at(i).at(j - 1) = 0;
218  } else {
219  std::cerr << "RCT mask producer: error -- unrecognized character"
220  << std::endl;
221  }
222  }
223  }
224 
225  // std::cout << "HF read done" << std::endl;
226 
227  maskFileStream.close();
228 
229  // std::cout << "file closed" << std::endl;
230 
231  // apply mask
232 
233  std::unique_ptr<EcalTrigPrimDigiCollection> maskedEcalTPs(
235  std::unique_ptr<HcalTrigPrimDigiCollection> maskedHcalTPs(
237  maskedEcalTPs->reserve(56 * 72);
238  maskedHcalTPs->reserve(56 * 72 + 18 * 8);
239  int nEcalSamples = 0;
240  int nHcalSamples = 0;
241 
242  for (unsigned int i = 0; i < ecalColl.size(); i++) {
243  nEcalSamples = ecalColl[i].size();
244  short ieta = (short)ecalColl[i].id().ieta();
245  unsigned short absIeta = (unsigned short)abs(ieta);
246  int sign = ieta / absIeta;
247  short iphi = (unsigned short)ecalColl[i].id().iphi();
248  // if (i < 20) {std::cout << "ieta is " << ieta << ", absIeta is " <<
249  // absIeta
250  // << ", iphi is " << iphi << std::endl;}
251 
252  EcalTriggerPrimitiveDigi ecalDigi(
253  EcalTrigTowerDetId(sign, EcalTriggerTower, absIeta, iphi));
254  ecalDigi.setSize(nEcalSamples);
255 
256  for (int nSample = 0; nSample < nEcalSamples; nSample++) {
257 
258  int energy = 0;
259  bool fineGrain = false;
260 
261  if (sign < 0) {
262  // std::cout << "eta-: mask is " <<
263  // ecalMask.at(0).at(iphi-1).at(absIeta-1) << std::endl;
264  energy = ecalMask.at(0).at(iphi - 1).at(absIeta - 1) *
265  ecalColl[i].sample(nSample).compressedEt();
266  fineGrain = (ecalMask.at(0).at(iphi - 1).at(absIeta - 1) != 0) &&
267  ecalColl[i].sample(nSample).fineGrain();
268  } else if (sign > 0) {
269  // std::cout << "eta+: mask is " <<
270  // ecalMask.at(1).at(iphi-1).at(absIeta-1) << std::endl;
271  energy = ecalMask.at(1).at(iphi - 1).at(absIeta - 1) *
272  ecalColl[i].sample(nSample).compressedEt();
273  fineGrain = (ecalMask.at(1).at(iphi - 1).at(absIeta - 1) != 0) &&
274  ecalColl[i].sample(nSample).fineGrain();
275  }
276 
277  ecalDigi.setSample(nSample,
278  EcalTriggerPrimitiveSample(energy, fineGrain, 0));
279  }
280  maskedEcalTPs->push_back(ecalDigi);
281  }
282  // std::cout << "End of ecal digi masking" << std::endl;
283 
284  // std::cout << "nHcalDigis is " << hcalColl.size() << std::endl;
285  for (unsigned int i = 0; i < hcalColl.size(); i++) {
286  nHcalSamples = hcalColl[i].size();
287  // if ((i % 100) == 0 ) {std::cout << "Loop " << i << std::endl;}
288  short ieta = (short)hcalColl[i].id().ieta();
289  unsigned short absIeta = (unsigned short)abs(ieta);
290  int sign = ieta / absIeta;
291  short iphi = (unsigned short)hcalColl[i].id().iphi();
292  // if (i < 20) {std::cout << "ieta is " << ieta << ", absIeta is " <<
293  // absIeta
294  // << ", iphi is " << iphi << std::endl;}
295  /*
296  if (hcalColl[i].SOI_compressedEt() != 0)
297  {
298  std::cout << "original et " << hcalColl[i].SOI_compressedEt()
299  << " fg " << hcalColl[i].SOI_fineGrain() << " iphi "
300  << iphi << " ieta " << ieta << std::endl;
301  }
302  */
303  HcalTriggerPrimitiveDigi hcalDigi(HcalTrigTowerDetId(ieta, iphi));
304  hcalDigi.setSize(nHcalSamples);
305  hcalDigi.setPresamples(hcalColl[i].presamples());
306 
307  for (int nSample = 0; nSample < nHcalSamples; nSample++) {
308 
309  int energy = 0;
310  bool fineGrain = false;
311 
312  if (absIeta < 29) {
313  if (sign < 0) {
314  energy = hcalMask.at(0).at(iphi - 1).at(absIeta - 1) *
315  hcalColl[i].sample(nSample).compressedEt();
316  fineGrain = (hcalMask.at(0).at(iphi - 1).at(absIeta - 1) != 0) &&
317  hcalColl[i].sample(nSample).fineGrain();
318  } else if (sign > 0) {
319  energy = hcalMask.at(1).at(iphi - 1).at(absIeta - 1) *
320  hcalColl[i].sample(nSample).compressedEt();
321  fineGrain = (hcalMask.at(1).at(iphi - 1).at(absIeta - 1) != 0) &&
322  hcalColl[i].sample(nSample).fineGrain();
323  }
324  } else if ((absIeta >= 29) && (absIeta <= 32)) {
325  // std::cout << "hf iphi: " << iphi << std::endl;
326  short hf_phi_index = iphi / 4;
327  // iphi = iphi/4; // change from 1,5,9, etc to access vector positions
328  // std::cout << "hf phi index: " << hf_phi_index << std::endl;
329  if (sign < 0) {
330  // std::cout << "ieta is " << ieta << ", absIeta is " << absIeta << ",
331  // iphi is " << iphi << std::endl; std::cout << "eta-: mask is " <<
332  // hfMask.at(0).at(hf_phi_index).at(absIeta-29) << std::endl; // hf
333  // ieta 0-3
334  energy = hfMask.at(0).at(hf_phi_index).at(absIeta - 29) *
335  hcalColl[i]
336  .sample(nSample)
337  .compressedEt(); // for hf, iphi starts at 0
338  fineGrain = (hfMask.at(0).at(hf_phi_index).at(absIeta - 29) != 0) &&
339  hcalColl[i].sample(nSample).fineGrain();
340  } else if (sign > 0) {
341  // std::cout << "ieta is " << ieta << ", absIeta is " << absIeta << ",
342  // iphi is " << iphi << std::endl; std::cout << "eta+: mask is " <<
343  // hfMask.at(1).at(hf_phi_index).at(absIeta-29) << std::endl;
344  energy = hfMask.at(1).at(hf_phi_index).at(absIeta - 29) *
345  hcalColl[i].sample(nSample).compressedEt();
346  fineGrain = (hfMask.at(1).at(hf_phi_index).at(absIeta - 29) != 0) &&
347  hcalColl[i].sample(nSample).fineGrain();
348  }
349  // iphi = iphi*4 + 1; // change back to original
350  // std::cout << "New hf iphi = " << iphi << std::endl;
351  }
352 
353  hcalDigi.setSample(nSample,
354  HcalTriggerPrimitiveSample(energy, fineGrain, 0, 0));
355 
356  // if (hcalDigi.SOI_compressedEt() != 0)
357  //{
358  // std::cout << "et " << hcalDigi.SOI_compressedEt()
359  // << "fg " << hcalDigi.SOI_fineGrain() << std::endl;
360  //}
361  }
362  maskedHcalTPs->push_back(hcalDigi);
363  }
364  // std::cout << "End of hcal digi masking" << std::endl;
365 
366  // put new data into event
367 
368  iEvent.put(std::move(maskedEcalTPs));
369  iEvent.put(std::move(maskedHcalTPs));
370 }
371 
372 // ------------ method called once each job just before starting event loop
373 // ------------
374 
375 // ------------ method called once each job just after ending the event loop
376 // ------------
378 
379 // define this as a plug-in
void setSample(int i, const HcalTriggerPrimitiveSample &sam)
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
edm::SortedCollection< HcalTriggerPrimitiveDigi > HcalTrigPrimDigiCollection
void produce(edm::Event &, const edm::EventSetup &) override
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void setSample(int i, const EcalTriggerPrimitiveSample &sam)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
bool isValid() const
Definition: HandleBase.h:74
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:480
edm::SortedCollection< EcalTriggerPrimitiveDigi > EcalTrigPrimDigiCollection
HLT enums.
size_type size() const
MaskedRctInputDigiProducer(const edm::ParameterSet &)
std::string fullPath() const
Definition: FileInPath.cc:163
def move(src, dest)
Definition: eostools.py:511