File indexing completed on 2026-10-15 08:17:18
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include "../src/SegLive.h" // the live-fraction rule (shared with Finalize's vs-segment page)
0014 #include <TH1D.h>
0015 #include <TObjString.h>
0016 #include <TObjArray.h>
0017 #include <TSystem.h>
0018 #include <cmath>
0019
0020
0021 void make_bad_segments(const char* setlist = "ana573_795xx,ana573_8156x,ana573_8158x", bool write = true){
0022 const double BANDW = 0.02, LOWMIN = 0.005, LOWNS = 4.;
0023 TString ldir = gSystem->DirName(gSystem->UnixPathName(__FILE__));
0024 TObjArray *ts = TString(setlist).Tokenize(",");
0025 const int NS = ts->GetEntries();
0026 std::vector<TString> setName, files;
0027 for (int s=0;s<NS;s++){
0028 TString sn = ((TObjString*)ts->At(s))->GetName(); setName.push_back(sn);
0029 for (int c=0;c<1000;c++){ TString p = ldir+"/../root/"+sn+Form("/chunks/corral_%02d.root",c); if (gSystem->AccessPathName(p)) break; files.push_back(p); }
0030 }
0031 if (files.empty()){ printf("no chunk files\n"); return; }
0032 SegLiveResult SL = SegLiveFractions(files);
0033 const bool feeMode = SL.feeMode;
0034 auto &live = SL.live;
0035 printf("%zu chunk files, cells %s\n",files.size(),feeMode ? "per FEE" : "generic grid");
0036 printf("%zu segments, %d never-live cells\n",live.size(),SL.nNever);
0037
0038 std::map<int,std::vector<std::pair<int,std::array<float,3>>>> byRun;
0039 for (auto &p : live) byRun[p.first.first].push_back({p.first.second,p.second});
0040 std::vector<TString> header; std::vector<std::pair<int,int>> low;
0041 for (auto &R : byRun){
0042 std::array<double,3> thr;
0043 for (int a=0;a<3;a++){
0044 TH1D hb("hb","",400,0.6,1.0); for (auto &s : R.second) hb.Fill(std::min(0.99999,(double)s.second[a]));
0045 double mode = hb.GetBinCenter(hb.GetMaximumBin()), s1 = 0, s2 = 0; int n = 0;
0046 for (auto &s : R.second){ double v = s.second[a]; if (fabs(v-mode)<BANDW){ s1 += v; s2 += v*v; n++; } }
0047 double sig = n>1 ? sqrt(std::max(0.,s2/n-pow(s1/n,2))) : 0.;
0048 thr[a] = mode - std::max(LOWMIN,LOWNS*sig);
0049 header.push_back(Form("# band run %d view %s mode %.4f sigma %.4f thr %.4f",R.first,a==0?"all":a==1?"S":"N",mode,sig,thr[a]));
0050 }
0051 int nl = 0;
0052 for (auto &s : R.second){ bool l = false; for (int a=0;a<3;a++) if (s.second[a]<thr[a]) l = true; if (l){ low.push_back({R.first,s.first}); nl++; } }
0053 printf("run %d: %d of %zu segments low (%.1f%%)\n",R.first,nl,R.second.size(),100.*nl/R.second.size());
0054 }
0055
0056 printf("%zu of %zu segments low\n",low.size(),live.size());
0057 if (!write){ printf("write = false: no bad_segments.txt written\n"); return; }
0058 std::vector<TString> dirs = setName; if (NS>1) dirs.push_back("ana573");
0059 for (auto &d : dirs){
0060 if (gSystem->AccessPathName(ldir+"/"+d+"/manifest.txt")) continue;
0061 TString fn = ldir+"/"+d+"/bad_segments.txt";
0062 FILE *fo = fopen(fn.Data(),"w");
0063 fprintf(fo,"# run segment -- below the run's live-fraction band in any view (all/S/N): live < mode - max(%.3f, %.0f sigma), band = mode +- %.2f\n",LOWMIN,LOWNS,BANDW);
0064 fprintf(fo,"# make_bad_segments.C(\"%s\"), %s cells, %zu of %zu segments; read by corral \"segveto\"\n",setlist,feeMode ? "FEE" : "generic",low.size(),live.size());
0065 for (auto &h : header) fprintf(fo,"%s\n",h.Data());
0066 for (auto &l : low) fprintf(fo,"%d %d\n",l.first,l.second);
0067 fclose(fo); printf("wrote %s\n",fn.Data());
0068 }
0069 }