127{
  130 
  133 
  135  string          usr;
  136  string          pwd;
  137  string          cookie;
  138  string          inputFile;
  140  string          detid;
  141  JRange_t        runs;
  143  JCondition_t    condition;
  144  JCondition_t    veto;
  145  TRegexp         regexp(".");
  146  JSelector       selection;
  148 
  149  try {
  150 
  151    JParser<> zap(
"Main program to evaluate quality data from data base."\
 
  152                  "\nThe condition and veto can be any TFormula compatible expression involving QA/QC parameters (see e.g. JQAQC.sh -h).");
  153    
  158    zap[
'f'] = 
make_field(inputFile,  
"Optional input file instead of database.")       = 
"";
 
  159    zap[
'o'] = 
make_field(
outputFile, 
"ROOT file with histograms and n-tuple or ASCII file with QA/QC data.") = 
"quality.root";
 
  161    zap[
'R'] = 
make_field(runs,       
"Run range")                                      = JRange_t(1, JRange_t::getMaximum());
 
  162    zap[
'S'] = 
make_field(source,     
"GIT versions")                                   = 
getGITTags(TRegexp(
"v[0-9]*\\.[0-9]*\\.[0-9]*$"), JGITTags_t::key_type(
"2019-04-12"));
 
  168 
  169    zap(argc, argv);
  170  }
  171  catch(const exception &error) {
  172    FATAL(error.what() << endl);
 
  173  }
  174 
  175 
  176  double W = 0.0;
  177  
  178  for (JCondition_t::const_iterator i = condition.begin(); i != condition.end(); ++i) {
  179    W += i->weight;
  180  }
  181 
  182  if (getFilenameExtension(
outputFile) == ROOT_FILE_FORMAT) {
 
  183    if (W <= 0.0) {
  184      FATAL(
"Invalid total weight: " << W << endl);
 
  185    }
  186  }
  187 
  188 
  191 
  192 
  193  if (inputFile == "") {                    
  194 
  196 
  197    try {
  198 
  199      JDB::reset(usr, pwd, cookie);
  200      
  201      const int ID = getDetector<int>   (detid);
 
  202      detid        = getDetector<string>(detid);
  203 
  204      
  205 
  207 
  208      NOTICE(
"Extracting run information from database... " << flush);
 
  209 
  210      ResultSet& rs  = 
getResultSet(getTable<JRuns>(), selection);
 
  211 
  212      for (
JRuns parameters; rs >> parameters; ) {
 
  213        if (TString(parameters.RUNSETUPNAME.c_str()).Contains(regexp)) {
  214          setups.
put(parameters);
 
  215        }
  216      }
  217 
  218      rs.Close();
  219 
  221 
  222      
  223 
  224      for (vector<string>::const_iterator git = source.begin(); git != source.end(); ++git) {
  225 
  228 
  229        map_type zmap;
  230 
  232 
  233        selector.add(&JRunSummaryNumbers::SOURCE_NAME, *git);
  234 
  235        try {
  236 
  237          NOTICE(
"Extracting run summmary information with source " << *git << 
" from database... " << flush);
 
  238 
  239          ResultSet& rs  = 
getResultSet(getTable<JRunSummaryNumbers>(), selector);
 
  240 
  242            if (setups.
has(parameters.RUN)) {
 
  243              zmap[parameters.RUN].insert(make_pair(parameters.PARAMETER_NAME, parameters.DATA_VALUE));
  244            }
  245          }
  246 
  247          rs.Close();
  248 
  250        }
  251        catch(
const exception& error) { 
NOTICE(endl); }
 
  252 
  253        for (map_type::const_iterator run = zmap.begin(); run != zmap.end(); ++run) {
  254 
  256          
  259          quality.
run      = run->first;
 
  260          quality.
name     = setups[run->first].name;
 
  261          quality.
value    = setups[run->first].value;
 
  262 
  263          for (data_type::const_iterator i = run->second.begin(); i != run->second.end(); ++i) {
  264            quality.
put(i->first, i->second);
 
  265          }
  266 
  268 
  269          buffer.insert(quality);           
  270        }
  271      }
  272    }
  273    catch(const exception& error) {
  274      FATAL(error.what() << endl);
 
  275    }
  276 
  277  } else {                                  
  278 
  280      WARNING(
"Detector identifier \"" << detid << 
"\" discarded." << endl);
 
  281    }
  282 
  283    const int ID = (
is_integer(detid) ? to_value<int>(detid) : -1);
 
  284 
  286 
  288 
  289    in >> comment;
  290 
  292      if (
ID == -1 || 
ID == quality.detector) {
 
  293        if (runs(quality.run)) {
  294          buffer.insert(quality);
  295        }
  296      }
  297    }
  298 
  299    in.close();
  300  }
  301 
  302 
  303  if (buffer.empty()) {
  304    FATAL(
"No data." << endl);
 
  305  }
  306 
  307  if (
debug >= debug_t) {
 
  308 
  310 
  312      writer.put(*quality);
  313    }
  314  } 
  315 
  316 
  317  if (getFilenameExtension(
outputFile) == ROOT_FILE_FORMAT) {
 
  318 
  319    runs = JRange_t(buffer.begin()->run, buffer.rbegin()->run);
  320 
  321    TH1D h0("h0", NULL, 1000, 0.0, 1.01);
  322    TH1D h1("h1", NULL, condition.size(), -0.5, condition.size() - 0.5);
  323 
  324    for (size_t i = 0; i != condition.size(); ++i) {
  325      h1.GetXaxis()->SetBinLabel(i+1, condition[i].formula.c_str());
  326    }
  327 
  328    h0.SetMinimum(0.0);
  329    h0.SetMaximum(1.1 * (runs.getUpperLimit() - runs.getLowerLimit() + 1));
  330 
  331    h1.SetMinimum(0.0);
  332    h1.SetMaximum(1.1 * (runs.getUpperLimit() - runs.getLowerLimit() + 1));
  333 
  334    const Double_t 
xmin = runs.getLowerLimit() - 0.5;
 
  335    const Double_t 
xmax = runs.getUpperLimit() + 0.5;
 
  336 
  337    JManager<TString, TH1D> H1(new TH1D("%", NULL, runs.getLength() + 1, xmin, xmax));
  338 
  339    
  340 
  341    for (JCondition_t::const_iterator i = condition.begin(); i != condition.end();++i) {
  342      H1[i->formula]->GetListOfFunctions()->Add(
new TF1(
MAKE_CSTRING(
"C " << i->formula << 
":upper"), 
MAKE_CSTRING(i->range.getUpperLimit()), xmin, xmax));
 
  343      H1[i->formula]->GetListOfFunctions()->Add(
new TF1(
MAKE_CSTRING(
"C " << i->formula << 
":lower"), 
MAKE_CSTRING(i->range.getLowerLimit()), xmin, xmax));
 
  344    }
  345 
  346    for (JCondition_t::const_iterator i = veto.begin(); i != veto.end();++i) {
  347      H1[i->formula]->GetListOfFunctions()->Add(
new TF1(
MAKE_CSTRING(
"V " << i->formula << 
":upper"), 
MAKE_CSTRING(i->range.getUpperLimit()), xmin, xmax));
 
  348      H1[i->formula]->GetListOfFunctions()->Add(
new TF1(
MAKE_CSTRING(
"V " << i->formula << 
":lower"), 
MAKE_CSTRING(i->range.getLowerLimit()), xmin, xmax));
 
  349    }
  350 
  351    ostringstream os;                       
  352 
  353    os << "run";
  354 
  355    for (size_t i = 0; i != condition.size(); ++i) {
  356      os << ":" << (char) ('a' + i);
  357    }
  358 
  359    os << ":Q:V:R";
  360 
  361    TNtuple n1("n1", "quality", os.str().c_str());
  362 
  363 
  364    
  365 
  367 
  369 
  370      double w = 0.0;
  371 
  372      for (size_t i = 0; i != condition.size(); ++i) {
  373 
  374        const JCondition& ps = condition[i];
  375        const double      y  = 
getResult(ps.formula, *quality);
 
  376 
  377        DEBUG(ps.formula << 
' ' << y << endl);
 
  378 
  379        tuple.push_back((Float_t) y);
  380 
  381        TH1D* p = H1[ps.formula];
  382 
  383        p->SetBinContent(p->FindBin((double) quality->run), y);
  384        p->SetBinError  (p->FindBin((double) quality->run), numeric_limits<double>::epsilon());
  385 
  386        if (ps.range(y)) {
  387          w += ps.weight;
  388        }
  389 
  390        h1.AddBinContent(i + 1, ps.range(y) ? 1.0 : 0.0);
  391      }
  392 
  393      const double Q = w/W;
  394 
  395      tuple.push_back((Float_t) Q);
  396 
  397      int V = 0;
  398 
  399      for (size_t i = 0; i != veto.size(); ++i) {
  400 
  401        const JCondition& ps = veto[i];
  402        const double      y  = 
getResult(ps.formula, *quality);
 
  403        
  404        DEBUG(ps.formula << 
' ' << y << endl);
 
  405 
  406        TH1D* p = NULL;
  407 
  408        p = H1[ps.formula];
  409 
  410        p->SetBinContent(p->FindBin((double) quality->run), y);
  411        p->SetBinError  (p->FindBin((double) quality->run), numeric_limits<double>::epsilon());
  412 
  414 
  415        p->SetBinContent(p->FindBin((double) quality->run), ps.range(y) ? 0.0 : 1.0);
  416        p->SetBinError  (p->FindBin((double) quality->run), numeric_limits<double>::epsilon());
  417 
  418        if (!ps.range(y)) {
  419          ++V;
  420        }
  421      }
  422 
  423      tuple.push_back((Float_t) V);
  424      tuple.push_back((Float_t) setups.
get(quality->run));
 
  425 
  426      h0.Fill(Q);
  427      n1.Fill(tuple.data());
  428 
  429      cout << setw(8) << quality->run << 
' ' << 
FIXED(5,3) << Q << 
' ' << setw(2) << V << endl;
 
  430    }
  431 
  432    double w = 0.0;
  433 
  434    for (Int_t i = 0; i <= h0.GetXaxis()->GetNbins(); ++i) {
  435      h0.SetBinContent(i, (w += h0.GetBinContent(i)));
  436    }
  437 
  438    
  439 
  441 
  442    out << h0 << h1 << n1 << H1;
  443    
  444    out.Write();
  445    out.Close();
  446  }
  447 
  448 
  449  if (getFilenameExtension(
outputFile) == ASCII_FILE_FORMAT) {
 
  450 
  451    
  452 
  454 
  455    out.setf(ios::fixed);
  456 
  458 
  460 
  461    out << comment;
  462 
  464      out << *i << endl;
  465    }
  466 
  467    out.close();
  468  }
  469}
