| 1 |
// |
| 2 |
// C/C++ headers |
| 3 |
// |
| 4 |
#include <fstream> |
| 5 |
#include <sstream> |
| 6 |
#include <iostream> |
| 7 |
// |
| 8 |
// ROOT headers |
| 9 |
// |
| 10 |
#include <TSystem.h> |
| 11 |
#include <TString.h> |
| 12 |
#include <TSQLServer.h> |
| 13 |
// |
| 14 |
// This package headers |
| 15 |
// |
| 16 |
#include <RunInfo.h> |
| 17 |
#include <RunInfoVerl2.h> |
| 18 |
// |
| 19 |
using namespace std; |
| 20 |
// |
| 21 |
// Usage subroutine |
| 22 |
// |
| 23 |
void usage(){ |
| 24 |
printf("\nUsage:\n"); |
| 25 |
printf("\n RunInfo [-v] [-h] [--version] -idRun ID_RUN [-processFile filename] [-processFolder folder]\n"); |
| 26 |
printf("\n --version print informations about compilation and exit\n"); |
| 27 |
printf("\n -h | --help print this help and exit \n"); |
| 28 |
printf("\n -v | --verbose be verbose [default: print nothing on STDOUT]\n"); |
| 29 |
printf("\n -idRun ID_RUN: ID number of the run to be processed \n"); |
| 30 |
printf("\n -processFile filename : output filename [default ID_RUN.Level2.root]\n"); |
| 31 |
printf("\n -processFolder where to store temporary data [default \"runinfoFolder\"]\n"); |
| 32 |
printf("\n Notice that parameter order does not matter. \n"); |
| 33 |
printf("\nExample: \n\nRunInfo -idRun 1085 -processFile nomefile.root\n\n"); |
| 34 |
}; |
| 35 |
// |
| 36 |
Bool_t existfile(TString filename){ |
| 37 |
ifstream myfile; |
| 38 |
myfile.open(filename.Data()); |
| 39 |
if ( !myfile ){ |
| 40 |
return(false); |
| 41 |
}; |
| 42 |
myfile.close(); |
| 43 |
return(true); |
| 44 |
} |
| 45 |
// |
| 46 |
Bool_t debug = false; |
| 47 |
// |
| 48 |
// |
| 49 |
// Here the main |
| 50 |
// |
| 51 |
int main(int numinp, char *inps[]){ |
| 52 |
// |
| 53 |
// Variables booking |
| 54 |
// |
| 55 |
int nul = 0; |
| 56 |
Int_t error = 0; |
| 57 |
Bool_t beverbose = false; |
| 58 |
ULong64_t run = 0; |
| 59 |
TString filename; |
| 60 |
TString processFolder = "runinfoFolder"; |
| 61 |
TSQLServer *dbc = 0; |
| 62 |
Bool_t givenid = false; |
| 63 |
// |
| 64 |
// Checking input parameters |
| 65 |
// |
| 66 |
if ( numinp > 1 ){ |
| 67 |
for ( int i = 0; i < numinp; i++ ){ |
| 68 |
if ( !strcmp(inps[i],"--version") ){ |
| 69 |
RunInfoInfo(true); |
| 70 |
exit(0); |
| 71 |
}; |
| 72 |
if ( !strcmp(inps[i],"-h") || !strcmp(inps[i],"--help") ){ |
| 73 |
usage(); |
| 74 |
exit(0); |
| 75 |
}; |
| 76 |
if ( !strcmp(inps[i],"-idRun") ) { |
| 77 |
if ( numinp-1 < i+1 ){ |
| 78 |
usage(); |
| 79 |
exit(-3); |
| 80 |
}; |
| 81 |
givenid = true; |
| 82 |
char *pEnd; |
| 83 |
run = strtoull(inps[i+1],&pEnd,0); |
| 84 |
}; |
| 85 |
if ( !strcmp(inps[i],"-processFile") ) { |
| 86 |
if ( numinp-1 < i+1 ){ |
| 87 |
usage(); |
| 88 |
exit(-3); |
| 89 |
}; |
| 90 |
filename = (TString)inps[i+1]; |
| 91 |
}; |
| 92 |
if ( !strcmp(inps[i],"-processFolder") ) { |
| 93 |
if ( numinp-1 < i+1 ){ |
| 94 |
usage(); |
| 95 |
exit(-3); |
| 96 |
}; |
| 97 |
processFolder = (TString)inps[i+1]; |
| 98 |
}; |
| 99 |
if ( !strcmp(inps[i],"-v") || !strcmp(inps[i],"--verbose") ) beverbose = true; |
| 100 |
}; |
| 101 |
} else { |
| 102 |
// |
| 103 |
// no input parameters exit with error, we need at least the run id. |
| 104 |
// |
| 105 |
printf(" RunInfo - ERROR: you must provide a run number (at least -1)\n"); |
| 106 |
exit(-1); |
| 107 |
}; |
| 108 |
// |
| 109 |
// If not in verbose mode redirect to /dev/null the stdout and stderr |
| 110 |
// |
| 111 |
if ( !beverbose ){ |
| 112 |
nul = open("/dev/null", O_CREAT | O_RDWR,S_IREAD | S_IWRITE); |
| 113 |
dup2(nul,1); |
| 114 |
dup2(nul,2); |
| 115 |
}; |
| 116 |
// |
| 117 |
// Check that an input run number has been given |
| 118 |
// |
| 119 |
if ( !givenid ) { |
| 120 |
printf(" RunInfo - ERROR: you must provide a run number (at least -1)\n"); |
| 121 |
exit(-1); |
| 122 |
}; |
| 123 |
// |
| 124 |
char *version = RunInfoInfo(false); |
| 125 |
// |
| 126 |
// Start: |
| 127 |
// |
| 128 |
printf("\n Welcome to the RunInfo software, version %s \n",version); |
| 129 |
// |
| 130 |
// Connect to the DB |
| 131 |
// |
| 132 |
printf("\nConnecting to database... \n"); |
| 133 |
// |
| 134 |
dbc = TSQLServer::Connect("mysql://localhost/pamelaprod","anonymous",""); |
| 135 |
// |
| 136 |
if( !dbc ) { |
| 137 |
printf(" RunInfo - ERROR: problems connecting to the DB (check username and passwd), exiting...\n\n"); |
| 138 |
exit(-2); |
| 139 |
}; |
| 140 |
bool connect = dbc->IsConnected(); |
| 141 |
// |
| 142 |
if( !connect ) { |
| 143 |
printf(" RunInfo - ERROR: problems connecting to the DB (check username and passwd), exiting...\n\n"); |
| 144 |
exit(-2); |
| 145 |
}; |
| 146 |
printf("...connected! \n\n"); |
| 147 |
// |
| 148 |
// |
| 149 |
// |
| 150 |
stringstream name; |
| 151 |
name.str(""); |
| 152 |
if ( run != 0ULL ){ |
| 153 |
// |
| 154 |
// Run is not 0, we must process only one run . |
| 155 |
// |
| 156 |
// |
| 157 |
// If no filename is given, use the deafult one run_id.Level2.root. |
| 158 |
// |
| 159 |
if ( !strcmp(filename.Data(),"") ){ |
| 160 |
name << run << ".Level2.root"; |
| 161 |
} else { |
| 162 |
name << filename.Data(); |
| 163 |
}; |
| 164 |
// |
| 165 |
} else { |
| 166 |
// |
| 167 |
// Run is 0, we must process all entries in the given file. |
| 168 |
// |
| 169 |
// |
| 170 |
// OUCH! no filename has been given, run out! |
| 171 |
// |
| 172 |
if ( !strcmp(filename.Data(),"") ) { |
| 173 |
printf(" RunInfo - ERROR: processing all runs but no filename is given\n"); |
| 174 |
exit(-4); |
| 175 |
}; |
| 176 |
name << filename.Data(); |
| 177 |
// |
| 178 |
}; |
| 179 |
TString outputfile; |
| 180 |
ItoRunInfo *runinfo = 0; |
| 181 |
TFile *file = 0; |
| 182 |
// |
| 183 |
stringstream temprname; |
| 184 |
const char* routdir = gSystem->WorkingDirectory(); |
| 185 |
temprname.str(""); |
| 186 |
temprname << routdir; |
| 187 |
temprname << "/" << processFolder.Data(); |
| 188 |
// |
| 189 |
// Output file is "outputfile" |
| 190 |
// |
| 191 |
outputfile = name.str().c_str(); |
| 192 |
printf("\n Output filename is: \n %s \n\n",outputfile.Data()); |
| 193 |
// |
| 194 |
// Check if file exists, if not exit with error (calorimeter needs tracker data). |
| 195 |
// |
| 196 |
if ( !existfile(outputfile.Data()) ) { |
| 197 |
printf(" RunInfo: creating LEVEL2 file \n"); |
| 198 |
}; |
| 199 |
// |
| 200 |
// OK, file exists, open it in "update" mode. |
| 201 |
// |
| 202 |
file = new TFile(outputfile.Data(),"UPDATE"); |
| 203 |
if ( !file->IsOpen() ){ |
| 204 |
printf(" RunInfo - ERROR: cannot open file for writing\n"); |
| 205 |
error = -802; |
| 206 |
goto theend; |
| 207 |
}; |
| 208 |
// |
| 209 |
runinfo = new ItoRunInfo(dbc,file,processFolder); |
| 210 |
error = runinfo->Update(run,"NONE",""); |
| 211 |
gSystem->Unlink(temprname.str().c_str()); |
| 212 |
// if ( error < 0 ) goto theend; |
| 213 |
runinfo->Close(); |
| 214 |
if ( file ){ |
| 215 |
file->Write(); |
| 216 |
file->Close(); |
| 217 |
}; |
| 218 |
// |
| 219 |
theend: |
| 220 |
// |
| 221 |
// Close the DB connection |
| 222 |
// |
| 223 |
printf("\nClose the connection to the database... \n"); |
| 224 |
dbc->Close(); |
| 225 |
printf("...connection terminated!\n\n"); |
| 226 |
// |
| 227 |
// Close redirection if the case. |
| 228 |
// |
| 229 |
if ( !beverbose ) close(nul); |
| 230 |
// |
| 231 |
// Return "error" |
| 232 |
// |
| 233 |
exit(error); |
| 234 |
} |