00001
00002
00003
00004
00005
00006
00008 #include "NDSgateTimeLooter.h"
00009 #include "DatabaseInterface/DbiWriter.h"
00010 #include "SpillTiming/SpillTimeND.h"
00011 #include "MessageService/MsgService.h"
00012 #include "MinosObjectMap/MomNavigator.h"
00013 #include "JobControl/JobCModuleRegistry.h"
00014 #include "TTree.h"
00015 #include "TFile.h"
00016 #include "RawData/RawDigitDataBlock.h"
00017 #include "RawData/RawRecord.h"
00018 #include "RawData/RawDaqSnarlHeader.h"
00019 #include "DataUtil/GetRawBlock.h"
00020 #include "DataUtil/GetRawHeader.h"
00021 #include "SpillTiming/SpillTimeFinder.h"
00022 #include "Calibrator/Calibrator.h"
00023 #include "RawData/RawDigit.h"
00024 #include "RawData/RawVtmTimeInfoBlock.h"
00025 #include "OnlineUtil/mdTriggerCodes.h"
00026 #include "Conventions/Munits.h"
00027 #include <vector>
00028 #include <algorithm>
00029 #include <string>
00030 #include <TSystem.h>
00031 #include <TFile.h>
00032 #include <TH1.h>
00033
00034 JOBMODULE(NDSgateTimeLooter, "NDSgateTimeLooter",
00035 "NDSgateTimeLooter");
00036
00037 ClassImp(NdSgateRec)
00038
00039 CVSID("$Id: NDSgateTimeLooter.cxx,v 1.4 2007/11/11 05:51:05 rhatcher Exp $");
00040
00041
00042
00043
00044 NDSgateTimeLooter::NDSgateTimeLooter()
00045 {
00046 fFile = 0;
00047 fTree = 0;
00048 fInitialized = false;
00049 }
00050
00051
00052
00053 NDSgateTimeLooter::~NDSgateTimeLooter()
00054 {
00055 Write();
00056 if(fFile) {
00057 if(fTree) fTree->Write();
00058 fFile->Write();
00059 fFile->Close();
00060 delete fFile;
00061 }
00062 }
00063
00064
00065
00066 JobCResult NDSgateTimeLooter::Ana(const MomNavigator* mom)
00067 {
00068 const RawDaqHeader* header =
00069 DataUtil::GetRawHeader<RawDaqHeader>(mom);
00070
00071 if(header) {
00072 if(!fInitialized) {
00073 fInitialized = true;
00074 fStart = header->GetVldContext().GetTimeStamp();
00075 fEnd = fStart;
00076 fStamps.clear();
00077 fRunStart = fRunStop = header->GetRun();
00078 }
00079
00080 if(header->GetVldContext().GetTimeStamp()>fEnd)
00081 fEnd = header->GetVldContext().GetTimeStamp();
00082 if(header->GetRun() > fRunStop)
00083 fRunStop = header->GetRun();
00084
00085 }
00086
00087 const std::vector<const RawVtmTimeInfoBlock*> vtmBlocks =
00088 DataUtil::GetRawBlocks<RawVtmTimeInfoBlock>(mom);
00089
00090 if(vtmBlocks.size()>0) {
00091
00092 static int last_latch = -1;
00093 if(vtmBlocks[0]->GetSpillTimeGPS() != last_latch) {
00094 last_latch = vtmBlocks[0]->GetSpillTimeGPS();
00095
00096 if(vtmBlocks[0]->GetTimeFrameNum()!=0)
00097 {
00098
00099
00100 int latch10_tot = 0;
00101 int latch10_low = 100000000;
00102 int latch10_high = 0;
00103 int nlatch = 0;
00104
00105 for(UInt_t i=0;i<vtmBlocks.size();i++) {
00106 int l = vtmBlocks[i]->GetSpillTimeGPS();
00107 if(l<latch10_low) latch10_low = l;
00108 if(l>latch10_high) latch10_high = l;
00109 latch10_tot += l;
00110 nlatch++;
00111 }
00112
00113 double latch10_avg = (double)latch10_tot/(double)nlatch;
00114
00115
00116 int nsec_53MHz = (int)(1e9*(double)vtmBlocks[0]->GetSpillTimeRF()
00117 /(double)vtmBlocks[0]->GetLastTimeStamp());
00118
00119
00120 if((latch10_high-latch10_low)>1) {
00121 MSG("SpillTime",Msg::kWarning) << "Not all crates agree on 10MHz time." << endl;
00122 MSG("SpillTime",Msg::kWarning) << "Earliest: " << latch10_low
00123 << " Latest: " << latch10_high
00124 << " Mean: " << latch10_avg << endl;
00125 }
00126
00127
00128 VldContext cx = vtmBlocks[0]->GetVldContext();
00129
00130
00131 int second = cx.GetTimeStamp().GetSec()
00132 + (int)TMath::Nint(cx.GetTimeStamp().GetNanoSec()/1e9)
00133 -1;
00134
00135 VldTimeStamp sgate_time(second, (int)(latch10_avg * 100.));
00136 VldTimeStamp sgate_53 (second, nsec_53MHz);
00137
00138 VldContext check_context(Detector::kNear,SimFlag::kData,sgate_time);
00139 VldTimeStamp t_database = SpillTimeFinder::Instance().
00140 GetTimeOfNearestSpill(cx);
00141
00142 MSG("SpillTime",Msg::kDebug) << "SGATE: " << sgate_time.AsString() << endl;
00143 MSG("SpillTime",Msg::kDebug) << " DB: " << t_database.AsString() << endl;
00144 MSG("SpillTime",Msg::kDebug) << " dt: " << (double)(sgate_time - t_database)*1e9 << "ns"
00145 << "\t tf: " << vtmBlocks[0]->GetTimeFrameNum() << endl;
00146 if(fWriteToFile) {
00147 static NdSgateRec* rec = 0;
00148 if(fFile==0) {
00149 rec = new NdSgateRec;
00150 fFile = new TFile(GetConfig().GetCharString("FileName"),"RECREATE");
00151 fTree = new TTree("vtm","vtm");
00152 fTree->Branch("ndrec","NdSgateRec",&rec,32000,1);
00153 }
00154
00155 rec->run = vtmBlocks[0]->GetRun();
00156 rec->subrun = vtmBlocks[0]->GetSubRun();
00157 rec->monitorts = cx.GetTimeStamp();
00158 rec->sgate = sgate_time;
00159 rec->sgate_53 = sgate_53;
00160 rec->spill = t_database;
00161 rec->dspill = (double)(sgate_time-t_database);
00162 fTree->Fill();
00163 }
00164
00165 if(fWriteToDB)
00166 fStamps.push_back(sgate_time);
00167 }
00168 }
00169 }
00170
00171 if((double)(fEnd-fStart) > fTableDuration) {
00172 Write();
00173 fStart = fEnd;
00174 fEnd = fStart;
00175 }
00176
00177 return JobCResult::kPassed;
00178 }
00179
00180 void NDSgateTimeLooter::Write()
00181 {
00182 if(fWriteToDB) {
00183
00184 MSG("SpillTime",Msg::kInfo) << "Time start: " << fStart.AsString() << endl;
00185 MSG("SpillTime",Msg::kInfo) << "Time end: " << fEnd.AsString() << endl;
00186
00187 VldTimeStamp vldCreate = fStart;
00188
00189 VldRange range( (int)Detector::kNear,
00190 (int)SimFlag::kData,
00191 fStart,
00192 fEnd,
00193 Form("Timestamps looted from ND VTM data, runs %d-%d",fRunStart,fRunStop)
00194 );
00195
00196 MSG("SpillTime",Msg::kInfo) << "Starting write: " << range.AsString() << endl;
00197
00198 DbiWriter<SpillTimeND> writer (range,
00199 -1,
00200 SpillTimeND::kTask_Vtm,
00201 vldCreate,
00202 0,
00203 Form("Timestamps looted from ND VTM data, runs %d-%d",fRunStart,fRunStop)
00204 );
00205
00206 for(UInt_t i = 0; i<fStamps.size(); i++) {
00207 SpillTimeND datum(fStamps[i]);
00208 writer << datum;
00209 }
00210
00211 writer.Close();
00212 }
00213
00214
00215 fStamps.clear();
00216 fInitialized = false;
00217 }
00218
00219
00220
00221
00222 const Registry& NDSgateTimeLooter::DefaultConfig() const
00223 {
00224
00225
00226
00227 static Registry r;
00228
00229
00230 std::string name = this->GetName();
00231 name += ".config.default";
00232 r.SetName(name.c_str());
00233
00234
00235 r.UnLockValues();
00236
00237 r.Set("TableDuration",8.0*Munits::hour);
00238 r.Set("WriteToDB",1);
00239 r.Set("WriteToFile",0);
00240 r.Set("FileName","nd_sgates.root");
00241
00242 r.LockValues();
00243
00244 return r;
00245 }
00246
00247
00248
00249 void NDSgateTimeLooter::Config(const Registry& r)
00250 {
00251 r.Get("TableDuration",fTableDuration);
00252 r.Get("WriteToDB",fWriteToDB);
00253 r.Get("WriteToFile",fWriteToFile);
00254 }
00255