00001 #include "Riostream.h"
00002 #include "TFile.h"
00003 #include "TTree.h"
00004 #include "TSystem.h"
00005 #include "TMath.h"
00006 #include <map>
00007 #include <algorithm>
00008 #include <vector>
00009 #include <iostream>
00010 #include "Validity/VldTimeStamp.h"
00011 #include "DatabaseInterface/DbiWriter.h"
00012 #include "MiniBooNEAna/MBSpill.h"
00013 #include "Conventions/Munits.h"
00014
00015
00016 using namespace std;
00017
00018 void Fill_MBSpill(){
00019
00020
00021 Char_t dirname[]="/afs/fnal.gov/files/data/minos/d69/tjyang/test/timing/new/";
00022 void *dirhandle = gSystem->OpenDirectory(gSystem->ExpandPathName(dirname));
00023
00024 Int_t nfiles = 0;
00025 Char_t *file;
00026 Char_t path_file[100];
00027
00028
00029 map<VldTimeStamp, MBSpill> spills;
00030 map<VldTimeStamp, MBSpill>::iterator p;
00031
00032 Double_t fTableDuration = 8.0*Munits::hour;
00033
00034
00035
00036 while ( ( file = const_cast<char*>(gSystem->GetDirEntry(dirhandle)) ) ) {
00037 ifstream in;
00038 if (strstr(file,"spill_info_") != 0) {
00039 strcpy(path_file,dirname);
00040 strcat(path_file,file);
00041 in.open(path_file);
00042 cout<<"open file:"<<path_file<<endl;
00043 nfiles++;
00044 int sec,nsec;
00045 float pot,hI;
00046 while (1) {
00047 in >> sec >> nsec >> pot >> hI;
00048 if (!in.good()) break;
00049 MBSpill mbspill(sec,nsec,pot,hI);
00050 VldTimeStamp ts((time_t)sec,nsec);
00051 p=spills.find(ts);
00052 if (p != spills.end()){
00053 if (pot>p->second.GetPOT()){
00054 spills.erase(p);
00055 spills.insert(pair<VldTimeStamp,MBSpill>(ts,mbspill));
00056 }
00057 }
00058 else if (TMath::Abs(hI)>10) {
00059 spills.insert(pair<VldTimeStamp,MBSpill>(ts,mbspill));
00060 }
00061 }
00062 }
00063 in.close();
00064 }
00065
00066
00067 if (spills.size()==0) {
00068 cout<<"no data found, check directory"<<endl;
00069 exit(0);
00070 }
00071
00072 cout<<nfiles<<" files found."<<endl;
00073
00074 p = spills.begin();
00075 VldTimeStamp fStart = p->first;
00076 VldTimeStamp fEnd = fStart;
00077 cout<<"Start "<<" "<<fStart<<" End "<<fEnd<<endl;
00078 vector<MBSpill> spills_chunk;
00079 while (p!=spills.end()){
00080 spills_chunk.push_back(p->second);
00081 fEnd = p->first;
00082 if((double)(fEnd-fStart) > fTableDuration && spills_chunk.size()){
00083 VldTimeStamp vldCreate = fStart;
00084 VldRange range( (int)Detector::kNear,
00085 (int)SimFlag::kData,
00086 fStart,
00087 fEnd,
00088 "MiniBooNE spill information"
00089 );
00090 cout<<"Starting write: " << range.AsString() << endl;
00091 DbiWriter<MBSpill> writer (range,
00092 -1,
00093 0,
00094 vldCreate,
00095 0,
00096 "MiniBooNE spill information"
00097 );
00098
00099 for (UInt_t i = 0; i<spills_chunk.size(); i++){
00100 writer << spills_chunk[i];
00101 }
00102 writer.Close();
00103 spills_chunk.clear();
00104 fStart = fEnd;
00105 fEnd = fStart;
00106 }
00107 p++;
00108 }
00109
00110 if(spills_chunk.size()){
00111 VldTimeStamp vldCreate = fStart;
00112 VldRange range( (int)Detector::kNear,
00113 (int)SimFlag::kData,
00114 fStart,
00115 fEnd,
00116 "MiniBooNE spill information"
00117 );
00118 cout<<"Starting write: " << range.AsString() << endl;
00119 DbiWriter<MBSpill> writer (range,
00120 -1,
00121 0,
00122 vldCreate,
00123 0,
00124 "MiniBooNE spill information"
00125 );
00126 for (UInt_t i = 0; i<spills_chunk.size(); i++){
00127 writer << spills_chunk[i];
00128 }
00129 writer.Close();
00130 spills_chunk.clear();
00131
00132 }
00133
00134 }