00001
00002
00003
00004
00005
00006
00007
00008
00010
00011
00012
00014
00015 #include <cassert>
00016 #include <float.h>
00017 #include <map>
00018
00019 #include "TROOT.h"
00020 #include "Riostream.h"
00021 #include "TFolder.h"
00022
00023
00024 #include "CandDigit/CandDigitBadChannels.h"
00025 #include "DatabaseInterface/DbiResultPtr.h"
00026
00027
00028 #include "Algorithm/AlgConfig.h"
00029 #include "Algorithm/AlgFactory.h"
00030 #include "CandDigit/AlgFilterDigitList.h"
00031 #include "Algorithm/AlgHandle.h"
00032 #include "CandDigit/CandDigit.h"
00033 #include "CandDigit/CandDigitHandle.h"
00034 #include "CandDigit/CandDigitList.h"
00035 #include "CandDigit/CandDigitListHandle.h"
00036 #include "CandDigit/FilterBadChannelMap.h"
00037 #include "Candidate/CandContext.h"
00038 #include "MessageService/MsgService.h"
00039 #include "RawData/RawChannelId.h"
00040 #include "Validity/VldContext.h"
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 ClassImp(AlgFilterDigitList)
00053
00054
00055 CVSID("$Id: AlgFilterDigitList.cxx,v 1.12 2008/11/12 17:37:14 rhatcher Exp $");
00056
00057
00058 AlgFilterDigitList::AlgFilterDigitList() :
00059 fTFolder(0)
00060 {
00061 }
00062
00063
00064 AlgFilterDigitList::~AlgFilterDigitList()
00065 {
00066 FilterBadChannelMap *badChannelMap;
00067 if (fTFolder && (badChannelMap = dynamic_cast<FilterBadChannelMap*>
00068 (fTFolder->FindObject("FilterBadChannelMap")))) {
00069 fTFolder->Remove(badChannelMap);
00070 delete badChannelMap;
00071 }
00072 }
00073
00074
00075 void AlgFilterDigitList::RunAlg(AlgConfig &ac, CandHandle &ch,
00076 CandContext &cx)
00077 {
00078 MSG("AlgFilterDigitList", Msg::kDebug)
00079 << "Starting AlgFilterDigitList::RunAlg()" << endl;
00080
00081
00082 assert(cx.GetDataIn());
00083
00084
00085 const CandDigitListHandle *cdlh =
00086 dynamic_cast<const CandDigitListHandle *> (cx.GetDataIn());
00087 assert(cdlh);
00088
00089
00090 Int_t filterstrategy = 0;
00091 Float_t chargeMin = -FLT_MAX;
00092 Double_t timeMin = -FLT_MAX;
00093 Int_t errorBitsMaskVA = 0;
00094 Int_t errorBitsMaskQIE = 0;
00095
00096 Int_t tmpi = 0;
00097 Double_t tmpd = 0;
00098 const char* tmps;
00099 if (ac.Get("FilterStrategy", tmpi)) filterstrategy = tmpi;
00100 if (ac.Get("ChargeMin",tmpd)) chargeMin = tmpd;
00101 if (ac.Get("SubtractedTimeMin",tmpd)) timeMin = tmpd;
00102 if (ac.Get("ErrorBitsMaskVA",tmpi)) errorBitsMaskVA = tmpi;
00103 if (ac.Get("ErrorBitsMaskQIE",tmpi)) errorBitsMaskQIE = tmpi;
00104 if (ac.Get("BadChannelFile", tmps)) fBadChannelFile = tmps;
00105
00106 MSG("AlgFilterDigitList", Msg::kDebug) << "file = "
00107 << fBadChannelFile << " strategy = "
00108 << filterstrategy << endl;
00109
00110
00111 ch.SetCandRecord((cdlh->GetCandRecord()));
00112
00113
00114 CandDigitListHandle &cddlh = dynamic_cast<CandDigitListHandle &>(ch);
00115 cddlh.SetAbsTime(cdlh->GetAbsTime());
00116 cddlh.SetIsSparse(cdlh->GetIsSparse());
00117
00118
00119 TIter cdhiter = cdlh->GetDaughterIterator();
00120 TObject *tob;
00121 while ((tob = cdhiter())) {
00122 CandDigitHandle *cdh = dynamic_cast<CandDigitHandle *>(tob);
00123 if (cdh == 0) continue;
00124
00125
00126
00127
00128
00129
00130 if (1==filterstrategy) {
00131 if ( cdh->GetCharge() < chargeMin ) continue;
00132 if ( cdh->GetSubtractedTime() < timeMin ) continue;
00133 if ( cdh->GetVaErrorBits() & errorBitsMaskVA ) continue;
00134 if ( cdh->GetQieErrorBits() & errorBitsMaskQIE ) continue;
00135 }
00136
00137
00138 if(filterstrategy==2){
00139 MSG("AlgFilterDigitList", Msg::kDebug)
00140 << "using strategy 2" << endl;
00141
00142
00143 if (fTFolder==0) {
00144 TFolder *lf = dynamic_cast<TFolder*>
00145 (gROOT->GetRootFolder()->FindObject("Loon"));
00146 if (lf==0) {
00147 MSG("AlgFilterDigitList", Msg::kDebug)
00148 << "Creating Loon TFolder" << endl;
00149 lf = gROOT->GetRootFolder()->AddFolder("Loon",
00150 "Loon analysis");
00151 gROOT->GetListOfBrowsables()->Add(lf, "Loon");
00152 }
00153 fTFolder = dynamic_cast<TFolder*>(lf->FindObject("CandDigit"));
00154 if (fTFolder==0) {
00155 MSG("AlgFilterDigitList", Msg::kDebug)
00156 << "Creating Loon/CandDigit TFolder" << endl;
00157 fTFolder =
00158 lf->AddFolder("CandDigit", "CandDigit diagnostics");
00159 }
00160 }
00161
00162 FilterBadChannelMap *badChannelMap =
00163 dynamic_cast<FilterBadChannelMap*>
00164 (fTFolder->FindObject("FilterBadChannelMap"));
00165 if (badChannelMap==0) {
00166 MAXMSG("AlgFilterDigitList", Msg::kInfo,2)
00167 << "Creating Loon/CandDigit/FilterBadChannelMap" << endl;
00168 MakeBadChannelMap(cdh->GetVldContext()->GetDetector());
00169 badChannelMap = dynamic_cast<FilterBadChannelMap*>
00170 (fTFolder->FindObject("FilterBadChannelMap"));
00171
00172 if ( !badChannelMap ) MSG("AlgFilterDigitList", Msg::kFatal)
00173 << "still no Loon/CandDigit/FilterBadChannelMap" << endl;
00174 }
00175
00176
00177 RawChannelId theChannel = cdh->GetChannelId();
00178 theChannel.ClearModeBits();
00179
00180 MSG("AlgFilterDigitList", Msg::kDebug) << "encoded value = "
00181 << theChannel.AsString("ec") << endl;
00182
00183
00184 map<RawChannelId, bool>& theMap = badChannelMap->fBadChannelMap;
00185 if (theMap.find(theChannel) != theMap.end()) {
00186 MSG("AlgFilterDigitList", Msg::kDebug)
00187 << "filtering " << cdh->GetChannelId().GetCrate() << "-"
00188 << cdh->GetChannelId().GetGeographicAddress() << "-"
00189 << cdh->GetChannelId().GetMasterChannel() << "-"
00190 << cdh->GetChannelId().GetMinderChannel() << endl;
00191 continue;
00192 }
00193
00194 if ( cdh->GetCharge() < chargeMin ) continue;
00195 if ( cdh->GetSubtractedTime() < timeMin ) continue;
00196 if ( cdh->GetVaErrorBits() & errorBitsMaskVA ) continue;
00197 if ( cdh->GetQieErrorBits() & errorBitsMaskQIE ) continue;
00198
00199 }
00200
00202
00203
00205 else if(filterstrategy==3){
00206 MSG("AlgFilterDigitList",Msg::kVerbose)
00207 <<"Using filterstrategy 3"<<endl;
00208
00209
00210 if ( cdh->GetCharge() < chargeMin ) continue;
00211 if ( cdh->GetSubtractedTime() < timeMin ) continue;
00212 if ( cdh->GetVaErrorBits() & errorBitsMaskVA ) continue;
00213 if ( cdh->GetQieErrorBits() & errorBitsMaskQIE ) continue;
00214
00216
00218
00219
00220 static FilterBadChannelMap* badChannelMap=0;
00221
00222
00223 static VldContext lastvc;
00224 if (*cdh->GetVldContext()!=lastvc) {
00225 MAXMSG("AlgFilterDigitList",Msg::kDebug,3)
00226 <<"AlgFilterDigitList: found new VldContext..."<<endl;
00227
00228 badChannelMap=this->MakeBadChannelMapDB(*cdh->GetVldContext());
00229
00230 if ( !badChannelMap ) MSG("AlgFilterDigitList", Msg::kFatal)
00231 << "failed to get anything from MakeBadChannelMapDB" << endl;
00232 else {
00233
00234
00235 static int ndumps = 1;
00236 if ( ndumps > 0 ) {
00237 --ndumps;
00238 size_t nbad = badChannelMap->fBadChannelMap.size();
00239 MSG("AlgFilterDigitList", Msg::kInfo)
00240 << "AlgFilterDigitList strategy " << filterstrategy
00241 << ", BadChannelMap has " << nbad << " entries" << endl;
00242 std::map<RawChannelId, bool>::const_iterator bcitr =
00243 badChannelMap->fBadChannelMap.begin();
00244 std::map<RawChannelId, bool>::const_iterator bcitr_end =
00245 badChannelMap->fBadChannelMap.end();
00246 size_t indx = 0;
00247 while ( bcitr != bcitr_end ) {
00248 MSG("AlgFilterDigitList", Msg::kInfo)
00249 << " [" << std::setw(3) << indx << "] "
00250 << bcitr->first << " " << ((bcitr->second)?"true":"false")
00251 << endl;
00252 indx++; bcitr++;
00253 }
00254 }
00255 }
00256 }
00257 assert(badChannelMap);
00258
00259 lastvc=*cdh->GetVldContext();
00260
00261
00262 if (badChannelMap->fBadChannelMap.size()>0) {
00263
00264 RawChannelId theChannel=cdh->GetChannelId();
00265 theChannel.ClearModeBits();
00266
00267 MSG("AlgFilterDigitList",Msg::kDebug)
00268 <<"Encoded value = "<<theChannel.AsString("ec")<<endl;
00269
00270
00271 map<RawChannelId, bool>& theMap = badChannelMap->fBadChannelMap;
00272 if (theMap.find(theChannel)!=theMap.end()) {
00273 MAXMSG("AlgFilterDigitList",Msg::kInfo,3)
00274 <<"AlgFilterDigitList::Filtering bad channel: "<<theChannel.AsString("ec")
00275 <<endl;
00276 continue;
00277 }
00278 }
00279 else {
00280 MAXMSG("AlgFilterDigitList",Msg::kInfo,1)
00281 <<"AlgFilterDigitList: No CandDigitBadChannels found in map"
00282 <<", skipping this filter step"<<endl;
00283 }
00284 }
00285
00286
00287 CandDigitHandle cddh(*cdh);
00288 ch.AddDaughterLink(cddh, kFALSE);
00289 }
00290 }
00291
00292
00293 void AlgFilterDigitList::Trace(const char * ) const
00294 {
00295 }
00296
00297
00298
00299 void AlgFilterDigitList::MakeBadChannelMap(Detector::Detector_t det)
00300 {
00301 MSG("AlgFilterDigitList",Msg::kDebug)
00302 << "MakeBadChannelMap called for " << Detector::AsString(det)
00303 << " using " << fBadChannelFile << endl;
00304
00305 UInt_t prevEncoded = 0;
00306
00307
00308 FilterBadChannelMap *badChMap = new FilterBadChannelMap();
00309
00310
00311 ifstream badChannels(fBadChannelFile.c_str());
00312
00313 if (!badChannels.is_open()) {
00314 MSG("AlgFilterDigitList", Msg::kError)
00315 << "cannot open list of bad channels,"
00316 << fBadChannelFile
00317 << " assume there are none" << endl;
00318
00319
00320 fTFolder->Add(badChMap);
00321 return;
00322 }
00323
00324
00325 if (det==Detector::kNear) {
00326 UInt_t crate = 0;
00327 UInt_t geo = 0;
00328 UInt_t masterChannel = 0;
00329 UInt_t minderChannel = 0;
00330
00331
00332 while (!badChannels.eof()) {
00333 badChannels >> crate >> geo >> masterChannel >> minderChannel;
00334
00335
00336 #ifdef RAWCHANNELID_NEW_CTOR
00337 RawChannelId rawId(det,ElecType::kQIE, crate, geo,
00338 masterChannel, minderChannel);
00339 #else
00340 RawChannelId rawId(det,ElecType::kQIE, crate, geo,
00341 masterChannel, minderChannel, false, false);
00342 #endif
00343 rawId.ClearModeBits();
00344 MSG("AlgFilterDigitList", Msg::kDebug)
00345 << "*******filter channel********"
00346 << crate << " " << geo << " " << masterChannel << " "
00347 << minderChannel << " "
00348 << rawId.AsString("ec") << endl;
00349
00350
00351 if (rawId.GetEncoded() != prevEncoded)
00352 badChMap->fBadChannelMap[rawId] = true;
00353
00354 }
00355 }
00356
00357
00358 fTFolder->Add(badChMap);
00359 return;
00360 }
00361
00362
00363
00364 FilterBadChannelMap* AlgFilterDigitList::MakeBadChannelMapDB(const VldContext& vc)
00365 {
00366
00367 static FilterBadChannelMap* badChMap=new FilterBadChannelMap();
00368
00369
00370 badChMap->fBadChannelMap.clear();
00371
00372 static Bool_t firstTime=true;
00373 if (firstTime) {
00374 firstTime=false;
00375 MAXMSG("AlgFilterDigitList",Msg::kDebug,200)
00376 <<"Running AlgFilterDigitList::MakeBadChannelMapDB for first time"
00377 <<endl;
00378
00379 }
00380
00381
00382
00383 Int_t task=0;
00384 DbiResultPtr<CandDigitBadChannels> badChannelsPtr(vc,task);
00385
00386 MAXMSG("AlgFilterDigitList",Msg::kDebug,100)
00387 <<"Num of CandDigitBadChannels rows="
00388 <<badChannelsPtr.GetNumRows()<<endl;
00389
00390
00391 for (UInt_t irow=0;irow<badChannelsPtr.GetNumRows();++irow) {
00392
00393
00394 const CandDigitBadChannels& badChannel=*(badChannelsPtr.GetRow(irow));
00395
00396 MSG("AlgFilterDigitList",Msg::kDebug)
00397 <<"Crate="<<badChannel.GetCrate()
00398 <<", ChAdd="<<badChannel.GetChAdd()
00399
00400
00401
00402 <<endl;
00403
00404 Int_t crate=badChannel.GetCrate();
00405 Int_t chadd=badChannel.GetChAdd();
00406
00407
00408 ElecType::Elec_t et=ElecType::kVA;
00409 if (vc.GetDetector()==Detector::kNear) et=ElecType::kQIE;
00410
00411
00412 RawChannelId rawId(vc.GetDetector(),et,crate,chadd);
00413 rawId.ClearModeBits();
00414
00415 MAXMSG("AlgFilterDigitList",Msg::kDebug,200)
00416 << "*******filter channel********"
00417 <<rawId.AsString("ec")<<endl;
00418
00419
00420
00421 badChMap->fBadChannelMap[rawId]=true;
00422 }
00423
00424 return badChMap;
00425 }
00426
00427