#define DEBUG(A)
Message macros.
 
#define ASSERT(A,...)
Assert macro.
 
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
 
TString replace(const TString &target, const TRegexp ®exp, const T &replacement)
Replace regular expression in input by given replacement.
 
#define MAKE_CSTRING(A)
Make C-string.
 
#define MAKE_STRING(A)
Make string.
 
Object reading from ASCII file.
 
Object(s) writing to ASCII file.
 
Simple data structure to support I/O of equations (see class JLANG::JEquation).
 
Utility class to parse command line options.
 
Implementation for ASCII output of objects with ROOT dictionary.
 
ResultSet & getResultSet(const std::string &query)
Get result set.
 
std::vector< JServer > getServernames()
Get list of names of available database servers.
 
std::vector< std::string > getGITTags(const TRegexp ®exp, const JGITTags_t::key_type &date)
Get selection of GIT tags.
 
Double_t getResult(const TString &text, TObject *object=NULL)
Get result of given textual formula.
 
bool is_integer(const std::string &buffer)
Check if string is an integer.
 
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
 
Auxiliary data structure for floating point format specification.
 
Data structure for measured coincidence rates of all pairs of PMTs in optical module.
 
Auxiliary data structure for data quality.
 
double value
setup identifier (see JRunsetups)
 
void put(const std::string &key, const std::string &value)
Put value at given key.
 
std::string name
setup name (see JRunsetups)
 
std::string GIT
GIT version used to write QA/QC data.
 
int detector
detector identifier
 
Auxiliary class for run setup evaluation.
 
void put(const int run, const std::string setup)
Put run parameters.
 
bool has(const int run) const
Check if run setup is vailable.
 
float get(const int run) const
Get run setup value.
 
Wrapper class for server name.
 
Template definition for getting table specific selector.
 
Empty structure for specification of parser element that is initialised (i.e. does not require input)...