CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CSCChipSpeedCorrectionDBConditions.h
Go to the documentation of this file.
1 #ifndef _CSCCHIPSPEEDCORRECTIONDBCONDITIONS_H
2 #define _CSCCHIPSPEEDCORRECTIONDBCONDITIONS_H
3 
4 #include <memory>
5 #include <cmath>
15 
20 
22  public:
25 
26  inline static CSCDBChipSpeedCorrection * prefillDBChipSpeedCorrection(bool isForMC, std::string dataCorrFileName, float dataOffse);
27 
29 
31 
32  private:
33  // ----------member data ---------------------------
36 
37  //Flag for determining if this is for setting MC or data corrections
38  bool isForMC;
39  //File for reading <= 15768 data chip corrections. MC will be fake (only one value for every chip);
40  std::string dataCorrFileName;
41  float dataOffset;
42 
43 };
44 
48 
49 #include<fstream>
50 #include<vector>
51 #include<iostream>
52 
53 
54 
55 // to workaround plugin library
57 {
58  if (isMC)
59  printf("\n Generating fake DB constants for MC\n");
60  else {
61  printf("\n Reading chip corrections from file %s \n",filename.data());
62  printf("my data offset value is %f \n",dataOffset);
63  }
64 
65  CSCIndexer indexer;
66 
67  const int CHIP_FACTOR=100;
68  const int MAX_SIZE = 15768;
69  const int MAX_SHORT= 32767;
71 
73  itemvector.resize(MAX_SIZE);
74  cndbChipCorr->factor_speedCorr= int (CHIP_FACTOR);
75 
76  //Fill chip corrections for MC is very simple
77  if (isMC){
78  for (int i=0;i<MAX_SIZE;i++){
79  itemvector[i].speedCorr = 0;
80  }
81  return cndbChipCorr;
82  }
83 
84  //Filling for data takes a little more time
85  FILE *fin = fopen(filename.data(),"r");
86 
87  int serialChamber,endcap,station,ring,chamber,chip;
88  float t,dt;
89  int nPulses;
90 
91  std::vector<int> new_index_id;
92  std::vector<float> new_chipPulse;
93  double runningTotal = 0;
94  int numNonZero = 0;
95  while (!feof(fin)){
96  //note space at end of format string to convert last \n
97  // int check = fscanf(fin,"%d %d %f %f \n",&serialChamber,&chip,&t,&dt);
98  int check = fscanf(fin,"%d %d %d %d %d %d %f %f %d \n",&serialChamber,&endcap,&station,&ring,&chamber,&chip,&t,&dt,&nPulses);
99  if (check != 9){
100  printf("The input file format is not as expected\n");
101  assert(0);
102  }
103 
104  int serialChamber_safecopy = serialChamber;
105  // Now to map from S. Durkin's serialChamber index convention
106  // ME+1/1-ME+41 = 1 -234
107  // ME+4/2 = 235-270*
108  // ME-1/1-ME-41 = 271-504*
109  // ME-4/2 = 505-540
110  // To the convention used in /DataFormats/MuonDetId/interface/CSCIndexer.h
111  // ME+1/1-ME+41 = 1 -234
112  // ME+4/2 = 469-504*
113  // ME-1/1-ME-41 = 235-468*
114  // ME-4/2 = 505-540
115  // Only the chambers marked * need to be remapped
116 
117  if (serialChamber >=235 && serialChamber <=270)
118  serialChamber += 234;
119  else { // not in ME+4\2
120  if (serialChamber >=271 && serialChamber <=504)
121  serialChamber -= 36;
122  }
123 
124  CSCDetId chamberId = indexer.detIdFromChamberIndex(serialChamber);
125  // Now to map from S. Durkin's chip index convention 0-29 (with 4,9,14,19,24,29 unused in ME1/3)
126  // To the convention used in /DataFormats/MuonDetId/interface/CSCIndexer.h
127  // Layer 1-6 and chip 1-5 for all chambers except ME1/3 which is chip 1-4
128  int layer = (chip)/5+1;
129  CSCDetId cscId(chamberId.endcap(),chamberId.station(),chamberId.ring(),chamberId.chamber(),layer);
130  // This should yield the same CSCDetId as decoding the chamber serial does
131  // If not, some debugging is needed
132  CSCDetId cscId_doubleCheck(endcap,station,ring,chamber,layer);
133  if (cscId != cscId_doubleCheck){
134  printf("Why doesn't chamberSerial %d map to e: %d s: %d r: %d c: %d ? \n",serialChamber_safecopy, endcap, station, ring, chamber);
135  assert(0);
136  }
137 
138  chip =(chip%5)+1;
139  // The file produced by Stan starts from the geometrical strip number stored in the CSCStripDigi
140  // When the strip digis are built, the conversion from electronics channel to geometrical strip (reversing ME+1/1a and, more importantly, ME-1/1b)
141  // is done before the digi is filled (EventFilter/CSCRawToDigi/src/CSCCFEBData.cc).
142  // Since we're filling an electronics channel database, I'll flip again ME-1/1b
143  if (endcap == 2 && station ==1 && ring==1 && chip <5){
144  chip = 5 - chip; //change 1-4 to 4-1
145  }
146 
147 
148  new_index_id.push_back(indexer.chipIndex(cscId,chip));
149  new_chipPulse.push_back(t);
150  if (t!=0){
151  runningTotal += t;
152  numNonZero++;
153  }
154  }
155  fclose(fin);
156 
157 
158  // Fill the chip corrections with zeros to start
159  for (int i=0;i<MAX_SIZE;i++){
160  itemvector[i].speedCorr = 0;
161  }
162 
163  for (unsigned int i=0;i<new_index_id.size();i++){
164  if((short int) (fabs((dataOffset-new_chipPulse[i])*CHIP_FACTOR+0.5))<MAX_SHORT)
165  itemvector[new_index_id[i]-1].speedCorr = (short int) ((dataOffset-new_chipPulse[i])*CHIP_FACTOR+0.5*(dataOffset>=new_chipPulse[i])-0.5*(dataOffset<new_chipPulse[i]));
166  //printf("i= %d \t new index id = %d \t corr = %f \n",i,new_index_id[i], new_chipPulse[i]);
167  }
168 
169  //For now, calculate the mean chip correction and use it for all chambers that don't have calibration pulse data (speedCorr ==0)
170  //or had values of zero (speedCorr == dataOffset)
171  //This should be a temporary fix until all chips that will read out in data have calibration information
172  //Since there is only a handful out of 15K chips with values more than 3 ns away from the average, this is probably very safe
173  //to first order
174  float ave = runningTotal/numNonZero;
175  for (int i=0;i<MAX_SIZE;i++){
176  if( itemvector[i].speedCorr == 0 ||itemvector[i].speedCorr == (short int)(dataOffset*CHIP_FACTOR+0.5) )
177  itemvector[i].speedCorr = (short int) ((dataOffset-ave)*CHIP_FACTOR+0.5*(dataOffset>=ave)-0.5*(dataOffset<ave));
178  }
179 
180 
181  return cndbChipCorr;
182 }
183 
184 #endif
int chamber() const
Definition: CSCDetId.h:70
float dt
Definition: AMPTWrapper.h:126
int i
Definition: DBlmapReader.cc:9
ReturnType produceDBChipSpeedCorrection(const CSCDBChipSpeedCorrectionRcd &)
int endcap() const
Definition: CSCDetId.h:95
CSCDetId detIdFromChamberIndex(IndexType ici) const
Definition: CSCIndexer.cc:62
bool check(const DataFrame &df, bool capcheck, bool dvercheck)
int ring() const
Definition: CSCDetId.h:77
IndexType chipIndex(IndexType ie, IndexType is, IndexType ir, IndexType ic, IndexType il, IndexType ichip) const
Definition: CSCIndexer.h:237
static CSCDBChipSpeedCorrection * prefillDBChipSpeedCorrection(bool isForMC, std::string dataCorrFileName, float dataOffse)
void setIntervalFor(const edm::eventsetup::EventSetupRecordKey &, const edm::IOVSyncValue &, edm::ValidityInterval &)
tuple filename
Definition: lut2db_cfg.py:20
std::vector< Item > ChipSpeedContainer
int station() const
Definition: CSCDetId.h:88