Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FROG.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FROG.cc
1 #include "FROG.h"
2 
3 #include <phool/phool.h>
4 
5 #include <odbc++/connection.h>
6 #include <odbc++/drivermanager.h>
7 #include <odbc++/resultset.h>
8 #include <odbc++/statement.h> // for Statement
9 #include <odbc++/types.h> // for SQLException
10 
11 #include <boost/tokenizer.hpp>
12 
13 #include <chrono>
14 #include <cstdlib>
15 #include <fstream>
16 #include <iostream>
17 #include <string>
18 #include <thread>
19 
20 const char *
21 FROG::location(const std::string &logical_name)
22 {
23  pfn = logical_name;
24  if (logical_name.empty() || logical_name.find('/') != std::string::npos)
25  {
26  if (Verbosity() > 0)
27  {
28  if (logical_name.empty())
29  {
30  std::cout << "FROG: empty string as filename" << std::endl;
31  }
32  else if (logical_name.find('/') != std::string::npos)
33  {
34  std::cout << "FROG: found / in filename, assuming it contains a full path" << std::endl;
35  }
36  }
37  return pfn.c_str();
38  }
39  try
40  {
41  char *gsearchpath_env = getenv("GSEARCHPATH");
42  if (gsearchpath_env == nullptr)
43  {
44  return pfn.c_str();
45  }
46  std::string gsearchpath(gsearchpath_env);
47  if (Verbosity() > 0)
48  {
49  std::cout << "FROG: GSEARCHPATH: " << gsearchpath << std::endl;
50  }
51  boost::char_separator<char> sep(":");
52  boost::tokenizer<boost::char_separator<char> > tok(gsearchpath, sep);
53  for (auto &iter : tok)
54  {
55  if (iter == "PG")
56  {
57  if (Verbosity() > 1)
58  {
59  std::cout << "Searching FileCatalog for disk resident file "
60  << logical_name << std::endl;
61  }
62  if (PGSearch(logical_name))
63  {
64  if (Verbosity() > 1)
65  {
66  std::cout << "Found " << logical_name << " in FileCatalog, returning "
67  << pfn << std::endl;
68  }
69  break;
70  }
71  }
72  else if (iter == "DCACHE")
73  {
74  if (Verbosity() > 1)
75  {
76  std::cout << "Searching FileCatalog for dCache file "
77  << logical_name << std::endl;
78  }
79  if (dCacheSearch(logical_name))
80  {
81  if (Verbosity() > 1)
82  {
83  std::cout << "Found " << logical_name << " in dCache, returning "
84  << pfn << std::endl;
85  }
86  break;
87  }
88  }
89  else if (iter == "XROOTD")
90  {
91  if (Verbosity() > 1)
92  {
93  std::cout << "Searching FileCatalog for XRootD file "
94  << logical_name << std::endl;
95  }
96  if (XRootDSearch(logical_name))
97  {
98  if (Verbosity() > 1)
99  {
100  std::cout << "Found " << logical_name << " in XRootD, returning "
101  << pfn << std::endl;
102  }
103  break;
104  }
105  }
106  else if (iter == "LUSTRE")
107  {
108  if (Verbosity() > 1)
109  {
110  std::cout << "Searching FileCatalog for Lustre file "
111  << logical_name << std::endl;
112  }
113  if (LustreSearch(logical_name))
114  {
115  if (Verbosity() > 1)
116  {
117  std::cout << "Found " << logical_name << " in Lustre, returning "
118  << pfn << std::endl;
119  }
120  break;
121  }
122  }
123  else if (iter == "MINIO")
124  {
125  if (Verbosity() > 1)
126  {
127  std::cout << "Searching FileCatalog for Lustre file via MinIO "
128  << logical_name << std::endl;
129  }
130  if (MinIOSearch(logical_name))
131  {
132  if (Verbosity() > 1)
133  {
134  std::cout << "Found " << logical_name << " in Lustre, returning MinIO URL "
135  << pfn << std::endl;
136  }
137  break;
138  }
139  }
140  else // assuming this is a file path
141  {
142  if (Verbosity() > 0)
143  {
144  std::cout << "Trying path " << iter << std::endl;
145  }
146  std::string fullfile = iter + "/" + logical_name;
147  if (localSearch(fullfile))
148  {
149  break;
150  }
151  }
152  }
153  }
154  catch (...)
155  {
156  if (Verbosity() > 0)
157  {
158  std::cout << "FROG: GSEARCHPATH not set " << std::endl;
159  }
160  }
161  Disconnect();
162  return pfn.c_str();
163 }
164 
165 bool FROG::localSearch(const std::string &logical_name)
166 {
167  if (std::ifstream(logical_name))
168  {
169  pfn = logical_name;
170  return true;
171  }
172  return false;
173 }
174 
176 {
177  if (m_OdbcConnection)
178  {
179  return true;
180  }
181  int icount = 0;
182  do
183  {
184  try
185  {
186  m_OdbcConnection = odbc::DriverManager::getConnection("FileCatalog", "argouser", "Brass_Ring");
187  return true;
188  }
189  catch (odbc::SQLException &e)
190  {
191  std::cout << PHWHERE
192  << " Exception caught during DriverManager::getConnection" << std::endl;
193  std::cout << "Message: " << e.getMessage() << std::endl;
194  }
195  icount++;
196  std::this_thread::sleep_for(std::chrono::seconds(30)); // sleep 30 seconds before retry
197  } while (icount < 5);
198  return false;
199 }
200 
202 {
203  delete m_OdbcConnection;
204  m_OdbcConnection = nullptr;
205 }
206 
207 bool FROG::PGSearch(const std::string &lname)
208 {
209  bool bret = false;
210  if (!GetConnection())
211  {
212  return bret;
213  }
214  std::string sqlquery = "SELECT full_file_path from files where lfn='" + lname + "' and full_host_name <> 'hpss' and full_host_name <> 'dcache' and full_host_name <> 'lustre'";
215 
216  if (Verbosity() > 1)
217  {
218  std::cout << "sql query:" << std::endl
219  << sqlquery << std::endl;
220  }
222  odbc::ResultSet *rs = stmt->executeQuery(sqlquery);
223 
224  if (rs->next())
225  {
226  pfn = rs->getString(1);
227  bret = true;
228  }
229  delete rs;
230  delete stmt;
231  return bret;
232 }
233 
234 bool FROG::dCacheSearch(const std::string &lname)
235 {
236  bool bret = false;
237  if (!GetConnection())
238  {
239  return bret;
240  }
241  std::string sqlquery = "SELECT full_file_path from files where lfn='" + lname + "' and full_host_name = 'dcache'";
242 
243  if (Verbosity() > 1)
244  {
245  std::cout << "sql query:" << std::endl
246  << sqlquery << std::endl;
247  }
249  odbc::ResultSet *rs = stmt->executeQuery(sqlquery);
250 
251  if (rs->next())
252  {
253  std::string dcachefile = rs->getString(1);
254  if (std::ifstream(dcachefile))
255  {
256  pfn = "dcache:" + dcachefile;
257  bret = true;
258  }
259  }
260  delete rs;
261  delete stmt;
262  return bret;
263 }
264 
265 bool FROG::XRootDSearch(const std::string &lname)
266 {
267  bool bret = false;
268  if (!GetConnection())
269  {
270  return bret;
271  }
272  std::string sqlquery = "SELECT full_file_path from files where lfn='" + lname + "' and full_host_name = 'lustre'";
273  if (Verbosity() > 1)
274  {
275  std::cout << "sql query:" << std::endl
276  << sqlquery << std::endl;
277  }
279  odbc::ResultSet *rs = stmt->executeQuery(sqlquery);
280 
281  if (rs->next())
282  {
283  std::string xrootdfile = rs->getString(1);
284  pfn = "root://xrdsphenix.rcf.bnl.gov/" + xrootdfile;
285  bret = true;
286  }
287  delete rs;
288  delete stmt;
289  return bret;
290 }
291 
292 bool FROG::LustreSearch(const std::string &lname)
293 {
294  bool bret = false;
295  if (!GetConnection())
296  {
297  return bret;
298  }
299  std::string sqlquery = "SELECT full_file_path from files where lfn='" + lname + "' and full_host_name = 'lustre'";
300 
302  odbc::ResultSet *rs = stmt->executeQuery(sqlquery);
303 
304  if (rs->next())
305  {
306  pfn = rs->getString(1);
307  bret = true;
308  }
309  delete rs;
310  delete stmt;
311  return bret;
312 }
313 
314 bool FROG::MinIOSearch(const std::string &lname)
315 {
316  bool bret = false;
317  if (!GetConnection())
318  {
319  return bret;
320  }
321  std::string sqlquery = "SELECT full_file_path from files where lfn='" + lname + "' and full_host_name = 'lustre'";
322 
323  if (Verbosity() > 1)
324  {
325  std::cout << "sql query:" << std::endl
326  << sqlquery << std::endl;
327  }
329  odbc::ResultSet *rs = stmt->executeQuery(sqlquery);
330 
331  if (rs->next())
332  {
333  pfn = rs->getString(1);
334  std::string toreplace("/sphenix/lustre01/sphnxpro");
335  size_t strpos = pfn.find(toreplace);
336  if (strpos == std::string::npos)
337  {
338  std::cout << " could not locate " << toreplace
339  << " in full file path " << pfn << std::endl;
340  exit(1);
341  }
342  else if (strpos > 0)
343  {
344  std::cout << "full file path " << pfn
345  << "does not start with " << toreplace << std::endl;
346  exit(1);
347 
348  }
349  pfn.replace(pfn.begin(),pfn.begin()+toreplace.size(),"s3://sphenixs3.rcf.bnl.gov:9000");
350  bret = true;
351  }
352  delete rs;
353  delete stmt;
354  return bret;
355 }