#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <DAQ++/DAQmanager.h>
#include <daq/GenModule.h>
#include <data/Analysis.h>
#include <config/ConfigParser.h>
#include <config/ModuleParser.h>
#include "gtkutils.h"
#include "Pedestal.h"
void PedestalGui::auto_connect()
{
SET_CONNECT( onOK );
SET_CONNECT( onCancel );
SET_CONNECT( onOpen );
SET_CONNECT( onHelp );
SET_CONNECT( onReset );
SET_CONNECT( onSelectModule );
}
void PedestalGui::user_init()
{
// Prepare the option menu
GtkWidget *menu, *item, *optmenu;
optmenu = get_widget("comboModules");
gtk_option_menu_remove_menu(GTK_OPTION_MENU(optmenu));
menu = gtk_menu_new();
gtk_label_set_text(GTK_LABEL(get_widget("lblPedMsg")), msg.c_str());
DAQpp::DAQmanager *daq_manager = DAQpp::DAQmanager::theDAQmanager();
DAQpp::ModulesMap::const_iterator mm;
for (mm = daq_manager->ModuleMap().begin();mm != daq_manager->ModuleMap().end();++mm)
{
GenModule *md = dynamic_cast<GenModule *>(mm->second);
if ( md )
{
item = gtk_menu_item_new_with_label( md->get_id() );
gtk_widget_show(item);
g_object_set_data(G_OBJECT(item), "module", (gpointer)md);
gtk_menu_append(GTK_MENU(menu), item);
}
}
gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu), menu);
gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu), 0);
onSelectModule(0, this);
}
void PedestalGui::onHelp(GtkWidget *, PedestalGui *P)
{
gtk_help_display("pedestals");
}
void PedestalGui::onReset(GtkWidget *, PedestalGui *P)
{
// Find which module is selected
GenModule *md = P->find_module();
if (!md)
return;
if ( P->change_module() )
{
int i, sz;
std::vector<double> &ped = md->get_par_value< std::vector<double> > ("pedestal");
std::vector<double> &noise = md->get_par_value< std::vector<double> > ("noise");
sz = ped.size();
for (i = 0;i < sz;i++)
{
ped[i] = noise[i] = 0.;
}
}
else
{
int i, sz;
double *ped = md->get_analysis()->Ped();
double *noise = md->get_analysis()->Noise();
sz = md->get_analysis()->Ntot();
for (i = 0;i < sz;i++)
{
ped[i] = noise[i] = 0.;
}
}
}
void PedestalGui::onCancel(GtkWidget *, PedestalGui *P)
{
delete P;
}
#define read_pedestals(ifile, ped, noise, sz) { \
int i; \
for (i=0;ifile && !ifile.eof() && i<sz;i++) { \
ifile >> ped[i] >> noise[i]; \
} \
}
#define write_pedestals(ofile,ped,noise,sz) { \
int i; \
for (i=0;i<sz;i++) { \
ffile << ped[i] << " " << noise[i] << std::endl; \
} \
}
void PedestalGui::onOK(GtkWidget *, PedestalGui *P)
{
std::string ofile = gtk_entry_get_text(GTK_ENTRY(P->get_widget("entryFilename")));
// Find which module is selected
ModuleParser *mp;
GenModule *md = P->find_module(&mp);
if (!md)
return;
// Find what do we want
if (ofile.empty() && mp)
{
mp->set_pedestal_file(ofile);
onCancel(NULL, P);
return;
}
try
{
int ich, sz;
std::vector<double> &ped = md->get_par_value< std::vector<double> > ("pedestal");
std::vector<double> &noise = md->get_par_value< std::vector<double> > ("noise");
if ( P->for_read )
{
std::ifstream ifile(ofile.c_str());
if (!ifile)
{
std::cout << "Could not load pedestals from " << ofile << std::endl;
onCancel(0,P);
return;
}
/** Do we really need to separate module and monitor ?
* Ignore the different by now.
*/
sz = ped.size();
read_pedestals(ifile, ped, noise, sz);
md->get_analysis()->trust_pedestals();
for (ich=0;ich<sz;ich++)
{
md->get_analysis()->Ped()[ich] = ped[ich];
md->get_analysis()->Noise()[ich] = noise[ich];
}
md->get_analysis()->trust_pedestals();
// if ( P->change_module() )
// {
// sz = ped.size();
// read_pedestals(ifile, ped, noise, sz);
// md->get_analysis()->trust_pedestals();
// }
// else
// {
// sz = md->get_analysis()->Ntot();
// read_pedestals(ifile, (md->get_analysis()->Ped()), (md->get_analysis()->Noise()), sz);
// }
ifile.close();
if (mp)
mp->set_pedestal_file(ofile);
}
else
{
std::ofstream ffile(ofile.c_str());
if (!ffile)
{
std::cout << "Could not open pedestal file for output: " << ofile << std::endl;
}
if ( P->change_module() )
{
sz = ped.size();
write_pedestals(ifile, ped, noise, sz);
}
else
{
sz = md->get_analysis()->Ntot();
write_pedestals(ifile, (md->get_analysis()->Ped()), (md->get_analysis()->Noise()), sz);
}
ffile.close();
}
}
catch ( DAQpp::Exception &ex)
{
std::cout << ex.what() << std::endl;
}
onCancel(NULL, P);
}
void PedestalGui::onOpen(GtkWidget *, PedestalGui *P)
{
const char *filters[] = {"Pedestals|*.ped", "All|*" ,0};
std::string ofile = ::get_file_name( (P->for_read ? "Load Pedestals" : "Save Pedestals"),
(P->for_read ? a_OpenFile : a_SaveFile),
0,
filters);
if (!ofile.empty())
gtk_entry_set_text(GTK_ENTRY(P->get_widget("entryFilename")),
ofile.c_str());
}
GenModule *
PedestalGui::find_module(ModuleParser **mp)
{
// Find which module is selected
GtkOptionMenu *option_menu = GTK_OPTION_MENU(get_widget("comboModules"));
GtkWidget *item = gtk_menu_get_active(GTK_MENU(option_menu->menu));
GenModule *md = (GenModule *)g_object_get_data(G_OBJECT(item), "module");
if (mp)
{
ConfigParser *cfg = ConfigParser::theConfig();
ModuleParser *hook = cfg->get_id_handler(md->get_id());
*mp = hook;
}
return md;
}
bool
PedestalGui::change_module()
{
gboolean from_module =
gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(get_widget("pedFromModule")));
return from_module;
}
void
PedestalGui::onSelectModule(GtkWidget *w, PedestalGui *P)
{
GenModule *module;
ModuleParser *mp=0;
module = P->find_module(&mp);
std::cout << "selecting module: " << module->get_id() << std::endl;
if (mp)
{
if (!mp->get_pedestal_file().empty())
{
gtk_entry_set_text(GTK_ENTRY(P->get_widget("entryFilename")),
mp->get_pedestal_file().c_str());
gtk_widget_queue_draw(P->get_widget("entryFilename"));
}
std::cout << "Pedestal file: " << mp->get_pedestal_file() <<std::endl;
}
}