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