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