ATLAS Offline Software
CaloLocalHadCoeffHelper.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 //-----------------------------------------------------------------------
6 // File and Version Information:
7 // $Id: CaloHadDMCoeffHelper.cxx,v 1.2 2009-03-06 14:43:23 pospelov Exp $
8 //
9 // Description: see CaloHadDMCoeffHelper.h
10 //
11 // Environment:
12 // Software developed for the ATLAS Detector at CERN LHC
13 //
14 // Author List:
15 // Gennady Pospelov
16 //
17 //-----------------------------------------------------------------------
19 #include <iostream>
20 #include <sstream>
21 #include <cstring>
22 #include <iomanip>
23 #include "boost/io/ios_state.hpp"
24 
25 
26 #define MAX_BUFFER_LEN 1024
27 
28 
30 {
31 
32 }
33 
34 
36 {
37 
38 }
39 
40 
41 // get HadDMArea from area name
42 const CaloLocalHadCoeff::LocalHadArea * CaloLocalHadCoeffHelper::getAreaFromName(const CaloLocalHadCoeff * coeff, const std::string& sname, int &indx) const
43 {
44  for(int i_area=0; i_area<coeff->getSizeAreaSet(); i_area++) {
45  if(sname == coeff->getArea(i_area)->getTitle()) {
46  indx = i_area;
47  return coeff->getArea(i_area);
48  }
49  }
50  std::cout << "CaloLocalHadCoeffHelper::getAreaFromName() -> Error! No such area '" << sname << "'" << std::endl;
51  return nullptr;
52 }
53 
54 
55 
56 /* ****************************************************************************
57 To read set of local hadronic coefficients from text file
58 **************************************************************************** */
60 {
62 
63  char cLine[MAX_BUFFER_LEN];
64 
65  // Find the full path to filename
66  std::cout << "CaloLocalHadCoeffHelper::InitDataFromFile - Reading file '" << filename << "'." << std::endl;
67 
68  std::ifstream fin(filename);
69  if ( !fin ) {
70  std::cout << "CaloLocalHadCoeffHelper::InitDataFromFile - Can't open file '" << filename << "'." << std::endl;
71  delete data; return nullptr;
72  }
73 
74  std::string sLine;
75  std::istringstream ist;
76  while(fin.getline(cLine,sizeof(cLine)-1)) {
77  if( strlen(cLine)==0 || cLine[0] == '#' || cLine[0] == '\n') continue;
78 
79  // parsing area line
80  sLine = cLine;
81  ist.clear(); ist.str(sLine);
82  std::string sdummy, area_title;
83  int area_indx(0), area_type(0), area_npars(0);
84  if( !(ist >> sdummy >> area_indx >> area_title >> area_type >> area_npars) ) {
85  std::cout << "CaloLocalHadCoeffHelper::initDataFromFile() -> Error! Could not parse line '" << cLine << "' at p1." << std::endl;
86  delete data; return nullptr;
87  }
88 
89  CaloLocalHadCoeff::LocalHadArea theArea(area_title.c_str(), area_type, area_npars);
90 
91  // loop over defined dimensions
92  while(fin.getline(cLine,sizeof(cLine)-1)){
93  if( cLine[0] == '#') continue;
94  sLine = cLine;
95  if(sLine.find("break") != std::string::npos) {
96  break;
97  }
99  if( !dim ) {
100  std::cout << "CaloLocalHadCoeffHelper::initDataFromFile() ->Error! Could not parse line '" << sLine << "' at p2a." << std::endl;
101  delete data; return nullptr;
102  }
103  theArea.addDimension(*dim);
104  delete dim;
105  }
106 
107  data->addArea(theArea);
108 
109  // now reading parameters
110  for(int i_len=0; i_len<theArea.getLength(); i_len++){
111  if(!fin.getline(cLine,sizeof(cLine)-1)) {
112  std::cout << "panic " << std::endl;
113  delete data; return nullptr;
114  }
115  sLine = cLine;
116  ist.clear(); ist.str(sLine);
117  int idummy;
118  if( !(ist >> idummy) ) {
119  std::cout << "CaloLocalHadCoeffHelper::initDataFromFile() -> Warning! Area " << theArea.getTitle() << " doesn't have parameters." << std::endl;
120  break;
121  }
122  if(idummy != theArea.getOffset()+i_len){
123  std::cout << "CaloLocalHadCoeffHelper::initDataFromFile() ->Error! Could not parse line '" << cLine << "' at p3." << std::endl;
124  delete data; return nullptr;
125  }
126  for(int j=0; j<theArea.getNdim(); j++) {
127  if(!(ist >> idummy)) {
128  std::cout << "CaloLocalHadCoeffHelper::initDataFromFile() -> panic!" << std::endl;
129  delete data; return nullptr;
130  }
131  }
133  pars.resize(theArea.getNpars(),0.0);
134  for(int j=0; j<theArea.getNpars(); j++) {
135  if( !(ist >> pars[j]) ) {
136  std::cout << "CaloLocalHadCoeffHelper::initDataFromFile() ->Error! Could not parse line '" << cLine << "' at p4." << std::endl;
137  std::cout << " dmArea.m_title" << theArea.getTitle() << std::endl;
138  delete data; return nullptr;
139  }
140  }
141  data->setCoeff(theArea.getOffset()+i_len, pars);
142  }
143  }
144  fin.close();
145 
146  return data;
147 }
148 
149 
150 
151 /* ****************************************************************************
152 
153 **************************************************************************** */
155 {
156  std::ofstream fout;
157  fout.open(fname);
158  PrintData(data, fout);
159  fout.close();
160 }
161 
162 
163 
164 /* ****************************************************************************
165 
166 **************************************************************************** */
168 {
169  const char *comments =
170  {
171  "# Coefficients for local hadronic calibration .\n\n"
172  };
173  fout << comments << std::endl;
174  char line[1024];
175 
176  // loop over areas
177  for(int i_area=0; i_area < data->getSizeAreaSet(); i_area++){
178  const CaloLocalHadCoeff::LocalHadArea *area = data->getArea(i_area);
179  fout << "area " << i_area << " " << area->getTitle() << " " << area->getType() << " " << area->getNpars() << std::endl;
180  for(int i_dim=0; i_dim<area->getNdim(); i_dim++){
181  const CaloLocalHadCoeff::LocalHadDimension *dim = area->getDimension(i_dim);
182  sprintf(line,"%-6s %2d %6.3f %12.3f ",dim->getTitle().c_str(), dim->getNbins(), dim->getXmin(), dim->getXmax() );
183  std::string sline(line);
184  sline += "flat";
185  fout << sline;
186 // if( !dim.m_xbins.size() ) {
187 // sline += "flat";
188 // fout << sline;
189 // }else {
190 // sline += "hand";
191 // fout << sline;
192 // for(unsigned int i=0; i<dim.m_xbins.size(); i++){
193 // fout << " " << dim.m_xbins[i];
194 // }
195 // }
196  fout << std::endl;
197  }
198  fout << "break" << std::endl; // i.e. no more dimensions
199 
200  // now printing the data
201  for(int i_data=0; i_data<area->getLength(); i_data++) {
202  int indx = area->getOffset() + i_data;
203  const CaloLocalHadCoeff::LocalHadCoeff *pars = data->getCoeff(indx);
204  if( !pars ) {
205  std::cout << "CaloLocalHadCoeffHelper::PrintData() -> Error! Wrong bin number" << std::endl;
206  return;
207  }
208  boost::io::ios_base_all_saver foutsave (fout);
209  fout << std::setw(5) << indx << " ";
210  std::vector<int > v_dim_indexes;
211  data->bin2indexes(indx, v_dim_indexes);
212  for(unsigned int i_dim=0; i_dim<v_dim_indexes.size(); i_dim++){
213  fout << std::setw(4) << v_dim_indexes[i_dim] << " ";
214  }
215  fout << " ";
216  for(unsigned int i_par=0; i_par<(*pars).size(); i_par++) {
217  fout << std::fixed << std::setprecision(6) << std::setw(12) << (*pars)[i_par] << " ";
218  }
219  fout << std::endl;
220  }
221 
222  // end of DM area
223  fout << std::endl;
224  }
225  // printing title strin
226 }
227 
228 
229 
230 /* **************************************************************************
231 parsing dimension string of type 'ener 8 3.1 6.3'
232 *************************************************************************** */
234 {
236  std::istringstream ist(sLine.c_str());
237 
238  std::string dim_title;
239  std::string stype;
240  int dim_nbins(0), dim_type(0);
241  float dim_xmax(0), dim_xmin(0);
242 
243  if( !(ist >> dim_title >> dim_nbins >> dim_xmin >> dim_xmax >> stype) ||
244  dim_nbins <= 0 ||
245  dim_nbins > 1000)
246  {
247  std::cout << "CaloHadDMCoeffHelper::parse_dim() -> Error! Could not parse line '" << sLine << "' at p1." << std::endl;
248  return nullptr;
249  }
250 
251  if(stype.find("flat") != std::string::npos) {
252  // equidistant binning
253  dim = new CaloLocalHadCoeff::LocalHadDimension(dim_title.c_str(), dim_type, dim_nbins, dim_xmin, dim_xmax);
254  }else if(stype.find("hand") != std::string::npos) {
255  // user defined binning
256  std::vector<float> x_bins;
257  float e;
258  for(int i=0; i<dim_nbins+1; i++) {
259  if( !(ist >> e) ) {
260  std::cout << "CaloHadDMCoeffHelper::parse_dim() -> Error! Could not parse line '" << sLine << "' at p2." << std::endl;
261  return nullptr;
262  }else{
263  x_bins.push_back(e);
264  }
265  }
266  dim = new CaloLocalHadCoeff::LocalHadDimension(dim_title.c_str(), dim_type, x_bins);
267  }else{
268  std::cout << "CaloHadDMCoeffHelper::parse_dim() -> Error! Could not parse line '" << sLine << "' at p3." << std::endl;
269  return nullptr;
270  }
271  return dim;
272 }
make_hlt_rep.pars
pars
Definition: make_hlt_rep.py:90
CaloLocalHadCoeff::LocalHadDimension
Class defines binning for user dimension.
Definition: CaloLocalHadCoeff.h:47
data
char data[hepevt_bytes_allocation_ATLAS]
Definition: HepEvt.cxx:11
WriteBchToCool.comments
comments
Definition: WriteBchToCool.py:297
checkFileSG.line
line
Definition: checkFileSG.py:75
yodamerge_tmp.dim
dim
Definition: yodamerge_tmp.py:239
CaloLocalHadCoeffHelper::parse_dim
CaloLocalHadCoeff::LocalHadDimension * parse_dim(std::string &sLine)
Definition: CaloLocalHadCoeffHelper.cxx:233
CaloLocalHadCoeff::LocalHadArea::getOffset
int getOffset() const
return area offset
Definition: CaloLocalHadCoeff.h:175
CaloLocalHadCoeff::LocalHadArea::getLength
int getLength() const
return area length
Definition: CaloLocalHadCoeff.h:177
CaloLocalHadCoeffHelper::PrintData
void PrintData(const CaloLocalHadCoeff *data, std::ostream &fout)
Definition: CaloLocalHadCoeffHelper.cxx:167
CaloLocalHadCoeff::getSizeAreaSet
int getSizeAreaSet() const
return number of areas defined for this data set
Definition: CaloLocalHadCoeff.h:248
CaloLocalHadCoeff::LocalHadCoeff
std::vector< float > LocalHadCoeff
Correction parameters for one general bin.
Definition: CaloLocalHadCoeff.h:220
CaloLocalHadCoeffHelper::InitDataFromFile
CaloLocalHadCoeff * InitDataFromFile(const char *fname)
Definition: CaloLocalHadCoeffHelper.cxx:59
CaloLocalHadCoeff::LocalHadArea::addDimension
void addDimension(LocalHadDimension &dim)
to add new dimension
Definition: CaloLocalHadCoeff.cxx:135
lumiFormat.i
int i
Definition: lumiFormat.py:92
dqt_zlumi_alleff_HIST.fout
fout
Definition: dqt_zlumi_alleff_HIST.py:59
CaloLocalHadCoeff
Hold binned correction data for local hadronic calibration procedure.
Definition: CaloLocalHadCoeff.h:41
CaloLocalHadCoeffHelper.h
MAX_BUFFER_LEN
#define MAX_BUFFER_LEN
Definition: CaloLocalHadCoeffHelper.cxx:26
CaloLocalHadCoeffHelper::getAreaFromName
const CaloLocalHadCoeff::LocalHadArea * getAreaFromName(const CaloLocalHadCoeff *coeff, const std::string &sname, int &indx) const
Definition: CaloLocalHadCoeffHelper.cxx:42
CaloLocalHadCoeff::LocalHadArea::getNdim
int getNdim() const
get number of dimensions
Definition: CaloLocalHadCoeff.h:179
CaloLocalHadCoeff::LocalHadArea::getNpars
int getNpars() const
return number of parameters
Definition: CaloLocalHadCoeff.h:173
python.AthDsoLogger.fname
string fname
Definition: AthDsoLogger.py:67
DiTauMassTools::MaxHistStrategyV2::e
e
Definition: PhysicsAnalysis/TauID/DiTauMassTools/DiTauMassTools/HelperFunctions.h:26
CaloLocalHadCoeffHelper::~CaloLocalHadCoeffHelper
virtual ~CaloLocalHadCoeffHelper()
Definition: CaloLocalHadCoeffHelper.cxx:35
CaloCellTimeCorrFiller.filename
filename
Definition: CaloCellTimeCorrFiller.py:24
CaloLocalHadCoeff::LocalHadArea
Definition of correction area.
Definition: CaloLocalHadCoeff.h:145
compute_lumi.fin
fin
Definition: compute_lumi.py:19
CaloLocalHadCoeff::getArea
const LocalHadArea * getArea(int n_area) const
return area
Definition: CaloLocalHadCoeff.cxx:201
area
double area(double R)
Definition: ConvertStaveServices.cxx:42
CaloLocalHadCoeff::LocalHadArea::getTitle
std::string getTitle() const
return name
Definition: CaloLocalHadCoeff.h:183
CaloLocalHadCoeffHelper::CaloLocalHadCoeffHelper
CaloLocalHadCoeffHelper()
Definition: CaloLocalHadCoeffHelper.cxx:29