Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

lootSpillFiles.cxx

Go to the documentation of this file.
00001 #include "lootSpillFiles.h"
00002 #include "TSystem.h"
00003 #include "TFile.h"
00004 #include "TTree.h"
00005 #include "TString.h"
00006 #include "Validity/VldTimeStamp.h"
00007 #include "SpillTiming/SpillTimeND.h"
00008 #include "DatabaseInterface/DbiWriter.h"
00009 #include "DatabaseInterface/DbiStatement.h"
00010 #include "DatabaseInterface/DbiCascader.h"
00011 #include <vector>
00012 #include <iostream>
00013 #include <fstream>
00014 
00015 using namespace std;
00016 
00017 void createSpillTables()
00018 {
00019   int  dbNo = 0; // or whatever cascade entry you need
00020   DbiStatement* s = DbiTableProxyRegistry::Instance()
00021     .GetCascader()
00022     .CreateStatement(dbNo);
00023 
00024   s->ExecuteUpdate("create table if not exists SPILLTIMENDVLD ("
00025                    " SEQNO         int not null primary key auto_increment,"
00026                    " TIMESTART     datetime not null,"
00027                    " TIMEEND       datetime not null,"
00028                    " DETECTORMASK  tinyint,"
00029                    " SIMMASK       tinyint,"
00030                    " TASK          int,"
00031                    " AGGREGATENO   int,"
00032                    " CREATIONDATE  datetime not null,"
00033                    " INSERTDATE    datetime not null )");
00034 
00035   s->ExecuteUpdate("create table if not exists SPILLTIMEND ("
00036                    " SEQNO integer,"
00037                    " SECOND integer,"
00038                    " NANOSEC integer, "
00039                    " index (SEQNO))");
00040 
00041   delete s;  //Drop statement to release connection.
00042   s = 0;
00043 }
00044 
00045 
00046 void lootSpillFiles(const char* path, const char* logfile)
00047 {
00048   // Loot the given directory for goblin files.
00049   void* dir = gSystem->OpenDirectory(path);
00050   while(1) {
00051     const char* entry = gSystem->GetDirEntry(dir);
00052     if(!entry) break;
00053     TString entrystr(entry);
00054     if((entrystr.Contains("spilltime") && entrystr.Contains(".dat"))) {
00055       cout << "Looking at " << entry << endl;
00056       char filename[1000];
00057       sprintf(filename,"%s/%s",path,entry);
00058       bool good = true;
00059       
00060       {
00061         ifstream lootfile(logfile);
00062         if(!lootfile.good()) cout << "Can't find logfile " << logfile << endl;
00063         char line[1000];
00064         while((!lootfile.eof())&&(lootfile.good())) {
00065           lootfile.getline(line,1000,'\n');
00066           if(strcmp(line,filename)==0) good=false;
00067         }
00068         lootfile.close();
00069       }      
00070 
00071       if(good) {
00072         lootSpillFile(filename);
00073 
00074         // Log that this file is processed.
00075         ofstream lootfile(logfile,ios_base::out|ios_base::ate|ios_base::app);
00076         lootfile << filename << endl;
00077         lootfile.close();
00078         
00079       } else
00080         cout << "Already processed file " << filename << endl;
00081     }
00082   }
00083 }
00084 
00085 bool TimeIsValid( int timet, int startt )
00086 {
00087   if(timet<1072918800) return false; // Before 2004
00088   if(timet>1577840400) return false; // after 2020
00089   if(abs(timet-startt)>600000) return false; // within a week of the current start time
00090   return true;
00091 }
00092 
00093 // How to loot a file:
00094 void lootSpillFile(const char* infile) 
00095 {
00096   char filename[1000];
00097   strcpy(filename,infile);
00098 
00099   bool gzipped = false;
00100   TString tfile(infile);
00101   if(tfile.EndsWith(".gz")) {
00102     gzipped = true;
00103     gSystem->Exec(Form("gunzip %s -c > /tmp/temp_spilldata.dat",infile));
00104     sprintf(filename,"/tmp/temp_spilldata.dat");
00105   }
00106 
00107   ifstream file(filename);
00108   if(!file.good()){
00109     cout << "Problem reading file " << filename << endl;
00110     return;
00111   }
00112 
00113   int stampsec;
00114   int stampnsec;
00115   int type;
00116   int spill;
00117 
00118   // Get first guess at start/end times:
00119   int timeStart = 2000000000;
00120   int timeEnd   = -100000000;
00121 
00122   // Build a vector of the data:
00123   vector<VldTimeStamp> data;
00124   //VldTimeStamp data[100000];
00125   UInt_t ndata = 0;
00126 
00127   char cline[1000];
00128   //string constants. From TimeGoblin/SpillHandler.cxx
00129   string Start("Start:");
00130   string Stop("Stop:");
00131 
00132   while((!file.eof())&&(file.good())) {
00133     file.getline(cline,1000,'\n');
00134     
00135     string line(cline);
00136 
00137     if(line.size()<=0) continue;
00138     
00139     if(line[0]=='#') {
00140       // It's a comment line.
00141       string::size_type pos;    
00142 
00143       pos = line.find(Start);
00144       if(pos!=string::npos) {
00145         // Found start line.
00146         if(sscanf(cline+pos+Start.size(),"%d",&stampsec)>0) {
00147           if(stampsec<timeStart) timeStart=stampsec;      
00148           if(stampsec>timeEnd  ) timeEnd=stampsec;        
00149           cout << "Start: " << stampsec << endl;
00150         } else {
00151           cout << "Couldn't interpret Start: line" << endl;
00152         }
00153       }
00154 
00155       pos = line.find(Stop);
00156       if(pos!=string::npos) {
00157         // Found start line.
00158         if(sscanf(cline+pos+Stop.size(),"%d",&stampsec)>0) {
00159           if(stampsec<timeStart) timeStart=stampsec;      
00160           if(stampsec>timeEnd  ) timeEnd=stampsec;        
00161           cout << "Stop: " << stampsec << endl;
00162         } else {
00163           cout << "Couldn't interpret Stop: line" << endl;
00164         }
00165       }
00166       
00167     } else {
00168       // ordinary line.
00169       int res = sscanf(cline,"%d %d %d %d",&stampsec,&stampnsec,&type,&spill);
00170       if(res==4) {
00171         //if(type==3)  // only genuine stamps now are written.
00172           
00173         if(TimeIsValid(stampsec,timeStart)) {
00174           if((stampsec+1) > timeEnd  ) timeEnd = stampsec+1;
00175           if((stampsec  ) < timeStart) timeStart = stampsec;
00176           
00177           VldTimeStamp stamp(stampsec,stampnsec);
00178           //cout  << stampsec << "\t" << stampnsec << "\t" << stamp.AsString() << endl;
00179           
00180           //data[ndata] = stamp;
00181           data.push_back(stamp);      
00182           ndata++;
00183         } else {
00184           cout << "Invalid timestamp: " << cline << endl;
00185         }       
00186       } else {
00187         cout << "Bad line: " << cline << endl;
00188       }
00189     }
00190   }
00191   
00192   //if(ndata>0) // Write the DB table even if no rows!
00193   {
00194 
00195   VldTimeStamp vldStart(timeStart,0);
00196   VldTimeStamp vldEnd(timeEnd,0);
00197 
00198   cout << "Time start: " << timeStart << "\t" << vldStart.AsString() << endl;
00199   cout << "Time end: "   << timeEnd   << "\t" << vldEnd.AsString() << endl;  
00200 
00201   VldTimeStamp vldCreate = vldStart;
00202   
00203   VldRange range( (int)Detector::kNear,
00204                   (int)SimFlag::kData,
00205                   vldStart,
00206                   vldEnd,
00207                   Form("Timestamps looted from TimeGoblin file %s by script $ Id: $",infile)
00208                   );
00209 
00210   cout << "Starting write: " << range.AsString() << endl;
00211 
00212   DbiWriter<SpillTimeND> writer (range,
00213                                  -1, //Aggregate
00214                                  SpillTimeND::kTask_TimeGoblin,  //task
00215                                  vldCreate, // Creation date
00216                                  0,   // db no
00217                                  Form("Timestamps looted from TimeGoblin spilltime.dat file %s by script $ Id: $",infile)
00218                                  );
00219 
00220   for(UInt_t i = 0; i<ndata; i++) {
00221     SpillTimeND datum(data[i]);
00222     writer << datum;
00223   }
00224 
00225   writer.Close();
00226   }
00227 
00228 
00229   if(gzipped) gSystem->Exec("rm /tmp/temp_spilldata.dat");
00230 }

Generated on Mon Feb 15 11:06:53 2010 for loon by  doxygen 1.3.9.1