run_info.cc

Go to the documentation of this file.
00001 
00006 #include "Reader.hh"
00007 #include "EventHandler.hh"
00008 #include "ConvertData.hh"
00009 #include "ConfigHandler.hh"
00010 #include "CommandSwitchFunctions.hh"
00011 #include "phrase.hh"
00012 #include <iostream>
00013 #include <ctime>
00014 #include <cstdio>
00015 #include <cstdlib>
00016 #include "Message.hh"
00017 #include "runinfo.hh"
00018 
00019 using namespace std;
00020 
00022 void PrintFileInfo(const char* fname, EventHandler* modules, 
00023                    const string& runinfo_file="", const string& comment="",
00024                    char answer=0,
00025                    bool force_regen = false,
00026                    bool db_only = false)
00027 {
00028   cout<<"Processing runinfo for file "<<fname<<"..."<<endl;
00029   Reader reader(fname);
00030   if(!reader.IsOk())
00031     return;
00032   
00033  
00034   runinfo* info = modules->GetRunInfo();
00035   if( ConfigHandler::GetInstance()->LoadParameterList(info) ){
00036     //couldn't find it in saved config, so see if there's a default file
00037     if(runinfo_file != ""){
00038       cout<<"Loading runinfo from file "<<runinfo_file<<endl;
00039       info->ReadFromFile(runinfo_file.c_str());
00040     }
00041   }
00042     
00043   if(comment != "") info->SetMetadata("comment",comment);
00044   if(force_regen || info->runid <=0 || 
00045      info->starttime == 0 || info->endtime == 0 ||
00046      info->events == 0 ){
00047     //process the first event
00048     //don't read from the database
00049     modules->AllowDatabaseAccess(false);
00050     modules->Initialize();
00051     RawEventPtr event = reader.GetEventWithIndex(0);
00052     if(modules->Process(event)){
00053       Message(ERROR)<<"Problem processing event from file "<<fname<<endl;
00054       return;
00055     }
00056     //process the last event
00057     event = reader.GetLastEvent();
00058     if(modules->Process(event)){
00059       Message(ERROR)<<"Problem processing event from file "<<fname<<endl;
00060       return;
00061     }
00062     
00063     //Finalize erases the info, so copy it
00064     info = new runinfo(*info);
00065     modules->Finalize();
00066   }
00067   //print the results
00068   if(!db_only){
00069     cout<<"Run information for file "<<fname<<":\n";
00070     cout<<*info<<endl;
00071   }
00072   //ask if we want to insert into the database
00073   while(answer != 'Y' && answer != 'y' && answer != 'N' && answer != 'n' &&
00074         answer != 'E' && answer != 'e'){
00075     if(answer)
00076       cout<<"'"<<answer<<"' is not a valid response!"<<endl;
00077     cout<<"Insert this run into the database? ([Y]es/[N]o/[E]dit)"<<endl;
00078     cin>>answer;
00079   }
00080   switch(answer){
00081   case 'N':
00082   case 'n':
00083     break;
00084   case 'E':
00085   case 'e':
00086     {
00087       char tempname[] = "/tmp/runinfoXXXXXX";
00088       int error = mkstemp(tempname);
00089       if(error > 0){
00090         info->SaveToFile(tempname);
00091         stringstream cmd;
00092         cmd<<"emacs "<<tempname;
00093         error = system(cmd.str().c_str());
00094         info->ReadFromFile(tempname);
00095         remove(tempname);
00096       }
00097     }
00098   case 'Y':
00099   case 'y':
00100     if(info->GetMetadata("comment") == ""){
00101       cout<<"Please enter a comment for this run:"<<endl;
00102       cin.ignore(100,'\n');
00103       std::string c;
00104       getline(cin, c);
00105       info->SetMetadata("comment",c);
00106     }
00107     //info->InsertIntoDatabase();
00108     break;
00109   default:
00110     cerr<<"Something weird happened..."<<endl;
00111   }
00112   
00113     
00114 }
00115   
00116 int main(int argc, char** argv)
00117 {
00118   ConfigHandler* config = ConfigHandler::GetInstance();
00119   config->SetProgramUsageString("run_info [options] <file1> [<file2> ...]");
00120   string runinfo_file="";
00121   std::string comment="";
00122   char query_answer=0;
00123   bool force_regen = false;
00124   bool db_only = false;
00125   config->AddCommandSwitch('i',"info-file", "Read the default run info from <file> if not found in the run cfg file",
00126                            CommandSwitch::DefaultRead<string>(runinfo_file),
00127                            "file");
00128   config->AddCommandSwitch('m',"message",
00129                            "Set the database comment on this run to <comment>",
00130                            CommandSwitch::DefaultRead<string>(comment),
00131                            "comment");
00132   config->AddCommandSwitch('a',"answer","Skip interactive query for database insertion; value can be 'y', 'n', or 'e'(edit)",
00133                            CommandSwitch::DefaultRead<char>(query_answer),
00134                            "answer");
00135   config->AddCommandSwitch('f',"force","Force regenerate info from raw data",
00136                            CommandSwitch::SetValue<bool>(force_regen,true));
00137   config->AddCommandSwitch(' ',"db-only",
00138                            "Don't print run info, just insert into database",
00139                            CommandSwitch::SetValue<bool>(db_only,true));
00140                            
00141                 
00142   if(config->ProcessCommandLine(argc, argv))
00143     return -1;
00144   if(config->GetNCommandArgs()==0)
00145     config->PrintSwitches(true);
00146   EventHandler* modules = EventHandler::GetInstance();
00147   modules->AddModule<ConvertData>();
00148   //cout<<"runid\tstarttime\tendtime\tevents\tlivetime\n";
00149   for(int i=1; i < argc; i++){
00150     PrintFileInfo(argv[i], modules, runinfo_file,comment,query_answer,
00151                   force_regen, db_only);
00152   }
00153   return 0;
00154 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines

Generated on 20 Jun 2014 for daqman by  doxygen 1.6.1