Bug #992
createStreamStructure
0%
Description
Nella fase di creazione dei pacchetti si verifica un errore di segmentation fault quando usata all'interno di un ciclo (a partire dalla seconda iterazione).
Riporto il codice in cui si è verificato l'errore:
/***************************************************************************//*
TELEMETRY GENERATOR From Montecarlo Simulated Data (first simulation)
-------------------
Author : Vito Conforti
email : conforti@iasfbo.inaf.it
date : 08 July 2013
*******************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
*
********************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include <sstream>
#include <iostream>
#include <stdlib.h>
#include "OutputSocketClient.h"
#include "OutputPacketStream.h"
#include "ByteStream.h"
#include "Output.h"
#include "OutputFile.h"
#include "Packet.h"
#include <fstream> // I/O on file
// include delle eccezioni
#include "PacketException.h"
#include "PacketExceptionFileFormat.h"
#include "PacketExceptionIO.h"
#include <limits>
#include <math.h>
#include <stdio.h>
#include <iomanip>
// include of time function
#include <ctime>
// tempi per le misure delle performance
#include <time.h>
#include <sys/time.h>
#include <mysql.h>
using namespace std;
using namespace PacketLib;
// for the execution:
// export LD_LIBRARY_PATH=/home/conforti/connettore/lib:$LD_LIBRARY_PATH
- @file main.cpp
- @brief this program create the raw files using Montecarlo Data
- @author Vito Conforti
/
string NumberToString ( int Number ){
ostringstream ss;
ss << Number;
return ss.str();
}
int StringToNumber ( const string &Text ) {
istringstream ss(Text);
int result;
return ss >> result ? result : 0;
}
void finish_with_error(MYSQL *con)
{
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
cout << " there is an error unknown with mysql " << endl;
exit(1);
}
/**
- \return true if the program ended correctly and it will store the telemetry file in output folder.
/
int main(int argc, char* argv) {
// time to measure the performance
timeval start, stop;
double elapsedTime;
cout << "*** ----- ASTRI MC TELEMETRY GENERATOR ----- *** " << endl;
// db connection
MYSQL_RES *result;
MYSQL_RES *result_event;
MYSQL_ROW row;
MYSQL_ROW row_events;
MYSQL *connection, mysql;
int state;
mysql_init(&mysql);
connection = mysql_real_connect(&mysql, "localhost","root","MYSQL01cst01","RootRaw",0,0,0);
if (connection == NULL) {
printf(mysql_error(&mysql));
return 1;
}
int mode = 1; // science = 1; Fiber cal = 2
int NPDMs = 37;
int elementsBlock = 130; // 64 pixel low gain, 64 pixel haig gain, 1 spare, 1 PDM id
// load simulated cameras
string querystring = "SELECT distinct tel_data FROM camera ORDER BY tel_data ";
mysql_query(&mysql, querystring.c_str() );
result = mysql_store_result(&mysql);
int camera[100]; // max 100 cameras is admitted
int index_camera = 0;
while((row = mysql_fetch_row(result))){
cout << "camera: " << row[0] << " "<< endl;
camera[index_camera] = atoi (row[0]);
index_camera++;
}
cout << "the cameras are: " << index_camera << endl;
int current_camera = 0;
string id_tel_string = "";
// one raw file for each camera will be created:
for (int id_tel = 0; id_tel < index_camera ; id_tel++){
// current camera
cout <<" camera corrente di 55 " << camera[id_tel] << endl;
current_camera = camera[id_tel];
cout <<" int current camera = " << current_camera << endl;
id_tel_string = static_cast<ostringstream*>( &(ostringstream() << current_camera ) )->str();
cout << "string current camera = " << id_tel_string << endl;
// data retrieval of the camera
querystring = "SELECT * FROM camera WHERE tel_data = " + id_tel_string; // seleziona tutti gli eventi della camera corrente
cout << "query camera " << querystring << endl;
mysql_query(&mysql, querystring.c_str() );
result = mysql_store_result(&mysql);
if (result == NULL) {
cout << "NON HA RITORNATO NULLA " << endl;
finish_with_error(&mysql);
}
int num_fields = mysql_num_fields(result);
cout << " numero di eventi per la camera corrente " << mysql_num_rows(result) << endl;
// WRITE RAW FILE
Output* out;
OutputPacketStream ops;
//ops.setFileNameConfig("conf/STRUCT.stream");
ops.setFileNameConfig("/home/conforti/Projects/ASTRICameraMCSimulator/conf/STRUCT.stream");
ops.createStreamStructure();--
char** param = (char**) new char*[3]; ///> array declaration
// the telemetry will be saved on a file
out = (Output*) new OutputFile(ops.isBigEndian());
string destination = "output/MC_raw_data_camera_" + id_tel_string + ".raw";
char* destinationPath = (char*) destination.c_str();
param[0] = destinationPath;
param[1] = 0;
///> open output
out->open(param);
///> connect the output
ops.setOutput(out);
try{
//cout << "eventi : "<< endl;
int source_sequence_counter = 0;
while((row = mysql_fetch_row(result))){
source_sequence_counter++; // start by 1
cout << "pacchetto numero: " << source_sequence_counter << endl;
//cout << "event_id: " << row[0] << " LTtime"<< row[3] << endl;
// ogni ciclo è un pacchetto
string event_id = row[0];
string lttime = row[3];
float lttime_float = atof(lttime.c_str() );
long event_id_long = atol(event_id.c_str() );
// id_tel is the current camera;
// WRITE PACKET
Packet* p = ops.getPacketType(1); // it is the firs packet format in the stream file
///> PACKET HEADER
p->header->setFieldValue(0, camera[id_tel]); //camera[id_tel]); // telescope Id
p->header->setFieldValue(1, 2); // type 2
p->header->setFieldValue(2, 1); // subtype 1
p->header->setFieldValue(3,source_sequence_counter); // source sequence counter
// DATA FIELD HEADER SETTING
// time tag second - field 0 - 2 word
// lttime must be split in second and millisecond
int lttime_float_intera = (int) lttime_float;
float decimale = lttime_float - lttime_float_intera;
while ( decimale != floor(decimale) ){
decimale = decimale * 10;
}
cout <<" time is: "<< lttime_float << "int = " << lttime_float_intera << " dec: " << (int) decimale<< endl;
p->dataField->dataFieldHeader->setFieldValue_4_14(0, lttime_float_intera); // time tag second
p->dataField->dataFieldHeader->setFieldValue_5_1(2, decimale ); // time tag millisecond
// time tag nano second - field 2 - 2 word
// event counter - field 4 - 2 word
p->dataField->dataFieldHeader->setFieldValue_3_14(4, event_id_long ); //long int unsigned
// NPDM (Number of PDM ) - field 12
p->dataField->dataFieldHeader->setFieldValue(12, 37); // number of PDM
// WRITE BLOCKS
/// write the number of block
p->dataField->setNumberOfRealDataBlock(NPDMs);
// manage the blocks
SDFBlockFixed* sdf = (SDFBlockFixed*) p->dataField->sourceDataField;
SDFBFBlock* block[NPDMs];
for (int npdm = 1; npdm < 38; npdm++){
int blockNumber = npdm - 1; // it start from 0
block[blockNumber] = sdf->getBlock(blockNumber);
// set the number of PDM - field 1
block[blockNumber]->setFieldValue(1, npdm ) ;
//cout << "pdm : " << npdm << endl;
// ogni ciclo è un blocco
int field_block_counter = 2;
// high gain
string npdm_string = static_cast<ostringstream*>( &(ostringstream() << npdm ) )->str();
querystring = "SELECT * from pixel_h where tel_data = " + id_tel_string + " and event_id = " + event_id + " and NPDM = " + npdm_string + " ORDER BY pixel_id ";
//cout << "query " << querystring << endl;
mysql_query(&mysql, querystring.c_str() );
result_event = mysql_store_result(&mysql);
// fieo lds number before the first value -1 (because it start by 0 )
int offset = 1;
int pixel_id = 0;
int field = 0;
while((row_events = mysql_fetch_row(result_event))){
double value_float = atof(row_events[4]);
pixel_id = atoi(row_events[2] );
field = pixel_id + offset;
//block[blockNumber]->setFieldValue_5_1(field_block_counter, value_float);
block[blockNumber]->setFieldValue_5_1(field, value_float);
field_block_counter++;
}
// low gain
npdm_string = static_cast<ostringstream*>( &(ostringstream() << npdm ) )->str();
querystring = "SELECT * from pixel_l where tel_data = " + id_tel_string + " and event_id = " + event_id + " and NPDM = " + npdm_string + " ORDER BY pixel_id ";
//cout << "query " << querystring << endl;
mysql_query(&mysql, querystring.c_str() );
result_event = mysql_store_result(&mysql);
offset = 65; // 64 pixel + 1=npdm + 1=spare
while((row_events = mysql_fetch_row(result_event))){
istringstream in(row_events[4]);
double value_float; // = (double) atof(row_events[4]);
in >> value_float;
pixel_id = atoi(row_events[2] );
field = pixel_id + offset;
block[blockNumber]->setFieldValue_5_1(field, value_float);
field_block_counter++;
}
//querystring = "SELECT * from pixel_l where ntel=ntel and event_id = event_id and pdm = npdm ORDER BY pixel_id ";
}
//cout << " PACKET WRITTEN: " << endl;
//cout << p->packet_output->printStreamInHexadecimal() << endl;
// send packet
ops.writePacket(p); ///> writing packet
}
///> close the output for current camera
out->close();
}catch (PacketException* e){
cout << "Packet Exception: " << endl;
cout << e->geterror() << endl;///> if there is an exception then view corresponding error
}
}
mysql_close(&mysql); // close a previously open connection
return 0;
}