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

BDSwicPedAccessor.cxx

Go to the documentation of this file.
00001 #include "BDSwicPedAccessor.h"
00002 #include "BDDevices.h"
00003 #include <Util/UtilBlockedRunningAverageVector.h>
00004 
00005 #include <MessageService/MsgService.h>
00006 CVSID("$Id: BDSwicPedAccessor.cxx,v 1.9 2006/05/27 07:31:45 rhatcher Exp $");
00007 
00008 
00009 #include <DatabaseInterface/DbiResultPtr.tpl>
00010 template class  DbiResultPtr<BeamMonSwicPeds>;
00011 
00012 #include <string>
00013 #include <vector>
00014 using namespace std;
00015 #include <cmath>
00016 
00017 static const float max_profile_ped = 50.0; // ADC
00018 
00019 
00020 static bool is_profile_monitor(const char* name)
00021 {
00022     static const vector<string> pm_names =  BDDevices::ProfileMonitors();
00023     for (size_t ind=0; ind<pm_names.size(); ++ind) {
00024         if (pm_names[ind] == name) return true;
00025     }
00026     return false;
00027 }
00028 
00029 static void mask_high_peds(vector<double> &ped)
00030 {
00031     for (int ind=0; ind<96; ++ind)
00032         if (fabs(ped[ind]) > max_profile_ped)
00033             ped[ind] = 0.0;
00034 }
00035 
00036 
00037 BDSwicPedAccessor::BDSwicPedAccessor()
00038     : fDRP(new DbiResultPtr<BeamMonSwicPeds>)
00039 {
00040 }
00041 
00042 BDSwicPedAccessor::~BDSwicPedAccessor()
00043 {
00044     SwicMap::iterator it, done = fSwicMap.end();
00045     for (it=fSwicMap.begin(); it != done; ++it) {
00046         delete it->second.channels;
00047     }
00048     fSwicMap.clear();
00049     if (fDRP) delete fDRP;
00050     fDRP = 0;
00051 }
00052 
00053 void BDSwicPedAccessor::AddDevice(const char* device_name,
00054                                   int minimum_sample_size)
00055 {
00056     if (fSwicMap.find(device_name) != fSwicMap.end()) return;
00057 
00058     int det = Detector::kNear | Detector::kFar;
00059     int sf = SimFlag::kData;
00060 
00061     Device dev;
00062     dev.channels = new UtilBlockedRunningAverageVector(96,minimum_sample_size);
00063     dev.range = VldRange(det,sf,VldTimeStamp::GetEOT(),VldTimeStamp::GetBOT(),
00064                          "swicpeds");
00065     dev.is_profile = is_profile_monitor(device_name);
00066     fSwicMap[device_name] = dev;
00067 }
00068 
00069 bool BDSwicPedAccessor::SetSpillTime(VldContext vc)
00070 {
00071     //cerr << "SetSpillTime with vc=" << vc << endl;
00072 
00073 
00074     // First find out if we need to bother
00075     bool finished = true;
00076     SwicMap::iterator it, done = fSwicMap.end();
00077     for (it=fSwicMap.begin(); it != done; ++it) {
00078         //cerr << it->first << ": vr=" << it->second.range << endl;
00079 
00080         if (! it->second.range.IsCompatible(vc)) {
00081             //cerr << "Incompatible, vc=" << vc << endl
00082             //<< "vr=" << it->second.range << endl;
00083             finished = false;
00084             break;
00085         }
00086     }
00087     if (finished) return false;
00088 
00089     // Got to hit the DB.  Maybe there is a smarter way to do this but
00090     // for now just clear everything and start fresh.
00091     for (it=fSwicMap.begin(); it != done; ++it) {
00092         if (!it->second.channels) {
00093             MSG("BD",Msg::kWarning) << "No device channels for " << it->first << endl;
00094             continue;
00095         }
00096         it->second.channels->Clear();
00097         it->second.range.SetTimeStart(VldTimeStamp::GetEOT());
00098         it->second.range.SetTimeEnd(VldTimeStamp::GetBOT());
00099     }
00100 
00101     int nrows = fDRP->NewQuery(vc,0,true);
00102     int n_empty_queries = 0;
00103     for (finished = false; !finished; nrows = fDRP->NextQuery(false)) {
00104 
00105         if (nrows)
00106             n_empty_queries = 0;
00107         else 
00108             ++n_empty_queries;
00109 
00110         if (n_empty_queries > 2) {
00111             MSG("BD",Msg::kWarning) << "Too many empty queries, giving up\n";
00112             return false;
00113         }
00114 
00115         const DbiValidityRec* vr = fDRP->GetValidityRec();
00116         //cerr << "Next query " << n_empty_queries << " with "
00117         //<< nrows << " rows, seqno="
00118         //<< vr->GetSeqNo() << endl
00119         //<< vr->GetVldRange() << endl;
00120 
00121         if (!nrows) {           // hit a gap.
00122             SwicMap::iterator it, done = fSwicMap.end();
00123             for (it=fSwicMap.begin(); it != done; ++it) {
00124                 Device& dev = it->second;
00125                 if (dev.range.GetTimeEnd() < vr->GetVldRange().GetTimeEnd())
00126                     dev.range.SetTimeEnd(vr->GetVldRange().GetTimeEnd());
00127                 if (dev.range.GetTimeStart() > vr->GetVldRange().GetTimeStart())
00128                     dev.range.SetTimeStart(vr->GetVldRange().GetTimeStart());
00129             }
00130             continue;
00131         }
00132 
00133         finished = true;
00134 
00135         for (int row = 0; row<nrows; ++row) {
00136             const BeamMonSwicPeds* peds = fDRP->GetRow(row);
00137             string name = peds->GetDeviceName();
00138             SwicMap::iterator it = fSwicMap.find(name);
00139             if (it == fSwicMap.end()) {
00140                 MSG("BD",Msg::kDebug) << "No Device for " << name << ", continuing\n";
00141                 continue;
00142             }
00143             Device& dev = it->second;
00144 
00145             if (!dev.channels) {
00146                 MSG("BD",Msg::kWarning) << "No Device for " << name << endl;
00147                 continue;
00148             }
00149 
00150             // don't need any more samples
00151             if (dev.channels->IsSampleFull()) {
00152                 MSG("BD",Msg::kDebug) << "Sample not yet full for " << name << ", continuing\n";
00153                 continue;
00154             }
00155 
00156             // enlarge vld range to include this
00157             if (dev.range.GetTimeEnd() < vr->GetVldRange().GetTimeEnd())
00158                 dev.range.SetTimeEnd(vr->GetVldRange().GetTimeEnd());
00159             if (dev.range.GetTimeStart() > vr->GetVldRange().GetTimeStart())
00160                 dev.range.SetTimeStart(vr->GetVldRange().GetTimeStart());
00161             
00162             vector<double> means = peds->GetMeansAsDoubles();
00163             if (dev.is_profile) mask_high_peds(means);
00164             vector<double> sigmas = peds->GetSigmasAsDoubles();
00165             dev.channels->Add(means,sigmas,peds->GetNsamples());
00166 
00167             if (!dev.channels->IsSampleFull()) finished = false;
00168         }
00169     }
00170 
00171     return true;
00172 }
00173 int BDSwicPedAccessor::GetPeds(const char* device_name,
00174                                vector<double> &mean,
00175                                vector<double> &sigma) const
00176 {
00177     SwicMap::const_iterator it = fSwicMap.find(device_name);
00178     if (it == fSwicMap.end()) {
00179         mean = vector<double>(96,0);
00180         sigma = vector<double>(96,0);
00181         return 0;
00182     }
00183     mean = it->second.channels->PopulationMean();
00184     sigma = it->second.channels->PopulationSigma();
00185     return it->second.channels->GetPopulationSize();
00186 }
00187 int BDSwicPedAccessor::GetPeds(const char* device_name,
00188                                vector<double> &mean) const
00189 {
00190     SwicMap::const_iterator it = fSwicMap.find(device_name);
00191     if (it == fSwicMap.end()) {
00192         mean = vector<double>(96,0);
00193         return 0;
00194     }
00195     mean = it->second.channels->PopulationMean();
00196     return it->second.channels->GetPopulationSize();
00197 }

Generated on Mon Nov 23 05:26:10 2009 for loon by  doxygen 1.3.9.1