Ignore:
Timestamp:
07/17/08 16:08:04 (16 years ago)
Author:
tbretz
Message:
*** empty log message ***
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/MagicSoft/Mars/datacenter/macros/doexclusions.C

    r7777 r9006  
    5959#include <iostream>
    6060#include <iomanip>
    61 #include <fstream>
    62 
    63 #include <MSQLServer.h>
     61
     62#include <TEnv.h>
     63#include <TSystem.h>
     64
    6465#include <TSQLRow.h>
    6566#include <TSQLResult.h>
    6667
    67 #include <TEnv.h>
    68 #include <TSystem.h>
     68#include "MSQLMagic.h"
    6969
    7070using namespace std;
    7171
    7272//get minimum or maximum runnumber of the runs of a night
    73 int GetRunNumber(MSQLServer &serv, TString date, TString value)
     73int GetRunNumber(MSQLServer &serv, Int_t tel, TString date, TString cmd)
    7474{
    75     TString query(Form("SELECT %s(fRunNumber) FROM RunData ", value.Data()));
     75    TString query;
     76    query  = Form("SELECT %s(fRunNumber) FROM RunData ", cmd.Data());
     77    query += Form("WHERE fTelescopeNumber=%d", tel);
    7678
    7779    if (date!="NULL")
    7880    {
    7981        TString day=date+" 13:00:00";
    80         query+=Form(" WHERE (fRunStart>ADDDATE(\"%s\", INTERVAL -1 DAY) AND fRunStart<\"%s\")",
    81                     day.Data(), day.Data());
    82     }
    83 
    84     cout << "query: " << query << endl;
     82        query += Form(" AND (fRunStart>ADDDATE(\"%s\", INTERVAL -1 DAY) AND fRunStart<\"%s\")",
     83                      day.Data(), day.Data());
     84    }
    8585
    8686    TSQLResult *res = serv.Query(query);
    8787    if (!res)
    8888    {
    89         cout << "Error - could not get run#" << endl;
     89        cout << "ERROR - Could not get " << cmd << " fRunNumber." << endl;
    9090        return -1;
    9191    }
    9292
    9393    TSQLRow *row =res->Next();
    94     cout << (void*)row << endl;
    9594    if (TString((*row)[0]).IsNull())
    9695    {
    97         cout << "No run available for this date" << endl;
     96        cout << "No run available for " << date << endl;
    9897        delete res;
    9998        return 0;
    10099    }
    101100    delete res;
     101
    102102    return atoi((*row)[0]);
    103103}
    104104
    105 //get part of a query (left join of tables)
    106 TString GetJoin(TString table)
     105int doexclusions(Int_t startrun, Int_t stoprun, Int_t tel=1, TString date="NULL", Bool_t dummy=kTRUE)
    107106{
    108     TString query(Form("left join %s ON RunData.f%sKEY=%s.f%sKEY ",
    109                        table.Data(), table.Data(), table.Data(), table.Data()));
    110     return query;
    111 }
    112 
    113 int doexclusions(Int_t startrun, Int_t stoprun, TString date="NULL")
    114 {
    115     TEnv env("sql.rc");
    116     TEnv rc("automatic-exclusions.rc");
    117 
    118     MSQLServer serv(env);
     107    MSQLMagic serv("sql.rc");
    119108    if (!serv.IsConnected())
    120109    {
     
    123112    }
    124113
     114    // Open rc files with exclusions
     115    TEnv rc("resources/exclusions.rc");
     116
     117    // Some information for the user
    125118    cout << "doexclusions" << endl;
    126119    cout << "------------" << endl;
    127120    cout << endl;
    128121    cout << "Connected to " << serv.GetName() << endl;
    129 
    130     //if neither start- nor stoprun is given, the minimum and maximum runnumber
    131     // is queried from the database to do the exclusions for all runs
    132     // if a night is given for all runs of this night
     122    cout << "Date:      " << date << endl;
     123
     124    serv.SetIsDummy(dummy);
     125
     126    // if neither start- nor stoprun is given, the minimum and maximum
     127    // runnumber is queried from the database to do the exclusions for
     128    // all runs if a night is given for all runs of this night
    133129    if (startrun==0 && stoprun==0)
    134130    {
    135         startrun=GetRunNumber(serv, date, "min");
    136         stoprun=GetRunNumber(serv, date, "max");
    137     }
     131        startrun = GetRunNumber(serv, tel, date, "MIN");
     132        stoprun  = GetRunNumber(serv, tel, date, "MAX");
     133    }
     134
     135    cout << "Start Run: " << startrun << endl;
     136    cout << "Stop  Run: " << stoprun  << endl;
     137
    138138    //check format of start- and stoprun
    139139    if (startrun<0 || stoprun<0)
    140140    {
    141         cout << "wrong format of runno" << endl;
     141        cout << "ERROR - Startrun<0 or stoprun<0." << endl;
    142142        return 2;
    143143    }
     144
    144145    //if no run for date is available, GetRunNumber() returns 0
    145146    if (startrun==0 || stoprun==0)
    146147        return 1;
    147148
    148     //get the condition for the runnumber range
    149     TString runcond(Form("AND fRunNumber BETWEEN %d AND %d ", startrun, stoprun));
    150 
    151     //get exclusions-reasons (stored in the table ExcludedFDA) from the database
    152     //the exclusions which can be done automatically are marked with the flag fExcludedFDAAutomatic='yes'
    153     //and with an importance (one run may be excluded for several reasons,
    154     //the reason is chosen according to the importance)
    155     TString query="SELECT fExcludedFDAKEY from ExcludedFDA where fExcludedFDAAutomatic='yes'";
    156     TSQLResult *res = serv.Query(query);
     149    // Get exclusions-reasons (stored in the table ExcludedFDA) from the DB
     150    TSQLResult *res = serv.Query("SELECT fExcludedFDAKEY, fExcludedFDAName "
     151                                 "FROM ExcludedFDA "
     152                                 "ORDER BY fExcludedFDAImportance ASC");
    157153    if (!res)
    158     {
    159         cout << "Error - could not do any automatic excludes." << endl;
    160154        return 2;
    161     }
    162155
    163156    //update the exclusion-reasons for all runs
     
    165158    while ((row = res->Next()))
    166159    {
    167         //read in values from the resource file automatic-exclusions (explanation see above)
    168         TString key=(*row)[0];
    169         TString column=rc.GetValue("key"+key+".Column", "");
    170         TString join1=rc.GetValue("key"+key+".Join1", "");
    171         TString join2=rc.GetValue("key"+key+".Join2", "");
    172         TString border=rc.GetValue("key"+key+".SpecialRunCond", "");
    173 
    174         //get importance of exclusion-reason from database
    175         TString query(Form("SELECT fExcludedFDAImportance from ExcludedFDA where fExcludedFDAKEY=%s ", key.Data()));
    176         TSQLResult *res2 = serv.Query(query);
     160        // check the key (NULL means "No exclusion")
     161        const TString key = (*row)[0];
     162        if (key.IsNull())
     163            continue;
     164
     165        // Get the corresponding condition from the file
     166        const TString cond = rc.GetValue("key"+key, "");
     167        if (cond.IsNull())
     168            continue;
     169
     170        // Get all files to be excluded
     171        TString query2 = "SELECT fRunNumber, fFileNumber FROM RunData ";
     172        query2 += serv.GetJoins("RunData", query2+cond);
     173
     174        query2 += Form("WHERE (%s) ", cond.Data());
     175        query2 += Form("AND fRunNumber BETWEEN %d AND %d ", startrun, stoprun);
     176        query2 += Form("AND fTelescopeNumber=%d", tel);
     177
     178        TSQLResult *res2 = serv.Query(query2);
    177179        if (!res2)
    178         {
    179             cout << "Error - could not get importance." << endl;
    180180            return 2;
    181         }
    182 
    183         TSQLRow *row2=res2->Next();
    184         Int_t newimp=atoi((*row2)[0]);
    185         delete res2;
    186 
    187         //get current importance from database
    188         //for the runs which match the exclusion-reason
    189         query="SELECT fRunNumber, fExcludedFDAImportance ";
    190         if (!column.IsNull())
    191             query+=Form(", %s", column.Data());
    192         if (!join1.IsNull())
    193             query+=Form(", f%sName", join1.Data());
    194         if (!join2.IsNull())
    195             query+=Form(", f%sName", join2.Data());
    196         query +=" FROM RunData ";
    197         query +=GetJoin("ExcludedFDA");
    198         if (!join1.IsNull())
    199             query+=GetJoin(join1.Data());
    200         if (!join2.IsNull())
    201             query+=GetJoin(join2.Data());
    202         query +=Form("WHERE (%s) ", rc.GetValue("key"+key+".Cond", ""));
    203         if (!border.IsNull())
    204             query+=Form(" AND fRunNumber BETWEEN IF(%s>%d, %d, %s) AND IF (%s<%d, %s, %d) ",
    205                         border.Data(), startrun, startrun, border.Data(),
    206                         border.Data(), stoprun, border.Data(), stoprun);
    207         else
    208             query +=runcond;
    209 
    210         cout << query << endl;
    211 
    212         res2 = serv.Query(query);
    213         if (!res2)
    214         {
    215             cout << "Error - no runs to exclude" << endl;
    216             return 2;
    217         }
    218 
    219         //compare new and old importance
    220         //change or keep the exclusion-reason accordingly
     181
     182        // Update exlcusion for file
     183        TSQLRow *row2=0;
    221184        while ((row2 = res2->Next()))
    222185        {
    223             if (TString((*row2)[1]).IsNull() || atoi((*row2)[1])>newimp)
    224             {
    225                 //change exclusion-reason
    226                 TString query(Form("UPDATE RunData SET fExcludedFDAKEY=%s WHERE fRunNumber=%s",
    227                                    key.Data(), (*row2)[0]));
    228                 cout << "QU: " << query << endl;
    229                 TSQLResult *res3 = serv.Query(query);
    230                 if (!res3)
    231                 {
    232                     cout << "Error - could not insert exclusion" << endl;
    233                     return 2;
    234                 }
    235                 delete res3;
    236                 continue;
    237             }
    238             //keep exclusion-reason
    239             cout << "run#: " << (*row2)[0] << " reason for exclusion is still the same" << endl;
     186            TString vars(Form("fExcludedFDAKEY=%s", key.Data()));
     187            TString where(Form("fRunNumber=%s AND fFileNumber=%s", (*row2)[0], (*row2)[1]));
     188
     189            if (serv.Update("RunData", vars, where)==kFALSE)
     190                return 2;
     191
     192            cout << "File M" << tel << ":" << (*row2)[0] << "/" << setw(3) << setfill('0') << (*row2)[1] << " excluded due to: " << (*row)[1] << "." << endl;
    240193        }
     194
    241195        delete res2;
    242196    }
    243197    delete res;
     198
    244199    return 1;
    245200}
    246201
    247202//run doexclusions for one night
    248 int doexclusions(TString date="NULL")
     203int doexclusions(TString date="NULL", Int_t tel=1, Bool_t dummy=kTRUE)
    249204{
    250     return doexclusions(0, 0, date);
     205    return doexclusions(0, 0, tel, date, dummy);
    251206}
Note: See TracChangeset for help on using the changeset viewer.