CMS 3D CMS Logo

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