All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SAMProjectSource.cxx
Go to the documentation of this file.
2 
4 
5 #include "ifdh.h"
6 
7 #include <unistd.h>
8 
9 #ifdef DARWINBUILD
10 #include <libgen.h>
11 #endif
12 
13 #include "TFile.h"
14 
15 namespace ana
16 {
17  bool SAMProjectSource::fgGotTickets = false;
18 
19  //----------------------------------------------------------------------
20  SAMProjectSource::SAMProjectSource(const std::string& proj, int fileLimit)
21  : fIFDH(new ifdh_ns::ifdh)
22  , fFile(0)
23  , fNFiles(fileLimit)
24  {
25  if(!fgGotTickets){
26  // No kerberos ticket means no point trying to voms-proxy-init. It likely
27  // also means we're in a grid job, where that would be counterproductive
28  // anyway.
29  if(system("klist -5 -s || klist -s") != 0) fgGotTickets = true;
30  }
31 
32  if(!fgGotTickets){
33  // This comes from NovaGridUtils, v02.10 onwards.
34  system("setup_fnal_security -b");
35 
36  fgGotTickets = true;
37  }
38 
39  // If X509_USER_PROXY isn't set, set it manually. Can help unconfuse IFDH
40  // about what x509 it's supposed to be using.
41  if(getenv("X509_USER_PROXY") == 0)
42  setenv("X509_USER_PROXY", TString::Format("/tmp/x509up_u%d", getuid()).Data(), 0);
43 
44  fIFDH->set_debug("0"); // shut up
45 
46  fProjectURL = fIFDH->findProject(proj, SAMExperiment());
47 
48  fProcessID = fIFDH->establishProcess(fProjectURL, "CAFAna", "v0.9", getenv("HOSTNAME"), getenv("USER"), SAMExperiment(), "", fileLimit);
49  }
50 
51  //----------------------------------------------------------------------
53  {
54  if(fFile){
55  // Tidy up the final file
56  const std::string fname = fFile->GetName();
57  delete fFile;
58  unlink(fname.c_str());
59  }
60 
61  // End the process cleanly
62  fIFDH->endProcess(fProjectURL, fProcessID);
63 
64  // certainly wrong for fileLimit case
65  // status = fIFDH.endProject(fProjectURL);
66 
67  fIFDH->cleanup();
68  }
69 
70  //----------------------------------------------------------------------
72  {
73  if(fFile){
74  // Tidy up the previous file
75  const std::string fname = fFile->GetName();
76  delete fFile;
77  fFile = 0;
78  unlink(fname.c_str());
79 
80  // And let SAM know we're done with it
81  fIFDH->updateFileStatus(fProjectURL, fProcessID, fname, "consumed");
82  }
83 
84  std::string tmp;
85 
86  tmp = fIFDH->getNextFile(fProjectURL, fProcessID);
87  const std::string uri(tmp);
88  if(uri.empty()) return 0; // out of files
89 
90  tmp = fIFDH->fetchInput(uri);
91  const std::string fname(tmp);
92  assert(!fname.empty());
93 
94  // Let SAM know we got it OK
95  fIFDH->updateFileStatus(fProjectURL, fProcessID, fname, "transferred");
96 
97  // Additional newlines because ifdh currently spams us with certificate
98  // messages.
99  if(fNFiles < 0) std::cout << std::endl << "Processing " << basename((char *)fname.c_str()) << std::endl << std::endl;
100 
101  fFile = new TFile(fname.c_str());
102  return fFile;
103  }
104 } // namespace
105 
string fname
Definition: demo.py:5
ifdh_ns::ifdh * fIFDH
SAMProjectSource(const std::string &proj, int fileLimit=-1)
process_name opflashCryoW ana
std::string SAMExperiment()
$SAM_EXPERIMENT or a nice error message and abort
virtual TFile * GetNextFile() override
Returns the next file in sequence, ready for reading.
TFile * fFile
The most-recently-returned file.
BEGIN_PROLOG could also be cout