SAMProjectSource.cxx
Go to the documentation of this file.
2 
3 #include "ifdh.h"
4 
5 #include <cassert>
6 #include <iostream>
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  //----------------------------------------------------------------------
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, getenv("SAM_STATION"));
47 
48  // grid jobs don't always have $USER set, but often they have $GRID_USER instead
49  char * strPtr;
50  std::string userStr;
51  if ( (strPtr = getenv("USER")) || (strPtr = getenv("GRID_USER")) )
52  userStr = strPtr;
53  else
54  userStr = "unknown";
55 
56  // grid jobs don't always have $HOSTNAME set, sometimes they use $OSG_HOSTNAME instead
57  std::string hostNameStr;
58  strPtr = getenv("HOSTNAME");
59  if(!strPtr) strPtr = getenv("OSG_HOSTNAME");
60  hostNameStr = strPtr;
61 
62  fProcessID = fIFDH->establishProcess(fProjectURL, "CAFAna", "v0.9", hostNameStr.c_str(), userStr.c_str(), "nova", "", fileLimit);
63  }
64 
65  //----------------------------------------------------------------------
67  {
68  if(fFile){
69  // Tidy up the final file
70  const std::string fname = fFile->GetName();
71  delete fFile;
72  unlink(fname.c_str());
73  }
74 
75  // End the process cleanly
76  fIFDH->endProcess(fProjectURL, fProcessID);
77 
78  // certainly wrong for fileLimit case
79  // status = fIFDH.endProject(fProjectURL);
80 
81  fIFDH->cleanup();
82  }
83 
84  //----------------------------------------------------------------------
86  {
87  if(fFile){
88  // Tidy up the previous file
89  const std::string fname = fFile->GetName();
90  delete fFile;
91  fFile = 0;
92  unlink(fname.c_str());
93 
94  // And let SAM know we're done with it
95  fIFDH->updateFileStatus(fProjectURL, fProcessID, fname, "consumed");
96  }
97 
98  const std::string uri = fIFDH->getNextFile(fProjectURL, fProcessID);
99  if(uri.empty()) return 0; // out of files
100 
101  const std::string fname = fIFDH->fetchInput(uri);
102  assert(!fname.empty());
103 
104  // Let SAM know we got it OK
105  fIFDH->updateFileStatus(fProjectURL, fProcessID, fname, "transferred");
106 
107  // Additional newlines because ifdh currently spams us with certificate
108  // messages.
109  if(fNFiles < 0) std::cout << std::endl << "Processing " << basename((char *)fname.c_str()) << std::endl << std::endl;
110 
111  fFile = new TFile(fname.c_str());
112  return fFile;
113  }
114 } // namespace
115 
Cuts and Vars for the 2020 FD DiF Study.
Definition: vars.h:6
system("rm -rf microbeam.root")
IFDH interface (data handling)
ifdh_ns::ifdh * fIFDH
SAMProjectSource(const std::string &proj, int fileLimit=-1)
std::string getenv(std::string const &name)
ifdh
Definition: ProjMan.py:8
fileLimit
Definition: ProjMan.py:109
OStream cout
Definition: OStream.cxx:6
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
Float_t proj
Definition: plot.C:35
virtual TFile * GetNextFile() override
Returns the next file in sequence, ready for reading.
assert(nhit_max >=nhit_nbins)
void Format(TGraph *gr, int lcol, int lsty, int lwid, int mcol, int msty, double msiz)
Definition: Style.cxx:154
TFile * fFile
The most-recently-returned file.
const XML_Char const XML_Char * uri
Definition: expat.h:380