All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FileListSource.cxx
Go to the documentation of this file.
2 
4 
5 #include "TFile.h"
6 
7 #include <cassert>
8 #include <iostream>
9 
10 namespace ana
11 {
12  bool FileListSource::fgGotTickets = false;
13 
14  //----------------------------------------------------------------------
15  FileListSource::FileListSource(const std::vector<std::string>& files,
16  int stride, int offset)
17  : fFileNames(files.begin(), files.end()),
18  fIt(fFileNames.begin()),
19  fStride(stride),
20  fFile(0)
21  {
22  if(fFileNames.empty()){
23  fN = 0;
24  return;
25  }
26 
27  if(offset < 0){
28  if(getenv("CAFANA_OFFSET"))
29  offset = atoi(getenv("CAFANA_OFFSET"));
30 
31  offset = std::max(offset, 0);
32  }
33 
34  if(fStride < 0){
35  if(getenv("CAFANA_STRIDE"))
36  fStride = atoi(getenv("CAFANA_STRIDE"));
37 
38  fStride = std::max(fStride, 1);
39  }
40 
41  if(fStride > int(files.size())){
42  std::cerr << "Warning: stride " << fStride
43  << " is greater than the number of files: " << files.size()
44  << ". This is strange and inefficient." << std::endl;
45  // Needs to be nonzero otherwise some callers go off into SAM-query
46  // land. Having a slightly misleading progress bar is a small price to
47  // pay in this weird case.
48  fN = 1;
49  return;
50  }
51 
52  // How many files will we process from the list with this offset and
53  // stride?
54  fN = (int(files.size())-offset-1)/fStride+1;
55 
56  for(const std::string& loc: files){
57  if(loc.rfind("/pnfs/", 0) == 0){ // ie begins with
58  if(!fgGotTickets){
59  // No kerberos ticket means no point trying to voms-proxy-init. It
60  // likely also means we're in a grid job, where that would be
61  // counterproductive anyway.
62  if(system("klist -5 -s || klist -s") != 0) fgGotTickets = true;
63  }
64 
65  if(!fgGotTickets){
66  // This comes from NovaGridUtils, v02.10 onwards.
67  // system("setup_fnal_security -b");
68 
69  fgGotTickets = true;
70  break;
71  }
72  }
73  }
74 
75  for(int i = 0; i < offset; ++i){
76  ++fIt;
77  if(fIt == fFileNames.end()) break;
78  }
79  }
80 
81  //----------------------------------------------------------------------
83  {
84  delete fFile;
85  }
86 
87  //----------------------------------------------------------------------
89  {
90  // Tidy up the last file we gave, which the caller no longer needs
91  delete fFile;
92  fFile = 0;
93 
94  if(fIt == fFileNames.end()) return 0; // Ran out of files
95 
96  // If the file is on pnfs rewrite it to an xrootd address
97  std::string loc = *fIt;
98  // loc = pnfs2xrootd(loc); // no-op for non /pnfs locations
99 
100  fFile = TFile::Open(loc.c_str()); // This pattern allows xrootd
101  assert(fFile);
102 
103  for(int i = 0; i < fStride; ++i){
104  if(fIt == fFileNames.end()) break;
105  ++fIt; // Move on to the next file, for the subsequent call
106  }
107 
108  return fFile;
109  }
110 }
BEGIN_PROLOG TPC Trig offset(g4 rise time) ProjectToHeight
Definition: CORSIKAGen.fcl:7
std::vector< std::string >::iterator fIt
Iterator into fFileNames.
BEGIN_PROLOG could also be cerr
process_name opflashCryoW ana
int fN
Number of files that will actually be returned.
std::vector< std::string > fFileNames
The list of files.
auto end(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:585
auto begin(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:573
FileListSource(const std::vector< std::string > &files, int stride=-1, int offset=-1)
default offset and stride mean obey cmd-line options
static bool fgGotTickets
Have we renewed our tickets?
virtual TFile * GetNextFile() override
Returns the next file in sequence, ready for reading.
TFile * fFile
The most-recently-returned file.