127int main(
int argc,
char** argv ) {
129 if ( argc<3 )
return usage( argc, argv, 1 );
131 bool allflag =
false;
133 std::vector<std::string> keys;
138 std::vector<std::string> Keys;
141 std::vector<std::string>
veto;
143 std::string creturn =
"";
146 bool usecounts =
false;
147 bool excluded =
false;
151 std::string afile =
"";
152 std::string aslice =
"";
154 double userthreshold = 100;
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 );
168 else if ( arg==
"-cr" ) creturn =
"\n";
169 else if ( arg==
"-c" || arg==
"--counts" ) usecounts =
true;
170 else if ( arg==
"-k" ) {
176 keys.push_back(argv[i]);
178 else return usage( argc, argv, 2 );
180 else if ( arg==
"-K" ) {
186 Keys.push_back(argv[i]);
188 else return usage( argc, argv, 3 );
190 else if ( arg==
"--veto" ) {
191 if ( ++i<argc )
veto.push_back(argv[i]);
192 else return usage( argc, argv, 4 );
194 else if ( arg==
"-n" ) {
195 if ( ++i<argc ) n = std::atoi(argv[i]);
196 else return usage( argc, argv, 5 );
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 );
207 if ( afile==
"" ) afile=std::move(arg);
208 else if ( aslice==
"" ) aslice=std::move(arg);
209 else return usage( argc, argv, 7 );
213 if ( afile==
"" )
return usage( argc, argv, 8 );
215 std::ifstream
file( afile.c_str() );
217 std::vector<std::vector<std::string> > block(1,std::vector<std::string>() );
221 std::vector<int> position(1, 0);
223 if ( aslice==
"" )
return usage( argc, argv, 9 );
225 std::string slice =
"/" + aslice +
'/';
227 std::string rawslice = std::move(aslice);
229 std::map<std::string, std::string> chains;
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",
"" ) );
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",
"" ) );
246 if ( sig !=
"" ) chains.insert( chain_map::value_type( sig, pat ) );
248 chain_map::const_iterator itr = chains.find( rawslice );
250 if ( itr==chains.end() ) {
251 std::cerr <<
"can not process type " << rawslice << std::endl;
255 std::string tag =
"";
256 if ( itr->second !=
"" ) {
261 std::vector<threshold> thresholds;
263 std::string fullpath =
"";
267 bool purity =
contains( slice,
"Purity" );
269 while( getline(
file, line ) && !
file.fail() ) {
271 if ( !
contains( line, slice ) )
continue;
273 std::vector<std::string> expl =
split( line );
277 unsigned expected_size = 0;
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;
295 std::cerr <<
"unknown HIST type " << expl[
index+2] << std::endl;
300 std::cerr <<
"unknown HIST type " << expl[
index] <<
" (2)" << std::endl;
304 if ( expl.size() > expected_size ) {
306 int counts = std::atof(expl[expected_size].c_str());
312 if ( counts >= userthreshold ) {
314 std::string tmp = expl[expected_size-2];
316 if ( !tag.empty() ) {
317 if (
contains( tmp, tag ) ) tmp.replace( tmp.find(tag), tag.size(),
"" );
319 size_t pos = tmp.find(
'_');
320 if ( pos!=std::string::npos ) tmp.replace( pos, tmp.size()-pos,
"" );
323 bool condition =
false;
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;
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] );
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] ) );
332 if ( condition && ( excluded || ( !
contains( expl[5],
"EFID" ) && !
contains( expl[4],
"gsc" ) &&
338 double thresh_var = std::atof( tmp.c_str() );
339 int counts_var = counts;
342 counts_var = int(thresh_var);
346 if ( expected_size >= 2 ) {
348 threshold t( thresh_var, it++, expl[expected_size-2], expl[expected_size-1], counts_var );
350 if (
verbose && fullpath==
"" )
for (
unsigned ip=0 ; ip<expected_size-2 ; ip++ ) fullpath += expl[ip]+
'/';
356 thresholds.push_back( std::move(t) );
366 if ( usecounts )
sort( thresholds.rbegin(), thresholds.rend() );
367 else sort( thresholds.begin(), thresholds.end() );
380 for (
size_t i=0 ; i<thresholds.size() ; i++ )
id.push_back(i);
383 else if ( usecounts ) {
387 for (
size_t i=0 ; i<thresholds.size() && i<n ; i++ )
id.push_back(i);
394 std::vector<unsigned> ind(n,0);
396 if ( thresholds.size() == 0 ) {
397 std::cerr <<
"no valid thresholds found" << std::endl;
402 if ( rawslice==
"Bjet" ) {
403 for (
size_t i=0 ; i<thresholds.size() ; i++ ) {
407 if ( thresholds[i].thresh > 30 )
break;
411 for (
size_t i=thresholds.size() ; i-- ; ) {
413 if ( thresholds[i].counts > 0.05*thresholds[ind[0]].counts ) {
421 for (
unsigned it=1 ; n>0 && it<n-1 ; it++ ) {
423 double tn = std::log(thresholds[ind[n-1]].thresh);
424 double t0 = std::log(thresholds[ind[0]].thresh);
426 double f = it*1.0/(n-1);
428 double cthresh = std::exp( f*tn + (1-f)*
t0 );
432 ind[it] = int( f*ind[n-1]+(1-f)*ind[0] );
436 double delta = cthresh - thresholds[ind[it]].thresh;
438 double R2 = delta*delta;
440 for (
size_t i=ind[0]+1 ; i<ind[n-1]-1 ; i++ ) {
441 double delta = cthresh - thresholds[i].thresh;
443 double R2_ = delta*delta;
453 id.push_back( ind[0] );
456 for (
unsigned i=1 ; i<n ; i++ ) {
457 if ( ind[i]!=ind[i-1] )
id.push_back( ind[i] );
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;