ATLAS Offline Software
skim.cxx
Go to the documentation of this file.
1 
10 // cppcheck-suppress-file stlIfStrFind; cannot use C++20 starts_with in this standalone code
11 
12 #include <stdlib.h>
13 
14 #include <iostream>
15 #include <vector>
16 #include <set>
17 #include <string>
18 #include <regex>
19 #include <algorithm>
20 #include <cstdio>
21 
22 #include "simpletimer.h"
23 
24 #include "TChain.h"
25 #include "TFile.h"
26 #include "TTree.h"
27 #include "TH1D.h"
28 
30 
31 #include "utils.h"
32 
33 template<class T>
34 void remove_duplicates(std::vector<T>& vec) {
35  std::sort(vec.begin(), vec.end());
36  vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
37 }
38 
39 std::string time_str() {
40  time_t t;
41  time(&t);
42  std::string s(ctime(&t));
43  return s.substr(0,s.find('\n'));
44 }
45 
46 int usage(int e=0) {
47  std::cerr << "usage: skim <filename> [OPTIONS]" << std::endl;
48  std::cerr << "\nremoves chains from the ntple\n";
49  std::cerr << "\nOptions:\n";
50  std::cerr << "\t-d | --delete\tremoves specified chains\n";
51  std::cerr << "\t-o | --output\toutput filename (default tree.root)\n";
52  std::cerr << "\t-r | --require\trequire that this chain has tracks\n";
53  std::cerr << "\t-f | --force\tforce processing even if no chains specified\n";
54  std::cerr << "\t --pt value\tremove offline tracks pt < value\n";
55  std::cerr << "\t --roi \tfilter offline tracks by roi phi\n";
56  std::cerr << "\nIf option -d is not given, the specified chains are retained and all others are removed" << std::endl;
57  std::cerr << "\nIf no chains are specifed simply all events with no L2 or EF chains are excluded" << std::endl;
58  return e;
59 }
60 
61 template<class T>
62 std::ostream& operator<<( std::ostream& os, const std::set<T>& s ) {
63  typename std::set<T>::const_iterator sitr = s.begin();
64  os << "[ ";
65  while ( sitr!=s.end() ) os << (*sitr++) << "\t";
66  os << " ]";
67  return os;
68 }
69 
70 
71 template<class T>
72 std::ostream& operator<<( std::ostream& s, const std::vector<T>& v ) {
73  typename std::vector<T>::const_iterator sitr = v.begin();
74  s << "[ ";
75  while ( sitr!=v.end() ) s << (*sitr++) << "\t";
76  s << " ]";
77  return s;
78 }
79 
80 
81 
82 void copyReleaseInfo( TFile* finput, TFile* foutdir ) {
83 
84  if ( finput && foutdir ) {
85 
86  TTree* tree = (TTree*)finput->Get("dataTree");
87  TTree* clone = tree->CloneTree();
88 
89  foutdir->cd();
90  clone->Write("", TObject::kOverwrite);
91 
92  delete clone;
93 
94  }
95 
96 }
97 
98 
99 
100 
101 int main(int argc, char** argv) {
102 
103  if ( argc<1 ) return usage(-1);
104 
105  std::set<std::string> require_chains;
106  std::vector<std::string> rchains;
107 
108  bool require = false;
109  bool deleting = false;
110 
111  std::string outfile = "tree.root";
112 
113  std::string infile="";
114 
115  bool force = false;
116 
117  double ptmin = 0;
118 
119  bool roi_filter = false;
120 
121  bool verbose = false;
122 
123  for ( int i=1 ; i<argc ; i++ ) {
124 
125  std::string arg(argv[i]);
126 
128 
129  if ( arg.find('-')!=0 ) {
130  if ( !deleting && infile!="" ) {
131  require = true;
132  require_chains.insert(argv[i]);
133  rchains.push_back(argv[i]);
134  continue;
135  }
136  else {
137  if ( infile=="" ) infile = arg;
138  else {
139  std::cerr << "more than one file specified: " << arg << std::endl;
140  return usage(-2);
141  infile=arg;
142  }
143  }
144  }
145  else {
146  if ( arg=="-h" || arg=="--help" ) return usage(0);
147  else if ( arg=="-o" || arg=="--output" ) {
148  if ( (i+1)<argc ) outfile = argv[++i];
149  else return usage(-1);
150  }
151  else if ( arg=="-r" || arg=="--require" ) {
152  if ( deleting ) {
153  std::cerr << "cannot require and delete chains" << std::endl;
154  return usage(-4);
155  }
156  require = true;
157  }
158  else if ( arg=="-d" || arg=="--delete" ) {
159  if ( require ) {
160  std::cerr << "cannot require and delete chains" << std::endl;
161  return usage(-3);
162  }
163  deleting = true;
164  }
165  else if ( arg=="--pt" ) {
166  if ( (i+1)<argc ) ptmin = std::atof(argv[++i])*1000;
167  else return usage(-1);
168  force = true;
169  }
170  else if ( arg=="-v" || arg=="--verbose" ) verbose = true;
171  else if ( arg=="-f" || arg=="--force" ) force = true;
172  else if ( arg=="--roi" ) { force=true; roi_filter = true; }
173  else if ( infile=="" ) infile = arg;
174  else {
175  std::cerr << "more than one file specified: " << arg << std::endl;
176  return usage(-2);
177  }
178  }
179  }
180 
181  std::cout << "required chains " << require_chains << std::endl;
182 
183  if ( require_chains.size()>0 ) require = true;
184 
185  if ( !force && require_chains.size()==0 ) {
186  std::cout << "no chains requested - not doing anything" << std::endl;
187  return 0;
188  }
189 
190  std::cout << "chains: " << rchains << std::endl;
191 
192 
193  std::cout << "skim::start " << time_str() << std::endl;
194 
195 
196  if ( require ) std::cout << "require chains " << require_chains << std::endl;
197  if ( deleting ) std::cout << "delete chains " << require_chains << std::endl;
198 
199 
200  if ( infile=="" ) {
201  std::cerr << "no files specified" << std::endl;
202  return usage(-1);
203  }
204 
205  std::cout << "reading from file: " << infile << std::endl;
206  std::cout << "writing to file: " << outfile << std::endl;
207 
208 
210  TIDA::Event* track_ev = new TIDA::Event();
211  TIDA::Event* h = track_ev;
212 
213  std::cout << "opening outfile: " << outfile << std::endl;
214 
215  TFile fout( outfile.c_str(), "recreate");
216 
217  fout.cd();
218 
220 
221  TTree *tree = new TTree("tree","tree");
222 
223  tree->Branch("TIDA::Event", "TIDA::Event", &h, 6400, 1);
224 
225  h->clear();
226 
228 
229  int ev_in = 0;
230  int ev_out = 0;
231 
232  {
233 
235  TFile finput( infile.c_str() );
236  if (!finput.IsOpen()) {
237  std::cerr << "Error: could not open output file" << std::endl;
238  exit(-1);
239  }
240 
241 
242  copyReleaseInfo( &finput, &fout );
243 
244  finput.cd();
245 
247 
248  // TChain* data = new TChain("tree");
249 
250  TTree* data = (TTree*)finput.Get("tree");
251 
252  TIDA::Event* track_iev = new TIDA::Event();
253 
254  data->SetBranchAddress("TIDA::Event",&track_iev);
255  // data->AddFile( argv[i] );
256 
257 
258  unsigned entries = data->GetEntries();
259 
260  std::cout << "input has " << entries << " events" << std::endl;
261 
262  std::string cck = "|/-\\";
263 
264  int ii=0;
265 
266  struct timeval tm = simpletimer_start();
267 
268  std::cout << "processing ..." << std::endl;
269 
270  for (unsigned int i=0; i<data->GetEntries() ; i++ ) {
271 
272  if ( verbose ) std::cout << "event " << i;
273 
274  if ( i%100==0 ) {
275 
276  double frac = i*1.0/entries;
277 
278  double t = simpletimer_stop(tm);
279 
280  double est = 0;
281 
282  if ( frac > 0 ) est = t/frac - t;
283 
284  double eventsps = 1000*i/t;
285 
286  std::printf( "\r%c %6.2lf %% time: %6.2lf s remaining %6.2lf s (%u at %5.2lf ps) ",
287  cck[ii%4], ((1000*(i+1)/entries)*0.1), t*0.001, est*0.001, i, eventsps );
288 
289  std::fflush(stdout);
290 
291  ii++;
292  }
293 
294 
295 
296  track_iev->clear();
297  track_ev->clear();
298 
299  data->GetEntry(i);
300 
301  ev_in++;
302 
303  *track_ev = *track_iev;
304 
305  // std::cout << *track_ev << std::endl;
306 
307  // std::cout << "track_ev names " << track_ev->chainnames() << std::endl;
308  // std::cout << "track_iev names " << track_iev->chainnames() << std::endl;
309 
310  if ( verbose ) std::cout << "----------------------------------------\n\tN chains " << track_ev->size() << " -> ";
311 
312  std::vector<TIDA::Chain>& chains = track_ev->chains();
313 
314 
316 
317  bool skip = true;
318 
320  for ( ; citr!=chains.end() ; ++citr ) {
321 
322  if ( citr->name().find("HLT")==std::string::npos ) continue;
323 
324  if ( require ) {
325  for ( unsigned j=rchains.size() ; j-- ; ) {
326  if ( rchains[j].find("HLT")==std::string::npos ) continue;
327  if ( citr->name().find(rchains[j])!=std::string::npos ) {
328  skip = false;
329  if ( verbose ) std::cout << "keepin' " << citr->name() << " " << rchains[j] << std::endl;
330  }
331 
332  }
333  }
334 
335  }
336 
337  if ( skip ) continue;
338 
340 
341  {
342 
344 
345  std::vector<std::string> chainnames = track_ev->chainnames();
346 
347  for ( size_t ic=0 ; ic<chainnames.size() ; ic++ ) {
348 
349 
350  bool matched = false;
351  for ( std::set<std::string>::iterator it=require_chains.begin() ; it!=require_chains.end() ; ++it ) {
352 
353  matched |= std::regex_match( chainnames[ic], std::regex(*it+".*") );
354 
355  if ( verbose && matched ) std::cout << "chain: " << chainnames[ic] << "\t :: reg " << *it << "\tmatched: " << matched << std::endl;
356 
357  }
358 
359  if ( ( require && !matched ) ) track_ev->erase( chainnames[ic] );
360  }
361 
362  if ( verbose ) std::cout << track_ev->size() << std::endl;
363 
364 
365  if ( roi_filter || ptmin>0 ) {
366 
367  TIDA::Chain* offline = 0;
368 
369  std::vector<std::string> chainnames = track_ev->chainnames();
370 
372 
373  for ( size_t ic=chainnames.size() ; ic-- ; ) {
374  if ( chainnames[ic] == "Offline" ) {
375  offline = &(track_ev->chains()[ic]);
376  break;
377  }
378  }
379 
380  if ( offline ) {
381  // track_ev->addChain( "Offline" );
382 
383  std::vector<TIDA::Chain>& chains = track_ev->chains();
385 
386  std::vector<std::pair<double,double> > philims;
387 
388  for ( ; citr!=chains.end() ; ++citr ) {
389  if ( citr->name().find("HLT_")!=std::string::npos ) {
390  for ( size_t ir=0 ; ir<citr->size() ; ir++ ) {
391  TIDARoiDescriptor& roi = citr->rois()[ir].roi();
392  if ( roi.composite() ) {
393  for ( size_t isub=0 ; isub<roi.size() ; isub++ ) {
394  philims.push_back( std::pair<double,double>( roi[isub]->phiMinus(), roi[isub]->phiPlus() ) );
395  }
396  }
397  else philims.push_back( std::pair<double,double>( roi.phiMinus(), roi.phiPlus() ) );
398  }
399  }
400  }
401 
402  remove_duplicates( philims );
403 
404  for ( size_t iroi=0 ; iroi<offline->size() ; iroi++ ) {
405 
406  std::vector<TIDA::Track>& tracks = offline->rois()[iroi].tracks();
407 
408  for ( std::vector<TIDA::Track>::iterator it=tracks.begin() ; it<tracks.end() ; ) {
409  bool inc = true;
410  if ( ptmin>0 ) {
411  if ( std::fabs(it->pT())<ptmin ) { inc=false; tracks.erase( it ); }
412  }
413  if ( inc && roi_filter ) {
414  bool remove_track = true;
415  for ( size_t isub=0 ; isub<philims.size() ; isub++ ) {
416 
417  if ( philims[isub].first < philims[isub].second ) {
418  if ( it->phi()>=philims[isub].first && it->phi()<=philims[isub].second ) {
419  remove_track = false;
420  break;
421  }
422  }
423  else {
424  if ( it->phi()>=philims[isub].first || it->phi()<=philims[isub].second ) {
425  remove_track = false;
426  break;
427  }
428  }
429  }
430  if ( remove_track ) { inc=false; tracks.erase( it ); }
431  }
432  if ( inc ) ++it;
433  }
434 
435  }
436 
437  }
438 
439 
440  }
441 
442 
443  if ( verbose ) std::cout << "writing event:\n" << *track_ev << std::endl;
444 
445  tree->Fill();
446  ev_out++;
447 
448 
449 #if 0
450  for (unsigned int ic=0 ; ic<chains.size() ; ic++ ) {
451  if ( chains[ic].name()=="Offline" ) {
452  const std::vector<TIDA::Track>& tracks = chains[ic].rois()[0].tracks();
453 
454  for ( unsigned it=0 ; it<tracks.size() ; it++ ) {
455  h->Fill( tracks[it].pT()*0.001 );
456  itracks++;
457 
458  }
459  break;
460  }
461  }
462 #endif
463 
464  }
465 
466  }
467 
468 
469  double t = simpletimer_stop(tm);
470 
471  std::printf( "\r%c %6.2lf %% time: %6.2lf s\n",
472  cck[ii%4], 100., t*0.001 );
473 
474 
475  finput.Close();
476 
477  }
478 
479  std::cout << "skim::done " << time_str() << std::endl;
480 
481  std::cout << "skim::events in: " << ev_in << std::endl;
482  std::cout << "skim::events out: " << ev_out << std::endl;
483 
484  fout.Write();
485  fout.Close();
486 
487  return 0;
488 }
489 
490 
491 
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
python.SystemOfUnits.second
int second
Definition: SystemOfUnits.py:120
CalculateHighPtTerm.pT
pT
Definition: ICHEP2016/CalculateHighPtTerm.py:57
data
char data[hepevt_bytes_allocation_ATLAS]
Definition: HepEvt.cxx:11
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
usage
int usage(int e=0)
Definition: skim.cxx:46
run.infile
string infile
Definition: run.py:13
simpletimer_start
struct timeval simpletimer_start(void)
Definition: DataQuality/HanConfigGenerator/src/simpletimer.h:23
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
calibdata.force
bool force
Definition: calibdata.py:19
TIDAEvent.h
Basic event class to contain a vector of chains for trigger analysis
TIDA::Event::chainnames
std::vector< std::string > chainnames() const
Definition: TIDAEvent.cxx:28
TrigJetMonitorAlgorithm.ptmin
ptmin
Definition: TrigJetMonitorAlgorithm.py:1139
TIDA::Event::size
unsigned size() const
vertex multiplicity ?
Definition: TIDAEvent.h:64
tree
TChain * tree
Definition: tile_monitor.h:30
JiveXML::Event
struct Event_t Event
Definition: ONCRPCServer.h:65
get_generator_info.stdout
stdout
Definition: get_generator_info.py:40
skel.it
it
Definition: skel.GENtoEVGEN.py:401
TIDARoiDescriptor
Describes the Region of Ineterest geometry It has basically 8 parameters.
Definition: TIDARoiDescriptor.h:42
copyReleaseInfo
void copyReleaseInfo(TFile *finput, TFile *foutdir)
copy the TTree of release info from one directory to another
Definition: skim.cxx:82
offline
vec
std::vector< size_t > vec
Definition: CombinationsGeneratorTest.cxx:9
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
TIDA::Event
Definition: TIDAEvent.h:33
TIDARoiDescriptor::size
size_t size() const
Definition: TIDARoiDescriptor.h:176
python.Utilities.clone
clone
Definition: Utilities.py:134
TIDARoiDescriptor::phiPlus
double phiPlus() const
Definition: TIDARoiDescriptor.h:141
TIDA::Event::chains
const std::vector< TIDA::Chain > & chains() const
Definition: TIDAEvent.h:76
PrepareReferenceFile.regex
regex
Definition: PrepareReferenceFile.py:43
utils.h
TIDARoiDescriptor::composite
bool composite() const
composite RoI methods
Definition: TIDARoiDescriptor.h:174
remove_duplicates
void remove_duplicates(std::vector< T > &vec)
Definition: skim.cxx:34
lumiFormat.i
int i
Definition: lumiFormat.py:85
LArCellNtuple.argv
argv
Definition: LArCellNtuple.py:152
dqt_zlumi_alleff_HIST.fout
fout
Definition: dqt_zlumi_alleff_HIST.py:59
checkxAOD.frac
frac
Definition: Tools/PyUtils/bin/checkxAOD.py:259
simpletimer.h
DQHistogramMergeRegExp.argc
argc
Definition: DQHistogramMergeRegExp.py:20
calibdata.exit
exit
Definition: calibdata.py:236
ReadFromCoolCompare.os
os
Definition: ReadFromCoolCompare.py:231
CxxUtils::atof
double atof(std::string_view str)
Converts a string into a double / float.
Definition: Control/CxxUtils/Root/StringUtils.cxx:91
grepfile.ic
int ic
Definition: grepfile.py:33
create_dcsc_inputs_sqlite.arg
list arg
Definition: create_dcsc_inputs_sqlite.py:48
simpletimer_stop
double simpletimer_stop(const struct timeval &start_time)
Definition: DataQuality/HanConfigGenerator/src/simpletimer.h:29
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
TIDA::Chain
Definition: TIDAChain.h:28
python.ElectronD3PDObject.matched
matched
Definition: ElectronD3PDObject.py:138
main
int main(int argc, char **argv)
Definition: skim.cxx:101
TIDA::Event::clear
void clear()
clear the event
Definition: TIDAEvent.h:86
python.copyTCTOutput.chains
chains
Definition: copyTCTOutput.py:81
ir
int ir
counter of the current depth
Definition: fastadd.cxx:49
python.PyAthena.v
v
Definition: PyAthena.py:154
h
CaloSwCorrections.time
def time(flags, cells_name, *args, **kw)
Definition: CaloSwCorrections.py:242
python.TriggerHandler.verbose
verbose
Definition: TriggerHandler.py:297
DeMoScan.first
bool first
Definition: DeMoScan.py:536
time_str
std::string time_str()
Definition: skim.cxx:39
TIDA::Event::erase
void erase(const std::string &name)
Definition: TIDAEvent.cxx:36
entries
double entries
Definition: listroot.cxx:49
operator<<
std::ostream & operator<<(std::ostream &os, const std::set< T > &s)
Definition: skim.cxx:62
skip
bool skip
Definition: TrigGlobEffCorrValidation.cxx:190
PrepareReferenceFile.outfile
outfile
Definition: PrepareReferenceFile.py:42
TIDARoiDescriptor::phiMinus
double phiMinus() const
Definition: TIDARoiDescriptor.h:140