Skip to content

Commit

Permalink
Minor code tweaks: int -> unsigned, formatting, clang-tidy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
hansenjo committed Nov 11, 2022
1 parent 36f25e5 commit d04a36a
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 64 deletions.
6 changes: 4 additions & 2 deletions DataFile.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

using namespace std;

DataFile::DataFile( string fname ) : filename(move(fname)), filep(nullptr)
DataFile::DataFile( string fname )
: filename{std::move(fname)}
, filep{nullptr}
, buffer{make_unique<evbuf_t[]>(MAX_EVTSIZE)}
{
// Constructor

buffer = make_unique<evbuf_t[]>(MAX_EVTSIZE);
buffer[0] = 0;
}

Expand Down
2 changes: 1 addition & 1 deletion DataFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

using evbuf_t = uint32_t;
using evbuf_ptr_t = std::unique_ptr<evbuf_t[]>;
static const int MAX_EVTSIZE = 1024;
static constexpr size_t MAX_EVTSIZE = 4*1024*1024;

class DataFile {
public:
Expand Down
3 changes: 1 addition & 2 deletions Database.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ class Database {
double value{};
} __attribute__((aligned(64)));

// Collection of key/value pairs from read from the database.
// Derived classes may extract their parameters from these items.
// Collection of key/value pairs read from the database.
std::vector<Item> m_items;

bool m_is_ready{false}; // True if database successfully read
Expand Down
4 changes: 2 additions & 2 deletions Decoder.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ int Decoder::Load( evbuf_t* evbuffer )
Clear();

char* evtp = ((char*)evbuffer)+sizeof(event.header);
int ndet = static_cast<int>(event.header.event_info & 0xFFFFU);
for( int i = 0; i < ndet; ++i ) {
auto ndet = event.header.event_info & 0xFFFFU;
for( decltype(ndet) i = 0; i < ndet; ++i ) {
auto* m = (ModuleData*)evtp;
if( !m )
return 3;
Expand Down
16 changes: 8 additions & 8 deletions Decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ class Decoder {
int Load( evbuf_t* evbuffer );
int Preload( evbuf_t* evbuffer );

[[nodiscard]] int GetEvSize() const { return event.header.event_length; }
[[nodiscard]] int GetNdata( int m ) const;
[[nodiscard]] double GetData( int m, int i ) const;
[[nodiscard]] double* GetDataBuf( int m ) const;
[[nodiscard]] bool IsSyncEvent() const;
[[nodiscard]] uint32_t GetEvSize() const { return event.header.event_length; }
[[nodiscard]] uint32_t GetNdata( int m ) const;
[[nodiscard]] double GetData( uint32_t m, uint32_t i ) const;
[[nodiscard]] double* GetDataBuf( uint32_t m ) const;
[[nodiscard]] bool IsSyncEvent() const;

private:
Event event;
Expand All @@ -29,7 +29,7 @@ class Decoder {


inline
int Decoder::GetNdata( int m ) const
uint32_t Decoder::GetNdata( int m ) const
{
if( event.module[m] == nullptr )
return 0;
Expand All @@ -38,14 +38,14 @@ int Decoder::GetNdata( int m ) const
}

inline
double Decoder::GetData( int m, int i ) const
double Decoder::GetData( uint32_t m, uint32_t i ) const
{
assert( event.module[m] );
return event.module[m]->data[i];
}

inline
double* Decoder::GetDataBuf( int m ) const
double* Decoder::GetDataBuf( uint32_t m ) const
{
assert( event.module[m] );
return event.module[m]->data;
Expand Down
11 changes: 7 additions & 4 deletions Detector.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
using namespace std;

Detector::Detector( string _name, int _imod )
: name(move(_name)), type("--baseclass--"), imod(_imod-1), fVars(nullptr)
: name(std::move(_name))
, type("--baseclass--")
, imod(_imod-1)
, fVars(nullptr)
{
if( imod<0 ) {
cerr << "\"" << name << "\": "
Expand All @@ -36,7 +39,7 @@ int Detector::Init( bool shared )
// Generic detector decoding
int Detector::Decode( Decoder& evdata )
{
int ndata = evdata.GetNdata(imod);
auto ndata = evdata.GetNdata(imod);
if( debug > 1 )
Print();
if( debug > 2 )
Expand All @@ -49,8 +52,8 @@ int Detector::Decode( Decoder& evdata )
data.assign( pdata, pdata+ndata );

if( debug > 3 ) {
for( int i = 0; i < ndata; ++i ) {
cout << evdata.GetData(imod,i);
for( decltype(ndata) i = 0; i < ndata; ++i ) {
cout << evdata.GetData(imod, i);
if( i+1 != ndata )
cout << ", ";
}
Expand Down
6 changes: 5 additions & 1 deletion Podd.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ extern int debug;

// Program configuration
struct Config {
Config() : nev_max(std::numeric_limits<size_t>::max()), nthreads(0), mark(0) {}
Config() noexcept
: nev_max(std::numeric_limits<size_t>::max())
, nthreads(0)
, mark(0)
{}
void default_names();

std::string input_file, odef_file, output_file, db_file;
Expand Down
4 changes: 3 additions & 1 deletion Variable.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
using namespace std;

Variable::Variable( string _name, string _note, const double* _loc )
: name(move(_name)), note(move(_note)), loc(_loc)
: name(std::move(_name))
, note(std::move(_note))
, loc(_loc)
{
assert(loc);
}
Expand Down
69 changes: 38 additions & 31 deletions generate.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@
using namespace std;

// Configuration
static const char* prgname;
static const char* filename = "";
static const size_t SIZE = 1024;
static const long int seed = 87934;
static int debug = 0;
static int NEVT = 10000;
static int NDET = 1;
static constexpr size_t SIZE = 1024;
static constexpr long int seed = 87934;
struct Config {
const char* prgname{""};
const char* filename{""};
int debug{0};
unsigned NEVT{10000};
unsigned NDET{1};
};
static Config conf;

// Gaussian-distributed random numbers
static inline
Expand All @@ -51,7 +54,7 @@ tuple<double,double> gauss()
// Usage message
static void usage()
{
cerr << "Usage: " << prgname << " [options] output_file" << endl
cerr << "Usage: " << conf.prgname << " [options] output_file" << endl
<< "where options are:" << endl
<< " [ -c num ]\tnumber of detectors to simulate (default 1)" << endl
<< " [ -n nev_max ]\t\tset number of events (default 10000)" << endl
Expand All @@ -73,13 +76,13 @@ class EventBuffer {
m_evtp(m_bufstart)
{}

void fill_header(int ndet) {
void fill_header(uint32_t ndet) {
EventHeader evthdr(0, ndet);
m_evtp = (char*)m_bufstart;
m_evtp = m_bufstart;
memcpy(m_evtp, &evthdr, sizeof(evthdr) );
m_evtp += sizeof(evthdr);
}
void append_module(int idet, int ndata, EvDat_t* data) {
void append_module(uint32_t idet, uint32_t ndata, EvDat_t* data) {
ModuleHeader modhdr( sizeof(modhdr) + ndata*sizeof(EvDat_t),
idet+1, // in the data file, module numbers start counting at 1
ndata );
Expand Down Expand Up @@ -116,25 +119,25 @@ class EventBuffer {
// Command line parser
void get_args( int argc, char** argv )
{
prgname = argv[0];
if( strlen(prgname) >= 2 && strncmp(prgname,"./",2) == 0 )
prgname += 2;
conf.prgname = argv[0];
if( strlen(conf.prgname) >= 2 && strncmp(conf.prgname,"./",2) == 0 )
conf.prgname += 2;

int opt;
while( (opt = getopt(argc, argv, "c:d:n:h")) != -1 ) {
switch (opt) {
case 'c':
NDET = stoi(optarg);
if( NDET > MAXMODULES ) {
conf.NDET = stoi(optarg);
if( conf.NDET > MAXMODULES ) {
cerr << "Too many detectors, max " << MAXMODULES << endl;
exit(255);
}
break;
case 'd':
debug = stoi(optarg);
conf.debug = stoi(optarg);
break;
case 'n':
NEVT = stoi(optarg);
conf.NEVT = stoi(optarg);
break;
case 'h':
default:
Expand All @@ -146,17 +149,17 @@ void get_args( int argc, char** argv )
cerr << "Output file name missing" << endl;
usage();
}
filename = argv[optind];
conf.filename = argv[optind];
}

int main( int argc, char** argv )
{
get_args(argc, argv);

// Open output
FILE* file = fopen(filename, "wb");
FILE* file = fopen(conf.filename, "wb");
if( !file ) {
cerr << "Cannot open file " << filename << endl;
cerr << "Cannot open file " << conf.filename << endl;
exit(1);
}

Expand All @@ -165,19 +168,19 @@ int main( int argc, char** argv )

try {
// Generate event data
for( int iev = 0; iev < NEVT; ++iev ) {
evbuffer.fill_header(NDET);
for( int idet = 0; idet < NDET; ++idet ) {
int ndata = 0;
for( unsigned iev = 0; iev < conf.NEVT; ++iev ) {
evbuffer.fill_header(conf.NDET);
for( unsigned idet = 0; idet < conf.NDET; ++idet ) {
unsigned ndata;
EvDat_t data[MAXDATA];
switch( idet ) {
case 1:
// Module type 2 wants 4-8 data points for linear fit
ndata = int(5. * drand48()) + 4;
ndata = unsigned(5. * drand48()) + 4;
{
double slope = (2.0 * drand48() - 1.0);
double inter = (2.0 * drand48() - 1.0);
for( int i = 0; i < ndata; ++i ) {
for( unsigned i = 0; i < ndata; ++i ) {
assert(2 * i + 1 < MAXDATA);
// y = error + intercept + slope*x;
auto [y1,y2] = gauss();
Expand All @@ -199,8 +202,8 @@ int main( int argc, char** argv )
break;
default:
// Generate between 1 and MAXDATA random data values per module
ndata = int(MAXDATA * drand48()) + 1;
for( int i = 0; i < ndata; ++i ) {
ndata = unsigned(MAXDATA * drand48()) + 1;
for( unsigned i = 0; i < ndata; ++i ) {
data[i] = 20.0 * drand48() - 10.0;
}
break;
Expand All @@ -217,8 +220,12 @@ int main( int argc, char** argv )
return 1;
}

cout << "Successfully generated " << NEVT << " events for " << NDET << " detectors" << endl;
if( fclose(file) != 0) {
cerr << "Error writing output file " << conf.filename << endl;
return 1;
}
cout << "Successfully generated " << conf.NEVT << " events for "
<< conf.NDET << " detectors" << endl;

fclose(file);
return 0;
}
19 changes: 11 additions & 8 deletions ppodd-tbb.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,13 @@ class EventReader {
tbb::concurrent_queue<EventBuffer*> m_queue;
};

EventReader::EventReader( size_t max, const string& filename ) :
m_inp(filename),
m_max(max),
m_count(0),
m_bufcount(0),
m_cur(nullptr) {}
EventReader::EventReader( size_t max, const string& filename )
: m_inp(filename)
, m_max(max)
, m_count(0)
, m_bufcount(0)
, m_cur(nullptr)
{}

EventBuffer* EventReader::operator()() {
if( (!m_inp.IsOpen() && m_inp.Open() != 0) )
Expand Down Expand Up @@ -303,8 +304,10 @@ void EventReader::push( EventBuffer* evt ) {

EventReader::~EventReader() {
m_inp.Close();
while( m_queue.try_pop(m_cur) )
while( m_queue.try_pop(m_cur) ) {
delete m_cur;
m_cur = nullptr;
}
}


Expand Down Expand Up @@ -383,7 +386,7 @@ void OutputWriter::WriteEvent( ostrm_t& os, Context* ctx, bool do_header ) {

void OutputWriter::WriteHeader( ostrm_t& os, Context* ctx ) {
// Write output file header
// <N = number of variables> N*<variable typ£££e> N*<variable name C-string>
// <N = number of variables> N*<variable type> N*<variable name C-string>
// where
// <variable type> = TTTNNNNN,
// with
Expand Down
11 changes: 7 additions & 4 deletions rawdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
#define PPODD_RAWDATA

#include <cstdint>
#include <cstddef>

using EvDat_t = double;

static const int32_t MAXDATA = 16;
static const int32_t MAXMODULES = 8;
static constexpr size_t MAXDATA = 16;
static constexpr size_t MAXMODULES = 8;

// For each event
struct EventHeader {
EventHeader() = default;
EventHeader() : event_length{0}, event_info{0} {}
EventHeader(uint32_t len, uint32_t info) :
event_length(len), event_info(info) {}
uint32_t event_length; // length of full event (bytes), including this word
Expand All @@ -21,7 +22,7 @@ struct EventHeader {

// For each module in the event
struct ModuleHeader {
ModuleHeader() = default;
ModuleHeader() : module_length{0}, module_number{0}, module_ndata{0} {}
ModuleHeader(uint32_t len, uint16_t num, uint16_t ndata ) :
module_length(len), module_number(num), module_ndata(ndata) {}
uint32_t module_length; // length of module data (bytes), including this word
Expand All @@ -30,11 +31,13 @@ struct ModuleHeader {
} __attribute__((aligned(8)));

struct ModuleData {
ModuleData() : data{} {}
ModuleHeader header;
EvDat_t data[MAXDATA];
} __attribute__((aligned(128)));

struct Event {
Event() : module{} {}
EventHeader header;
ModuleData* module[MAXMODULES];
} __attribute__((aligned(128)));
Expand Down

0 comments on commit d04a36a

Please sign in to comment.