ATLAS Offline Software
skim.cxx
Go to the documentation of this file.
1 
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>& _s ) {
73  typename std::vector<T>::const_iterator sitr = _s.begin();
74  s << "[ ";
75  while ( sitr!=_s.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 adding_chains = false;
116  bool deleting_chains = false;
117 
118  bool force = false;
119 
120  double ptmin = 0;
121 
122  bool roi_filter = false;
123 
124  bool verbose = false;
125 
126  for ( int i=1 ; i<argc ; i++ ) {
127 
128  std::string arg(argv[i]);
129 
130  if ( arg.find('-')!=0 ) {
131  if ( adding_chains || deleting_chains ) {
132  require_chains.insert(argv[i]);
133  rchains.push_back(argv[i]);
134  continue;
135  }
136  }
137  else {
138  adding_chains = false;
139  deleting_chains = false;
140  }
141 
142  if ( arg=="-h" || arg=="--help" ) return usage(0);
143  else if ( arg=="-o" || arg=="--output" ) {
144  if ( (i+1)<argc ) outfile = argv[++i];
145  else return usage(-1);
146  }
147  else if ( arg=="-r" || arg=="--require" ) {
148  if ( deleting ) {
149  std::cerr << "cannot require and delete chains" << std::endl;
150  return usage(-4);
151  }
152  adding_chains = true;
153  require = true;
154  }
155  else if ( arg=="-d" || arg=="--delete" ) {
156  if ( require ) {
157  std::cerr << "cannot require and delete chains" << std::endl;
158  return usage(-3);
159  }
160  deleting_chains = true;
161  deleting = true;
162  }
163  else if ( arg=="--pt" ) {
164  if ( (i+1)<argc ) ptmin = std::atof(argv[++i])*1000;
165  else return usage(-1);
166  force = true;
167  }
168  else if ( arg=="-v" || arg=="--verbose" ) verbose = true;
169  else if ( arg=="-f" || arg=="--force" ) force = true;
170  else if ( arg=="--roi" ) { force=true; roi_filter = true; }
171  else if ( infile=="" ) infile = arg;
172  else {
173  std::cerr << "more than one file specified: " << arg << std::endl;
174  return usage(-2);
175  }
176  }
177 
178  // std::cout << "required chains " << require_chains << std::endl;
179 
180  if ( !force && require_chains.size()==0 ) {
181  std::cout << "no chains requested - not doing anything" << std::endl;
182  return 0;
183  }
184 
185 
186  std::cout << "skim::start " << time_str() << std::endl;
187 
188  // if ( require_chains.size()>0 ) require = true;
189 
190  if ( require ) std::cout << "require chains " << require_chains << std::endl;
191  if ( deleting ) std::cout << "delete chains " << require_chains << std::endl;
192 
193 
194  if ( infile=="" ) {
195  std::cerr << "no files specified" << std::endl;
196  return usage(-1);
197  }
198 
199  std::cout << "reading from file " << infile << std::endl;
200  std::cout << "writing to file " << outfile << std::endl;
201 
202 
204  TIDA::Event* track_ev = new TIDA::Event();
205  TIDA::Event* h = track_ev;
206 
207  std::cout << "opening outfile: " << outfile << std::endl;
208 
209  TFile fout( outfile.c_str(), "recreate");
210 
211  fout.cd();
212 
214 
215  TTree *tree = new TTree("tree","tree");
216 
217  // tree->Branch("Track","Int",&t,6400);
218  tree->Branch("TIDA::Event", "TIDA::Event",&h,6400, 1);
219 
220  h->clear();
221 
223 
224  int ev_in = 0;
225  int ev_out = 0;
226 
227  {
228 
230  TFile finput( infile.c_str() );
231  if (!finput.IsOpen()) {
232  std::cerr << "Error: could not open output file" << std::endl;
233  exit(-1);
234  }
235 
236 
237  copyReleaseInfo( &finput, &fout );
238 
239  finput.cd();
240 
242 
243  // TChain* data = new TChain("tree");
244 
245  TTree* data = (TTree*)finput.Get("tree");
246 
247  TIDA::Event* track_iev = new TIDA::Event();
248 
249  data->SetBranchAddress("TIDA::Event",&track_iev);
250  // data->AddFile( argv[i] );
251 
252 
253  unsigned entries = data->GetEntries();
254 
255  std::cout << "input has " << entries << " events" << std::endl;
256 
257  std::string cck = "|/-\\";
258 
259  int ii=0;
260 
261  struct timeval tm = simpletimer_start();
262 
263  std::cout << "processing ..." << std::endl;
264 
265  for (unsigned int i=0; i<data->GetEntries() ; i++ ) {
266 
267  if ( verbose ) std::cout << "event " << i;
268 
269  if ( i%100==0 ) {
270 
271  double frac = i*1.0/entries;
272 
273  double t = simpletimer_stop(tm);
274 
275  double est = 0;
276 
277  if ( frac > 0 ) est = t/frac - t;
278 
279  double eventsps = 1000*i/t;
280 
281  std::printf( "\r%c %6.2lf %% time: %6.2lf s remaining %6.2lf s (%u at %5.2lf ps) ",
282  cck[ii%4], ((1000*(i+1)/entries)*0.1), t*0.001, est*0.001, i, eventsps );
283 
284  std::fflush(stdout);
285 
286  ii++;
287  }
288 
289 
290 
291  track_iev->clear();
292  track_ev->clear();
293 
294  data->GetEntry(i);
295 
296  ev_in++;
297 
298  *track_ev = *track_iev;
299 
300  // std::cout << *track_ev << std::endl;
301 
302  // std::cout << "track_ev names " << track_ev->chainnames() << std::endl;
303  // std::cout << "track_iev names " << track_iev->chainnames() << std::endl;
304 
305  if ( verbose ) std::cout << "----------------------------------------\n\tN chains " << track_ev->size() << " -> ";
306 
307  std::vector<TIDA::Chain>& chains = track_ev->chains();
308 
309 
311 
312  bool skip = true;
313 
315  for ( ; citr!=chains.end() ; ++citr ) {
316 
317  if ( citr->name().find("HLT")==std::string::npos ) continue;
318 
319  if ( require ) {
320  for ( unsigned j=rchains.size() ; j-- ; ) {
321  if ( rchains[j].find("HLT")==std::string::npos ) continue;
322  if ( citr->name().find(rchains[j])!=std::string::npos ) {
323  skip = false;
324  if ( verbose ) std::cout << "keepin' " << citr->name() << " " << rchains[j] << std::endl;
325  }
326 
327  }
328  }
329 
330  }
331 
332  if ( skip ) continue;
333 
334 
336 
337  {
338 
340 
341  std::vector<std::string> chainnames = track_ev->chainnames();
342 
343  for ( size_t ic=0 ; ic<chainnames.size() ; ic++ ) {
344 
345  bool matched = false;
346  for ( std::set<std::string>::iterator it=require_chains.begin() ; it!=require_chains.end() ; ++it ) {
347 
348  matched |= std::regex_match( chainnames[ic], std::regex(*it+".*") );
349 
350  if ( verbose && matched ) std::cout << "chain: " << chainnames[ic] << "\t :: reg " << *it << "\tmatched: " << matched << std::endl;
351 
352  }
353 
354  if ( ( require && !matched ) ) track_ev->erase( chainnames[ic] );
355  }
356 
357  if ( verbose ) std::cout << track_ev->size() << std::endl;
358 
359 
360  if ( roi_filter || ptmin>0 ) {
361 
362  TIDA::Chain* offline = 0;
363 
364  std::vector<std::string> chainnames = track_ev->chainnames();
365 
367 
368  for ( size_t ic=chainnames.size() ; ic-- ; ) {
369  if ( chainnames[ic] == "Offline" ) {
370  offline = &(track_ev->chains()[ic]);
371  break;
372  }
373  }
374 
375  if ( offline ) {
376  // track_ev->addChain( "Offline" );
377 
378  std::vector<TIDA::Chain>& chains = track_ev->chains();
380 
381  std::vector<std::pair<double,double> > philims;
382 
383  for ( ; citr!=chains.end() ; ++citr ) {
384  if ( citr->name().find("HLT_")!=std::string::npos ) {
385  for ( size_t ir=0 ; ir<citr->size() ; ir++ ) {
386  TIDARoiDescriptor& roi = citr->rois()[ir].roi();
387  if ( roi.composite() ) {
388  for ( size_t isub=0 ; isub<roi.size() ; isub++ ) {
389  philims.push_back( std::pair<double,double>( roi[isub]->phiMinus(), roi[isub]->phiPlus() ) );
390  }
391  }
392  else philims.push_back( std::pair<double,double>( roi.phiMinus(), roi.phiPlus() ) );
393  }
394  }
395  }
396 
397  remove_duplicates( philims );
398 
399  for ( size_t iroi=0 ; iroi<offline->size() ; iroi++ ) {
400 
401  std::vector<TIDA::Track>& tracks = offline->rois()[iroi].tracks();
402 
403  for ( std::vector<TIDA::Track>::iterator it=tracks.begin() ; it<tracks.end() ; ) {
404  bool inc = true;
405  if ( ptmin>0 ) {
406  if ( std::fabs(it->pT())<ptmin ) { inc=false; tracks.erase( it ); }
407  }
408  if ( inc && roi_filter ) {
409  bool remove_track = true;
410  for ( size_t isub=0 ; isub<philims.size() ; isub++ ) {
411 
412  if ( philims[isub].first < philims[isub].second ) {
413  if ( it->phi()>=philims[isub].first && it->phi()<=philims[isub].second ) {
414  remove_track = false;
415  break;
416  }
417  }
418  else {
419  if ( it->phi()>=philims[isub].first || it->phi()<=philims[isub].second ) {
420  remove_track = false;
421  break;
422  }
423  }
424  }
425  if ( remove_track ) { inc=false; tracks.erase( it ); }
426  }
427  if ( inc ) ++it;
428  }
429 
430  }
431 
432  }
433 
434 
435  }
436 
437 
438  if ( verbose ) std::cout << *track_ev << std::endl;
439 
440  // std::cout << "writing event " << track_ev->event_number() << " <<<<<<<<<<<<<<<<<<<<" << std::endl;
441  tree->Fill();
442  ev_out++;
443 
444 
445 #if 0
446  for (unsigned int ic=0 ; ic<chains.size() ; ic++ ) {
447  if ( chains[ic].name()=="Offline" ) {
448  const std::vector<TIDA::Track>& tracks = chains[ic].rois()[0].tracks();
449 
450  for ( unsigned it=0 ; it<tracks.size() ; it++ ) {
451  h->Fill( tracks[it].pT()*0.001 );
452  itracks++;
453 
454  }
455  break;
456  }
457  }
458 #endif
459 
460  }
461 
462  }
463 
464 
465  double t = simpletimer_stop(tm);
466 
467  std::printf( "\r%c %6.2lf %% time: %6.2lf s\n",
468  cck[ii%4], 100., t*0.001 );
469 
470 
471  finput.Close();
472 
473  }
474 
475  std::cout << "skim::done " << time_str() << std::endl;
476 
477  std::cout << "skim::events in: " << ev_in << std::endl;
478  std::cout << "skim::events out: " << ev_out << std::endl;
479 
480  fout.Write();
481  fout.Close();
482 
483  return 0;
484 }
485 
486 
487 
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
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:27
TrigJetMonitorAlgorithm.ptmin
ptmin
Definition: TrigJetMonitorAlgorithm.py:1081
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:423
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:12
LArCellConditions.argv
argv
Definition: LArCellConditions.py:112
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:92
dqt_zlumi_alleff_HIST.fout
fout
Definition: dqt_zlumi_alleff_HIST.py:59
checkxAOD.frac
frac
Definition: Tools/PyUtils/bin/checkxAOD.py:256
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:192
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
DiTauMassTools::MaxHistStrategyV2::e
e
Definition: PhysicsAnalysis/TauID/DiTauMassTools/DiTauMassTools/HelperFunctions.h:26
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:534
time_str
std::string time_str()
Definition: skim.cxx:39
TIDA::Event::erase
void erase(const std::string &name)
Definition: TIDAEvent.cxx:35
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