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

BDCheckDB.cxx

Go to the documentation of this file.
00001 #include "BDCheckDB.h"
00002 
00003 #include <BeamDataUtil/BDSpillAccessor.h>
00004 #include <BeamDataUtil/BeamMonSpill.h>
00005 #include <BeamDataUtil/BDEarliest.h>
00006 
00007 #include <Validity/VldContext.h>
00008 #include <Validity/VldTimeStamp.h>
00009 #include <RawData/RawRecord.h>
00010 #include <RawData/RawBeamMonHeaderBlock.h>
00011 #include <RawData/RawBeamMonBlock.h>
00012 #include <RawData/RawDataBlock.h>
00013 #include <RawData/RawBeamData.h>
00014 #include <Util/UtilMath.h>
00015 #include <Conventions/Munits.h>
00016 #include <HistMan/HistMan.h>
00017 
00018 #include <Conventions/Detector.h>
00019 #include <Conventions/SimFlag.h>
00020 #include <SpillTiming/SpillTimeFinder.h>
00021 
00022 #include <TSystem.h>
00023 #include <TTree.h>
00024 #include <TFile.h>
00025 
00026 #include <list>
00027 #include <string>
00028 #include <iostream>
00029 using namespace std;
00030 
00031 const char* TOROIDS[] = { "E:TOR101", "E:TR101D", "E:TORTGT", "E:TRTGTD", 0 };
00032 const char* toroids[] = { "tor101", "tr101d", "tortgt", "trtgtd", 0 };
00033 
00034 BDCheckDB::BDCheckDB()
00035 {
00036     HistMan hm("bd_check_db");
00037 
00038     hm.Book<TH1F>("dt","Time from file VldContext to spill time, less delay",
00039             1000,-0.1,0.1);
00040     hm.Book<TH1F>("dtnd","Time from file VldContext to SpillTimeND",
00041             1000,-0.1,0.1);
00042     hm.Book<TH1F>("dh","File-DB horn diff (kAmp)",
00043             200,-0.001,0.001);
00044 
00045     for (int ind=0; toroids[ind]; ++ind) {
00046         hm.Book<TH1F>(toroids[ind],Form("File-DB for %s",TOROIDS[ind]),
00047                       200,-0.0001,0.0001);
00048     }
00049 }
00050 
00051 BDCheckDB::~BDCheckDB()
00052 {
00053     HistMan hm("");
00054     hm.WriteOut("bd_check_db.root");
00055 }
00056 
00057 bool BDCheckDB::SpinFile(const char* filename)
00058 {
00059     TFile infile(filename,"READ");
00060     TTree* intree=(TTree*)(infile.Get("BeamMon"));
00061 
00062     int bad_spills = 0, count=0;
00063 
00064     RawRecord* record = 0;
00065     int nentries = intree->GetEntries();
00066     for ( Int_t ient = 0; ient < nentries; ient++ ) {
00067         intree -> SetBranchAddress("RawRecord",&record);
00068         intree->GetEntry(ient);
00069 
00070         const RawBeamMonBlock* rbmb = 0;
00071         const RawBeamMonHeaderBlock* rbmhb = 0;
00072         TObject *obj = 0;
00073         TIter itr = record->GetRawBlockIter();
00074         while ((obj = itr())) {
00075             const RawBeamMonBlock* b =
00076                 dynamic_cast<const RawBeamMonBlock*>(obj);
00077             if (b) rbmb = b;
00078             const RawBeamMonHeaderBlock* h =
00079                 dynamic_cast<const RawBeamMonHeaderBlock*>(obj);
00080             if (h) rbmhb = h;
00081         }
00082 
00083         if (!(rbmb && rbmhb)) continue;
00084 
00085         ++count;
00086         bool good = this->CheckSpill(*rbmb,*rbmhb);
00087         if (!good) ++bad_spills;
00088 
00089     }
00090 
00091     if (bad_spills)
00092         cerr << filename << " " << bad_spills << " bad spills out of "
00093              << count << endl;
00094 
00095     infile.Close();
00096     return !bad_spills;
00097 }
00098 
00099 void BDCheckDB::SpinDir(const char* dirname)
00100 {
00101     void* dir = gSystem->OpenDirectory(dirname);
00102     if (!dir) {
00103         cerr << "Failed to open directory \"" << dirname << "\"\n";
00104         return;
00105     }
00106     list<string> file_names;
00107     const char* cptr=0;
00108     while ( (cptr = gSystem->GetDirEntry(dir)) )
00109         if (cptr) file_names.push_back(cptr);
00110     if (!file_names.size()) return;
00111     file_names.sort();
00112     list<string>::iterator it, done = file_names.end();
00113 
00114     int bad_files = 0;
00115 
00116     for (it=file_names.begin(); it != done; ++it) {
00117         string file = *it;
00118         if (string::npos == file.find(".mbeam.root") || file[0] != 'B') {
00119             cerr << "Skipping unmatched file \"" << file << "\"\n";
00120             continue;
00121         }
00122         
00123         string path = dirname;
00124         path += "/" + file;
00125         cerr << "Processing " << path << endl;
00126         
00127         bool good = this->SpinFile(path.c_str());
00128         if (!good) {
00129             ++bad_files;
00130             cerr << path << " is bad\n";
00131         }
00132     }
00133     
00134     cerr << bad_files << " bad files\n";
00135 }
00136 
00137 
00138 VldTimeStamp get_timestamp(const RawBeamMonBlock& rbmb)
00139 {
00140     double dae=0, vme=0;
00141     BDEarliest::CalculateEarliest(rbmb,dae,vme);
00142     if (vme == 0) {
00143         int t = (int)dae;
00144         return VldTimeStamp(t,(int)((dae-t)*1e9));
00145     }
00146     else {
00147         int t = (int)vme;
00148         return VldTimeStamp(t,(int)((vme-t)*1e9));
00149     }
00150 }
00151 
00152 
00153 bool BDCheckDB::CheckSpill(const RawBeamMonBlock& rbmb,
00154                            const RawBeamMonHeaderBlock& /*rbmhb*/)
00155 {
00156     HistMan hm("bd_check_db");
00157 
00158     VldTimeStamp file_vts = get_timestamp(rbmb);
00159     const BeamMonSpill* spill = BDSpillAccessor::Get().LoadSpill(file_vts);
00160     if (!spill) {
00161         cerr << file_vts << " no DB entry\n";
00162         return false;
00163     }
00164     Detector::Detector_t det = Detector::kNear;
00165     SimFlag::SimFlag_t simflag = SimFlag::kData;
00166     VldContext file_vc(det,simflag,file_vts);
00167     VldTimeStamp spilltimend = SpillTimeFinder::Instance().GetTimeOfNearestSpill(file_vc);
00168 
00169     double nd_file_diff_time = (double)(file_vts - spilltimend);
00170     hm.Fill1d("dtnd",nd_file_diff_time);
00171 
00172     if (fabs(nd_file_diff_time) > 0.5) {
00173         cerr << "spilltimend: "
00174              << file_vts << " " << spilltimend << " " << nd_file_diff_time << endl;
00175     }
00176 
00177     VldTimeStamp db_vts = spill->SpillTime();
00178     double diff_time = file_vts - db_vts;
00179     hm.Fill1d("dt",diff_time);
00180     if (fabs(diff_time) > 0.5) {
00181         cerr << "timing: "
00182              << file_vts << " " << db_vts << " " << diff_time << endl;
00183         return false;
00184     }
00185 
00186 
00187     int errors=0;
00188 
00189     double db_toroids[4] = {
00190         spill->fTor101, spill->fTr101d, spill->fTortgt, spill->fTrtgtd
00191     };
00192 
00193     for (int ind=0; TOROIDS[ind]; ++ind) {
00194         double file_tor = 0;
00195         const RawBeamData* data = rbmb[TOROIDS[ind]];
00196         if (data && data->GetDataLength()) file_tor = data->GetData()[0];
00197         if (UtilMath::sameValue(file_tor, db_toroids[ind],true)) continue;
00198         double dt = file_tor - db_toroids[ind];
00199         hm.Fill1d(toroids[ind],dt);
00200         if (fabs(dt/(file_tor+db_toroids[ind])) > 1e-5) {
00201             cerr << "toroids: "
00202                  << db_vts << " " << TOROIDS[ind] << " "
00203                  << file_tor << " - " << db_toroids[ind] << " = "
00204                  << dt << " rel = " << dt/(file_tor+db_toroids[ind])
00205                  << endl;
00206             ++errors;
00207         }
00208     }
00209 
00210     double file_horn = 0;
00211     const char* horns[] = { "E:NSLINA","E:NSLINB","E:NSLINC","E:NSLIND",0 };
00212     for (int ind=0; horns[ind]; ++ind) {
00213         const RawBeamData* data = rbmb[horns[ind]];
00214         if (data && data->GetDataLength()) file_horn += data->GetData()[0];
00215     }
00216     double dh = file_horn-spill->fHornCur;
00217     hm.Fill1d("dh",dh);
00218     if (fabs(dh) > 0.01) {
00219         cerr << "horn: file=" << file_horn << " db="<<spill->fHornCur 
00220              << " dh=" << dh << endl;
00221         ++errors;
00222     }
00223 
00224     return !errors;
00225 }

Generated on Sat Nov 21 22:45:29 2009 for loon by  doxygen 1.3.9.1