ATLAS Offline Software
chainparser.cxx
Go to the documentation of this file.
1 /* emacs: this is -*- c++ -*- */
11 #include <iostream>
12 #include <fstream>
13 #include <string>
14 #include <vector>
15 #include <cstdlib>
16 #include <cstdio>
17 #include <map>
18 #include <regex>
19 
20 
21 
22 #include <cmath>
23 
24 #include <algorithm>
25 
26 #include "utils.h"
27 
28 template<typename T>
29 std::ostream& operator<<( std::ostream& s, std::vector<T>& v ) {
30  for ( size_t i=0 ; i<v.size() ; i++ ) s << v[i] << std::endl;
31  return s;
32 }
33 
34 
35 bool contains( const std::string& s, const std::string& regx ) {
36  return s.find( regx )!=std::string::npos;
37 }
38 
39 bool contains( const std::string& s, char c ) {
40  return s.find( c )!=std::string::npos;
41 }
42 
43 
44 bool contains_end( const std::string& s, const std::string& regx ) {
45  return std::regex_match( s, std::regex(regx+'$') );
46 }
47 
48 
49 std::vector<std::string> split( std::string& line ) {
50  std::vector<std::string> strings;
51  while ( contains( line, '/' ) ) line.replace( line.find('/'), 1, " " );
52  std::replace(line.begin(), line.end(), '\t', ' ');
53 
54  while ( contains( line, ' ' ) ) {
55  std::string stmp = chop( line, " " );
56  std::string s = chop( stmp, "\t" );
57  if ( contains( s, '\t' ) ) {
58  line.replace( s.find('\t'), 1, " " );
59  continue;
60  }
61  while ( contains( s, ' ' ) ) line.replace( s.find(' '), 1, "" );
62  if ( !s.empty() ) strings.push_back( s );
63  }
64 
65  strings.push_back( line );
66 
67  // for ( size_t i=0 ; i<strings.size() ; i++ ) std::cout << " : " << strings[i];
68  // std::cout << std::endl;
69 
70  return strings;
71 }
72 
73 
74 struct threshold {
75 
76  threshold( double t_, int i_, const std::string& c, const std::string& k, int co ) :
77  i(i_), thresh(t_), chain(c), key(k), counts(co) { }
78 
79  int i;
80  double thresh;
81 
82  std::string chain;
83  std::string key;
84 
85  int counts;
86 
87 };
88 
89 bool operator< ( const threshold& t0, const threshold& t1 ) { return t0.thresh < t1.thresh; }
90 bool operator> ( const threshold& t0, const threshold& t1 ) { return t0.thresh > t1.thresh; }
91 bool operator==( const threshold& t0, const threshold& t1 ) { return t0.thresh == t1.thresh; }
92 bool operator<=( const threshold& t0, const threshold& t1 ) { return t0.thresh <= t1.thresh; }
93 
94 std::ostream& operator<<( std::ostream& s, const threshold& t ) {
95  return s << t.chain << "\t" << t.key << "\t(" << t.thresh << ")\t" << t.counts;
96 }
97 
98 
99 int usage( int , char** argv, int i=0 ) {
100  if ( i!=0 ) std::cout << "error " << i << "\n\n";
101 
102  std::cout << "Usage: " << argv[0] << " FILE SLICE [OPTIONS] file\n\n";
103 
104  std::cout << "\t decodes which chains are available from a HIST file log\n";
105  std::cout << "\t by default, sorts chains by threshold\n";
106 
107  std::cout << "Options:\n";
108  std::cout << " -all \t list all chains\n";
109  std::cout << " -cr, --return \t separate chains with a return\n";
110  std::cout << " -c, --counts \t sort by counts rather than threshold\n";
111  std::cout << " -t, --thresh value \t require at least value entries - default is 100\n";
112  std::cout << " -n value \t report only n chains - default is 4\n\n";
113 
114  std::cout << " -k expression \t match any chain containing expression\n";
115  std::cout << " -K expression \t match only chains containing expression\n";
116  std::cout << " --veto expression \t veto all chains containing \"expression\"\n";
117  std::cout << " -x, --excluded \t also include usually excluded entries\n\n";
118  std::cout << " -s, --sig sig pat \t add a new signature, sig, to the expected list with the pattern definied by pat\n";
119 
120  std::cout << " -v, --verbose \t verbose output\n\n";
121  std::cout << " -h, --help \t this help\n" << std::endl;
122 
123  return i;
124 }
125 
126 
127 int main( int argc, char** argv ) {
128 
129  if ( argc<3 ) return usage( argc, argv, 1 );
130 
131  bool allflag = false;
132 
133  std::vector<std::string> keys;
134 
135  // keys.push_back( "IDTrig" );
136  bool nokeys = true;
137 
138  std::vector<std::string> Keys;
139  bool noKeys = true;
140 
141  std::vector<std::string> veto;
142 
143  std::string creturn = "";
144 
145  bool verbose = false;
146  bool usecounts = false;
147  bool excluded = false;
148 
149  unsigned n = 4;
150 
151  std::string afile = "";
152  std::string aslice = "";
153 
154  double userthreshold = 100;
155 
156  std::string sig="";
157  std::string pat="";
158 
159 
160  for ( int i=1 ; i<argc ; i++ ) {
161  std::string arg = argv[i];
162  if ( arg=="-all" ) allflag = true;
163  else if ( arg=="-v" || arg=="--verbose" ) verbose = true;
164  else if ( arg=="-t" || arg=="--threshold" ) {
165  if ( ++i<argc ) userthreshold = std::atof(argv[i]);
166  else return usage( argc, argv, 1 );
167  }
168  else if ( arg=="-cr" ) creturn = "\n";
169  else if ( arg=="-c" || arg=="--counts" ) usecounts = true;
170  else if ( arg=="-k" ) {
171  if ( ++i<argc ) {
172  if ( nokeys ) {
173  keys.clear();
174  nokeys = false;
175  }
176  keys.push_back(argv[i]);
177  }
178  else return usage( argc, argv, 2 );
179  }
180  else if ( arg=="-K" ) {
181  if ( ++i<argc ) {
182  if ( noKeys ) {
183  Keys.clear();
184  noKeys = false;
185  }
186  Keys.push_back(argv[i]);
187  }
188  else return usage( argc, argv, 3 );
189  }
190  else if ( arg=="--veto" ) {
191  if ( ++i<argc ) veto.push_back(argv[i]);
192  else return usage( argc, argv, 4 );
193  }
194  else if ( arg=="-n" ) {
195  if ( ++i<argc ) n = std::atoi(argv[i]);
196  else return usage( argc, argv, 5 );
197  }
198  else if ( arg=="-h" || arg=="--help" ) return usage( argc, argv, 6 );
199  else if ( arg=="-x" || arg=="--excluded" ) excluded = true;
200  else if ( arg=="-s" || arg=="--sig" ) {
201  if ( ++i<argc ) sig = argv[i];
202  else return usage( argc, argv, 1 );
203  if ( ++i<argc ) pat = argv[i];
204  else return usage( argc, argv, 1 );
205  }
206  else {
207  if ( afile=="" ) afile=arg;
208  else if ( aslice=="" ) aslice=arg;
209  else return usage( argc, argv, 7 );
210  }
211  }
212 
213  if ( afile=="" ) return usage( argc, argv, 8 );
214 
215  std::ifstream file( afile.c_str() );
216 
217  std::vector<std::vector<std::string> > block(1,std::vector<std::string>() );
218 
219  std::string line;
220 
221  std::vector<int> position(1, 0);
222 
223  if ( aslice=="" ) return usage( argc, argv, 9 );
224 
225  std::string slice = "/" + aslice + '/';
226 
227  std::string rawslice = aslice;
228 
229  std::map<std::string, std::string> chains;
230 
231  typedef std::map<std::string, std::string> chain_map;
232  chains.insert( chain_map::value_type( "Electron", "_e" ) );
233  chains.insert( chain_map::value_type( "Egamma", "_e" ) );
234  chains.insert( chain_map::value_type( "Muon", "_mu" ) );
235  chains.insert( chain_map::value_type( "Tau", "_tau" ) );
236  chains.insert( chain_map::value_type( "Bjet", "_j" ) );
237  chains.insert( chain_map::value_type( "FTK", "_FTK" ) );
238  chains.insert( chain_map::value_type( "BjetVtx", "" ) );
239 
240  chains.insert( chain_map::value_type( "EgammaPurity", "_e" ) );
241  chains.insert( chain_map::value_type( "MuonPurity", "_mu" ) );
242  chains.insert( chain_map::value_type( "TauPurity", "_tau" ) );
243  chains.insert( chain_map::value_type( "BjetPurity", "_j" ) );
244  chains.insert( chain_map::value_type( "FTKPurity", "" ) );
245 
246  if ( sig != "" ) chains.insert( chain_map::value_type( sig, pat ) );
247 
248  chain_map::const_iterator itr = chains.find( rawslice );
249 
250  if ( itr==chains.end() ) {
251  std::cerr << "can not process type " << rawslice << std::endl;
252  return -1;
253  }
254 
255  std::string tag = "";
256  if ( itr->second != "" ) {
257  tag = "HLT";
258  tag += itr->second;
259  }
260 
261  std::vector<threshold> thresholds;
262 
263  std::string fullpath = "";
264 
266 
267  bool purity = contains( slice, "Purity" );
268 
269  while( getline( file, line ) && !file.fail() ) {
270 
271  if ( !contains( line, slice ) ) continue;
272 
273  std::vector<std::string> expl = split( line );
274 
275  int it = 0;
276 
277  unsigned expected_size = 0;
278 
279 
280  int index = 2;
281 
282  int diff = 0;
283 
284  if ( expl[0].find("run_")==std::string::npos ) index=diff=1;
285 
286  // bool shifter = false;
287 
288  if ( expl[index] == "IDMon" ) expected_size = 6-diff;
289  else if ( expl[index] == "TRIDT" ) {
290  if ( expl[index+2] == "Expert" && !purity ) expected_size = 7-diff;
291  else if ( expl[index+2] == "Shifter" && contains(expl[3],"etVtx") ) expected_size = 7-diff;
292  else if ( expl[index+3] == "Fullscan" && purity ) expected_size = 7-diff;
293  else if ( expl[index+2] == "Shifter" || purity ) expected_size = 6-diff;
294  else {
295  std::cerr << "unknown HIST type " << expl[index+2] << std::endl;
296  return 1;
297  }
298  }
299  else {
300  std::cerr << "unknown HIST type " << expl[index] << " (2)" << std::endl;
301  return 1;
302  }
303 
304  if ( expl.size() > expected_size ) {
305 
306  int counts = std::atof(expl[expected_size].c_str());
307 
308  // for ( size_t ie=0 ; ie<expl.size() ; ie++ ) std::cout << ie << " " << expl[ie] << " / ";
309  // std::cout << "counts: " << counts << " (" << expected_size << ")" << std::endl;
310 
312  if ( counts >= userthreshold ) {
313 
314  std::string tmp = expl[expected_size-2];
315 
316  if ( !tag.empty() ) {
317  if ( contains( tmp, tag ) ) tmp.replace( tmp.find(tag), tag.size(), "" );
318 
319  size_t pos = tmp.find('_');
320  if ( pos!=std::string::npos ) tmp.replace( pos, tmp.size()-pos, "" );
321  }
322 
323  bool condition = false;
324 
325  if ( keys.size()>0 ) for ( size_t ik=keys.size() ; ik-- ; ) condition |= contains( expl[expected_size-2], keys[ik] ) || contains( expl[expected_size-1], keys[ik] );
326  else condition = true;
327 
328  if ( Keys.size()>0 ) for ( size_t ik=Keys.size() ; ik-- ; ) condition &= contains( expl[expected_size-2], Keys[ik] ) || contains( expl[expected_size-1], Keys[ik] );
329 
330  if ( veto.size()>0 ) for ( size_t iv=veto.size() ; iv-- ; ) condition &= !( contains( expl[expected_size-2], veto[iv] ) || contains( expl[expected_size-1], veto[iv] ) );
331 
332  if ( condition && ( excluded || ( !contains( expl[5], "EFID" ) && !contains( expl[4], "gsc" ) &&
333  !contains( expl[4], "medium" ) && !contains( expl[4], "loose" ) &&
334  !contains( expl[4], "2015" ) && !contains( expl[4], "_r1_" ) &&
335  !( contains( expl[4], "_track" ) && !contains( expl[4], "tracktwo") )&&
336  !contains( expl[4], "L2Star" ) && !contains( expl[4], "Shifter" ) ) ) ) {
337 
338  double thresh_var = std::atof( tmp.c_str() );
339  int counts_var = counts;
340 
341  if ( usecounts ) {
342  counts_var = int(thresh_var);
343  thresh_var = counts;
344  }
345 
346  if ( expected_size >= 2 ) {
347 
348  threshold t( thresh_var, it++, expl[expected_size-2], expl[expected_size-1], counts_var );
349 
350  if ( verbose && fullpath=="" ) for ( unsigned ip=0 ; ip<expected_size-2 ; ip++ ) fullpath += expl[ip]+'/';
351 
352  // std::cout << "\tfullpath " << fullpath << std::endl;
353  // std::cout << "\t" << t << "\t" << contains( expl[4], "_track_" ) << std::endl;
354  // std::cout << "\tadding: " << t << std::endl;
355 
356  thresholds.push_back( t );
357  }
358  }
359  }
360  }
361  }
362 
363 
365 
366  if ( usecounts ) sort( thresholds.rbegin(), thresholds.rend() );
367  else sort( thresholds.begin(), thresholds.end() );
368 
369 
371 
372  std::vector<int> id;
373 
374 
375 
376  if ( allflag ) {
377 
378  // write out all thresholds
379 
380  for ( size_t i=0 ; i<thresholds.size() ; i++ ) id.push_back(i);
381 
382  }
383  else if ( usecounts ) {
384 
386 
387  for ( size_t i=0 ; i<thresholds.size() && i<n ; i++ ) id.push_back(i);
388 
389  }
390  else {
391 
392  // select (up to) 3 representative thresholds
393 
394  std::vector<unsigned> ind(n,0);
395 
396  if ( thresholds.size() == 0 ) {
397  std::cerr << "no valid thresholds found" << std::endl;
398  return -2;
399  }
400 
401 
402  if ( rawslice=="Bjet" ) {
403  for ( size_t i=0 ; i<thresholds.size() ; i++ ) {
404  ind[0] = i;
407  if ( thresholds[i].thresh > 30 ) break;
408  }
409  }
410 
411  for ( size_t i=thresholds.size() ; i-- ; ) {
412  // if ( thresholds[i].counts > 0.05*thresholds[ind[0]].counts ) {
413  if ( thresholds[i].counts > 0.05*thresholds[ind[0]].counts ) {
414  ind[n-1] = i;
415  break;
416  }
417  }
418 
420 
421  for ( unsigned it=1 ; n>0 && it<n-1 ; it++ ) {
422 
423  double tn = std::log(thresholds[ind[n-1]].thresh);
424  double t0 = std::log(thresholds[ind[0]].thresh);
425 
426  double f = it*1.0/(n-1);
427 
428  double cthresh = std::exp( f*tn + (1-f)*t0 );
429 
430  // double cthresh = std::exp( (it/(n-1))*std::log(thresholds[ind[n-1]].thresh) - (it/(n-1) - 1)*std::log(thresholds[ind[0]].thresh ) ;
431 
432  ind[it] = int( f*ind[n-1]+(1-f)*ind[0] );
433 
434  // std::cout << "it " << it << "\tf " << f << "\tcthresh " << cthresh << "\tint " << ind[it] << "\tthresholds size " << thresholds.size() << std::endl;
435 
436  double delta = cthresh - thresholds[ind[it]].thresh;
437 
438  double R2 = delta*delta;
439 
440  for ( size_t i=ind[0]+1 ; i<ind[n-1]-1 ; i++ ) {
441  double delta = cthresh - thresholds[i].thresh;
442 
443  double R2_ = delta*delta;
444 
445  if ( R2_ < R2 ) {
446  ind[it] = i;
447  R2 = R2_;
448  }
449 
450  }
451  }
452 
453  id.push_back( ind[0] );
454 
455 
456  for ( unsigned i=1 ; i<n ; i++ ) {
457  if ( ind[i]!=ind[i-1] ) id.push_back( ind[i] );
458  }
459 
460  // std::cout << "indices " << id << std::endl;
461 
462  }
463 
464  for ( size_t i=0 ; i<id.size() ; i++ ) {
465  if ( verbose ) std::cout << " " << fullpath << thresholds[id[i]].chain << "/" << thresholds[id[i]].key << creturn;
466  else std::cout << " " << thresholds[id[i]].chain << "_" << thresholds[id[i]].key << creturn;
467  }
468 
469  return 0;
470 }
471 
replace
std::string replace(std::string s, const std::string &s2, const std::string &s3)
Definition: hcg.cxx:307
checkFileSG.line
line
Definition: checkFileSG.py:75
threshold::thresh
double thresh
Definition: chainparser.cxx:80
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
main
int main(int argc, char **argv)
Definition: chainparser.cxx:127
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
physval_make_web_display.thresh
thresh
Definition: physval_make_web_display.py:36
index
Definition: index.py:1
ALFA_EventTPCnv_Dict::t0
std::vector< ALFA_RawData_p1 > t0
Definition: ALFA_EventTPCnvDict.h:42
ALFA_EventTPCnv_Dict::t1
std::vector< ALFA_RawDataCollection_p1 > t1
Definition: ALFA_EventTPCnvDict.h:43
skel.it
it
Definition: skel.GENtoEVGEN.py:396
mc.diff
diff
Definition: mc.SFGenPy8_MuMu_DD.py:14
chop
std::string chop(std::string &s1, const std::string &s2)
Definition: hcg.cxx:158
split
std::vector< std::string > split(std::string &line)
Definition: chainparser.cxx:49
LArG4GenerateShowerLib.condition
condition
Definition: LArG4GenerateShowerLib.py:19
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
drawFromPickle.exp
exp
Definition: drawFromPickle.py:36
operator<
bool operator<(const threshold &t0, const threshold &t1)
Definition: chainparser.cxx:89
PrepareReferenceFile.regex
regex
Definition: PrepareReferenceFile.py:43
threshold::i
int i
Definition: chainparser.cxx:79
threshold::key
std::string key
Definition: chainparser.cxx:83
operator>
bool operator>(const threshold &t0, const threshold &t1)
Definition: chainparser.cxx:90
threshold::chain
std::string chain
Definition: chainparser.cxx:82
threshold::counts
int counts
Definition: chainparser.cxx:85
utils.h
contains
bool contains(const std::string &s, const std::string &regx)
does a string contain the substring
Definition: chainparser.cxx:35
operator<<
std::ostream & operator<<(std::ostream &s, std::vector< T > &v)
Definition: chainparser.cxx:29
usage
int usage(int, char **argv, int i=0)
Definition: chainparser.cxx:99
perfmonmt-refit.slice
slice
Definition: perfmonmt-refit.py:52
lumiFormat.i
int i
Definition: lumiFormat.py:85
LArCellNtuple.argv
argv
Definition: LArCellNtuple.py:152
beamspotman.n
n
Definition: beamspotman.py:731
python.BuildSignatureFlags.sig
sig
Definition: BuildSignatureFlags.py:218
file
TFile * file
Definition: tile_monitor.h:29
find_tgc_unfilled_channelids.ip
ip
Definition: find_tgc_unfilled_channelids.py:3
hist_file_dump.f
f
Definition: hist_file_dump.py:135
DQHistogramMergeRegExp.argc
argc
Definition: DQHistogramMergeRegExp.py:20
DeMoUpdate.tmp
string tmp
Definition: DeMoUpdate.py:1167
CxxUtils::atof
double atof(std::string_view str)
Converts a string into a double / float.
Definition: Control/CxxUtils/Root/StringUtils.cxx:91
create_dcsc_inputs_sqlite.arg
list arg
Definition: create_dcsc_inputs_sqlite.py:48
dso-stats.pat
pat
Definition: dso-stats.py:39
id
SG::auxid_t id
Definition: Control/AthContainers/Root/debug.cxx:227
threshold
Definition: chainparser.cxx:74
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
python.copyTCTOutput.chains
chains
Definition: copyTCTOutput.py:81
python.PyAthena.v
v
Definition: PyAthena.py:154
DeMoScan.index
string index
Definition: DeMoScan.py:364
threshold::threshold
threshold(double t_, int i_, const std::string &c, const std::string &k, int co)
Definition: chainparser.cxx:76
python.TriggerHandler.verbose
verbose
Definition: TriggerHandler.py:297
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
contains_end
bool contains_end(const std::string &s, const std::string &regx)
Definition: chainparser.cxx:44
CxxUtils::atoi
int atoi(std::string_view str)
Helper functions to unpack numbers decoded in string into integers and doubles The strings are requir...
Definition: Control/CxxUtils/Root/StringUtils.cxx:85
python.Bindings.keys
keys
Definition: Control/AthenaPython/python/Bindings.py:798
veto
std::vector< std::string > veto
these patterns are anded
Definition: listroot.cxx:191
operator==
bool operator==(const threshold &t0, const threshold &t1)
Definition: chainparser.cxx:91
CaloCondBlobAlgs_fillNoiseFromASCII.tag
string tag
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:24
operator<=
bool operator<=(const threshold &t0, const threshold &t1)
Definition: chainparser.cxx:92
python.compressB64.c
def c
Definition: compressB64.py:93
checkFileSG.ind
list ind
Definition: checkFileSG.py:118
fitman.k
k
Definition: fitman.py:528
beamspotman.fullpath
string fullpath
Definition: beamspotman.py:1039