ATLAS Offline Software
CurrMap.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "CurrMap.h"
6 #include <stdlib.h>
7 #include <iostream>
8 #include <stdio.h>
9 
10 CurrMap::CurrMap(const std::string& filename,double xnorm)
11 {
12  // normalisation to convert nA from the map back to MeV
13  // use average in straight section:
14  // this assumes that the electronic calibration is such
15  // that Sum ai*(ADC-PED) gives the initial current for a triangular
16  // signal of drift time ~475 ns
17  m_norm = xnorm;
18  m_nx=0;
19  m_ny=0;
20  m_xmin=0.;
21  m_xmax=0.;
22  m_dx=0.;
23  m_ymin=0.;
24  m_ymax=0.;
25  m_dy=0.;
26 
27  m_gap=nullptr;
28  m_curr0=nullptr;
29  m_curr1=nullptr;
30  m_curr2=nullptr;
31 
32 
33  FILE * fp = fopen(filename.c_str(),"r");
34  if(fp){
35  char line[81];
36  fgets(line,80,fp);
37  sscanf(&line[0],"%d%f%f%d%f%f",&m_nx,&m_xmin,&m_xmax,&m_ny,&m_ymin,&m_ymax);
38 
39  if(m_nx>0 && m_ny>0 && m_nx<10000 && m_ny<10000) {//coverity issue. This is a tainted variable protection, 10000 can be changed if required.
40  m_dx = (m_xmax-m_xmin)/((float) m_nx);
41  m_dy = (m_ymax-m_ymin)/((float) m_ny);
42 
43  m_gap = new float[m_nx*m_ny];
44  m_curr0 = new float[m_nx*m_ny];
45  m_curr1 = new float[m_nx*m_ny];
46  m_curr2 = new float[m_nx*m_ny];
47 
48  for (int i=0; i<m_nx;i++)
49  {
50  for (int j=0; j<m_ny;j++) {
51  m_gap[j*m_nx+i]=0.;
52  m_curr0[j*m_nx+i]=0;
53  m_curr1[j*m_nx+i]=0;
54  m_curr2[j*m_nx+i]=0;
55  }
56  }
57 
58  int ix,iy;
59  float gap,cur1,cur2,cur3;
60  while (fgets(line,80,fp)) {
61  sscanf(&line[0],"%d%d%f%f%f%f",&ix,&iy,&gap,&cur1,&cur2,&cur3);
62  if(ix >= 0 && ix < m_nx && iy >= 0 && iy < m_ny){
63  m_gap[iy*m_nx+ix]=gap;
64  m_curr0[iy*m_nx+ix]=cur1/m_norm;
65  m_curr1[iy*m_nx+ix]=cur2/m_norm;
66  m_curr2[iy*m_nx+ix]=cur3/m_norm;
67  }
68  }
69  }
70  else{
71  std::cout << "Error in CurrMap::CurrMap: nx or ny out of limits." << std::endl;
72  }
73  fclose(fp);
74  }
75  else{
76 
77  std::cout << "Error in CurrMap::CurrMap: The file could not be open." << std::endl;
78  }
79 
80 
81 }
82 
83 
85 {
86  if (m_gap) delete [] m_gap;
87  if (m_curr0) delete [] m_curr0;
88  if (m_curr1) delete [] m_curr1;
89  if (m_curr2) delete [] m_curr2;
90 }
91 
92 
93 void CurrMap::GetAll(double x, double y, double* gap, double* curr0, double* curr1, double* curr2) const
94 {
95  *gap=0;
96  *curr0=0;
97  *curr1=0;
98  *curr2=0;
99 
100  if (m_nx==0 || m_ny ==0) return;
101  if (x<m_xmin ) x=m_xmin;
102  if (x>=m_xmax ) x=m_xmax-0.0001;
103  if (y<m_ymin ) y=m_ymin;
104  if (y>=m_ymax ) y=m_ymax-0.0001;
105 
106  int ix,iy;
107  ix = (int) ((x-m_xmin)/m_dx);
108  iy = (int) ((y-m_ymin)/m_dy);
109 
110  if ( (ix+1) < m_nx && (iy+1) < m_ny) {
111  float x0 = ((float) ix)*m_dx+m_xmin;
112  float x1 = x0+m_dx;
113  float y0 = ((float) iy)*m_dy+m_ymin;
114  float y1 = y0+m_dy;
115  float w[4];
116  w[0]=(x1-x)*(y1-y);
117  w[1]=(x-x0)*(y1-y);
118  w[2]=(x1-x)*(y-y0);
119  w[3]=(x-x0)*(y-y0);
120 
121  float sumw=0.;
122  for (int i=0;i<2;i++) {
123  for (int j=0;j<2;j++) {
124  int n=ix+i+(iy+j)*m_nx;
125  if (m_curr0[n]>0) {
126  int m=i+2*j;
127  sumw +=w[m];
128  *gap += m_gap[n]*w[m];
129  *curr0 += m_curr0[n]*w[m];
130  *curr1 += m_curr1[n]*w[m];
131  *curr2 += m_curr2[n]*w[m];
132  }
133  }
134  }
135  if (sumw>0.) {
136  *gap = *gap/sumw;
137  *curr0 = *curr0/sumw;
138  *curr1 = *curr1/sumw;
139  *curr2 = *curr2/sumw;
140  }
141  else {
142  // try to recover a non zero current by moving in the map
143  int jy=-1;
144  int jx=-1;
145  int idistm=100;
146  for (int iiy=iy-7;iiy<iy+8;iiy++) {
147  for (int iix=ix-7;iix<ix+8;iix++) {
148  if (iiy>=0 && iiy<m_ny && iix>=0 && iix<m_nx) {
149  if (m_curr0[iix+iiy*m_nx]>0) {
150  int idist=(iix-ix)*(iix-ix)+(iiy-iy)*(iiy-iy);
151  if(idist<idistm) {
152  idistm=idist;
153  jx=iix;
154  jy=iiy;
155  }
156  }
157  }
158  }
159  }
160  if (idistm<100 && jx>=0 && jy>=0) {
161  *gap = m_gap[jx+jy*m_nx];
162  *curr0 = m_curr0[jx+jy*m_nx];
163  *curr1 = m_curr1[jx+jy*m_nx];
164  *curr2 = m_curr2[jx+jy*m_nx];
165  } else {
166  *gap=0;
167  *curr0=0;
168  *curr1=0;
169  *curr2=0;
170  }
171  }
172  }
173  else {
174  // on the edges of the map, no linear interpolation
175  int n=ix+iy*m_nx;
176  if (m_curr0[n]<1e-6 && (ix+1) <m_nx) n=ix+1 +iy*m_nx;
177  if (m_curr0[n]<1e-6 && (ix-1) >0 ) n=ix-1 +iy*m_nx;
178  if (m_curr0[n]<1e-6 && (iy+1) <m_ny) n=ix+(iy+1)*m_nx;
179  if (m_curr0[n]<1e-6 && (iy-1) <0) n=ix+(iy-1)*m_nx;
180  if (m_curr0[n]<1e-6 && (ix+1) < m_nx && (iy+1) < m_ny) n=ix+1+(iy+1)*m_nx;
181  if (m_curr0[n]<1e-6 && (ix-1) >0 && (iy+1) < m_ny) n=ix-1+(iy+1)*m_nx;
182  if (m_curr0[n]<1e-6 && (ix+1) < m_nx && (iy-1) >0 ) n=ix+1+(iy-1)*m_nx;
183  if (m_curr0[n]<1e-6 && (ix-1) >0 && (iy-1) >0 ) n=ix-1+(iy-1)*m_nx;
184  *gap = m_gap[n];
185  *curr0 = m_curr0[n];
186  *curr1 = m_curr1[n];
187  *curr2 = m_curr2[n];
188  }
189 }
CurrMap::~CurrMap
~CurrMap()
Definition: CurrMap.cxx:84
CurrMap::m_curr0
float * m_curr0
Definition: CurrMap.h:32
CurrMap::m_xmin
float m_xmin
Definition: CurrMap.h:31
plotBeamSpotCompare.x1
x1
Definition: plotBeamSpotCompare.py:216
CurrMap::GetAll
void GetAll(double x, double y, double *gap, double *curr0, double *curr1, double *curr2) const
Definition: CurrMap.cxx:93
checkFileSG.line
line
Definition: checkFileSG.py:75
python.SystemOfUnits.m
int m
Definition: SystemOfUnits.py:91
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
CurrMap.h
CurrMap::m_dx
float m_dx
Definition: CurrMap.h:31
CurrMap::m_curr1
float * m_curr1
Definition: CurrMap.h:32
plotBeamSpotVxVal.sumw
int sumw
Definition: plotBeamSpotVxVal.py:236
x
#define x
CaloSwCorrections.gap
def gap(flags, cells_name, *args, **kw)
Definition: CaloSwCorrections.py:212
makeTRTBarrelCans.y1
tuple y1
Definition: makeTRTBarrelCans.py:15
CurrMap::m_nx
int m_nx
Definition: CurrMap.h:30
CurrMap::m_ymin
float m_ymin
Definition: CurrMap.h:31
CurrMap::m_norm
float m_norm
Definition: CurrMap.h:33
lumiFormat.i
int i
Definition: lumiFormat.py:92
trigmenu_modify_prescale_json.fp
fp
Definition: trigmenu_modify_prescale_json.py:53
beamspotman.n
n
Definition: beamspotman.py:731
CurrMap::m_ny
int m_ny
Definition: CurrMap.h:30
CurrMap::m_curr2
float * m_curr2
Definition: CurrMap.h:32
CurrMap::CurrMap
CurrMap(const std::string &filename, double xnorm)
Definition: CurrMap.cxx:10
DiTauMassTools::MaxHistStrategyV2::e
e
Definition: PhysicsAnalysis/TauID/DiTauMassTools/DiTauMassTools/HelperFunctions.h:26
y
#define y
CurrMap::m_gap
float * m_gap
Definition: CurrMap.h:32
CaloCellTimeCorrFiller.filename
filename
Definition: CaloCellTimeCorrFiller.py:24
CurrMap::m_ymax
float m_ymax
Definition: CurrMap.h:31
CurrMap::m_dy
float m_dy
Definition: CurrMap.h:31
python.IoTestsLib.w
def w
Definition: IoTestsLib.py:200
readCCLHist.float
float
Definition: readCCLHist.py:83
CurrMap::m_xmax
float m_xmax
Definition: CurrMap.h:31