ATLAS Offline Software
Loading...
Searching...
No Matches
CaloLocalHadCoeffHelper.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 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
33
34
39
40
41// get HadDMArea from area name
42const 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/* ****************************************************************************
57To 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 area_npars < 0 || area_npars > 1000 ) {
86 std::cout << "CaloLocalHadCoeffHelper::initDataFromFile() -> Error! Could not parse line '" << cLine << "' at p1." << std::endl;
87 delete data; return nullptr;
88 }
89
90 CaloLocalHadCoeff::LocalHadArea theArea(area_title.c_str(), area_type, area_npars);
91
92 // loop over defined dimensions
93 while(fin.getline(cLine,sizeof(cLine)-1)){
94 if( cLine[0] == '#') continue;
95 sLine = cLine;
96 if(sLine.find("break") != std::string::npos) {
97 break;
98 }
100 if( !dim ) {
101 std::cout << "CaloLocalHadCoeffHelper::initDataFromFile() ->Error! Could not parse line '" << sLine << "' at p2a." << std::endl;
102 delete data; return nullptr;
103 }
104 theArea.addDimension(*dim);
105 delete dim;
106 }
107
108 data->addArea(theArea);
109
110 // now reading parameters
111 for(int i_len=0; i_len<theArea.getLength(); i_len++){
112 if(!fin.getline(cLine,sizeof(cLine)-1)) {
113 std::cout << "panic " << std::endl;
114 delete data; return nullptr;
115 }
116 sLine = cLine;
117 ist.clear(); ist.str(sLine);
118 int idummy;
119 if( !(ist >> idummy) ) {
120 std::cout << "CaloLocalHadCoeffHelper::initDataFromFile() -> Warning! Area " << theArea.getTitle() << " doesn't have parameters." << std::endl;
121 break;
122 }
123 if(idummy != theArea.getOffset()+i_len){
124 std::cout << "CaloLocalHadCoeffHelper::initDataFromFile() ->Error! Could not parse line '" << cLine << "' at p3." << std::endl;
125 delete data; return nullptr;
126 }
127 for(int j=0; j<theArea.getNdim(); j++) {
128 if(!(ist >> idummy)) {
129 std::cout << "CaloLocalHadCoeffHelper::initDataFromFile() -> panic!" << std::endl;
130 delete data; return nullptr;
131 }
132 }
134 pars.resize(theArea.getNpars(),0.0);
135 for(int j=0; j<theArea.getNpars(); j++) {
136 if( !(ist >> pars[j]) ) {
137 std::cout << "CaloLocalHadCoeffHelper::initDataFromFile() ->Error! Could not parse line '" << cLine << "' at p4." << std::endl;
138 std::cout << " dmArea.m_title" << theArea.getTitle() << std::endl;
139 delete data; return nullptr;
140 }
141 }
142 data->setCoeff(theArea.getOffset()+i_len, pars);
143 }
144 }
145 fin.close();
146
147 return data;
148}
149
150
151
152/* ****************************************************************************
153
154**************************************************************************** */
156{
157 std::ofstream fout;
158 fout.open(fname);
160 fout.close();
161}
162
163
164
165/* ****************************************************************************
166
167**************************************************************************** */
169{
170 const char *comments =
171 {
172 "# Coefficients for local hadronic calibration .\n\n"
173 };
174 fout << comments << std::endl;
175 char line[1024];
176
177 // loop over areas
178 for(int i_area=0; i_area < data->getSizeAreaSet(); i_area++){
179 const CaloLocalHadCoeff::LocalHadArea *area = data->getArea(i_area);
180 fout << "area " << i_area << " " << area->getTitle() << " " << area->getType() << " " << area->getNpars() << std::endl;
181 for(int i_dim=0; i_dim<area->getNdim(); i_dim++){
182 const CaloLocalHadCoeff::LocalHadDimension *dim = area->getDimension(i_dim);
183 sprintf(line,"%-6s %2d %6.3f %12.3f ",dim->getTitle().c_str(), dim->getNbins(), dim->getXmin(), dim->getXmax() );
184 std::string sline(line);
185 sline += "flat";
186 fout << sline;
187// if( !dim.m_xbins.size() ) {
188// sline += "flat";
189// fout << sline;
190// }else {
191// sline += "hand";
192// fout << sline;
193// for(unsigned int i=0; i<dim.m_xbins.size(); i++){
194// fout << " " << dim.m_xbins[i];
195// }
196// }
197 fout << std::endl;
198 }
199 fout << "break" << std::endl; // i.e. no more dimensions
200
201 // now printing the data
202 for(int i_data=0; i_data<area->getLength(); i_data++) {
203 int indx = area->getOffset() + i_data;
204 const CaloLocalHadCoeff::LocalHadCoeff *pars = data->getCoeff(indx);
205 if( !pars ) {
206 std::cout << "CaloLocalHadCoeffHelper::PrintData() -> Error! Wrong bin number" << std::endl;
207 return;
208 }
209 boost::io::ios_base_all_saver foutsave (fout);
210 fout << std::setw(5) << indx << " ";
211 std::vector<int > v_dim_indexes;
212 data->bin2indexes(indx, v_dim_indexes);
213 for(unsigned int i_dim=0; i_dim<v_dim_indexes.size(); i_dim++){
214 fout << std::setw(4) << v_dim_indexes[i_dim] << " ";
215 }
216 fout << " ";
217 for(unsigned int i_par=0; i_par<(*pars).size(); i_par++) {
218 fout << std::fixed << std::setprecision(6) << std::setw(12) << (*pars)[i_par] << " ";
219 }
220 fout << std::endl;
221 }
222
223 // end of DM area
224 fout << std::endl;
225 }
226 // printing title strin
227}
228
229
230
231/* **************************************************************************
232parsing dimension string of type 'ener 8 3.1 6.3'
233*************************************************************************** */
235{
237 std::istringstream ist(sLine.c_str());
238
239 std::string dim_title;
240 std::string stype;
241 int dim_nbins(0), dim_type(0);
242 float dim_xmax(0), dim_xmin(0);
243
244 if( !(ist >> dim_title >> dim_nbins >> dim_xmin >> dim_xmax >> stype) ||
245 dim_nbins <= 0 ||
246 dim_nbins > 1000)
247 {
248 std::cout << "CaloHadDMCoeffHelper::parse_dim() -> Error! Could not parse line '" << sLine << "' at p1." << std::endl;
249 return nullptr;
250 }
251
252 if(stype.find("flat") != std::string::npos) {
253 // equidistant binning
254 dim = new CaloLocalHadCoeff::LocalHadDimension(dim_title.c_str(), dim_type, dim_nbins, dim_xmin, dim_xmax);
255 }else if(stype.find("hand") != std::string::npos) {
256 // user defined binning
257 std::vector<float> x_bins;
258 float e;
259 for(int i=0; i<dim_nbins+1; i++) {
260 if( !(ist >> e) ) {
261 std::cout << "CaloHadDMCoeffHelper::parse_dim() -> Error! Could not parse line '" << sLine << "' at p2." << std::endl;
262 return nullptr;
263 }else{
264 x_bins.push_back(e);
265 }
266 }
267 dim = new CaloLocalHadCoeff::LocalHadDimension(dim_title.c_str(), dim_type, x_bins);
268 }else{
269 std::cout << "CaloHadDMCoeffHelper::parse_dim() -> Error! Could not parse line '" << sLine << "' at p3." << std::endl;
270 return nullptr;
271 }
272 return dim;
273}
#define MAX_BUFFER_LEN
double area(double R)
char data[hepevt_bytes_allocation_ATLAS]
Definition HepEvt.cxx:11
CaloLocalHadCoeff * InitDataFromFile(const char *fname)
CaloLocalHadCoeff::LocalHadDimension * parse_dim(std::string &sLine)
const CaloLocalHadCoeff::LocalHadArea * getAreaFromName(const CaloLocalHadCoeff *coeff, const std::string &sname, int &indx) const
void PrintData(const CaloLocalHadCoeff *data, std::ostream &fout)
Definition of correction area.
int getLength() const
return area length
const std::string & getTitle() const
return name
int getNdim() const
get number of dimensions
int getNpars() const
return number of parameters
int getOffset() const
return area offset
void addDimension(LocalHadDimension &dim)
to add new dimension
Class defines binning for user dimension.
Hold binned correction data for local hadronic calibration procedure.
const LocalHadArea * getArea(int n_area) const
return area
std::vector< float > LocalHadCoeff
Correction parameters for one general bin.
int getSizeAreaSet() const
return number of areas defined for this data set
static TFile * fout
Definition listroot.cxx:40