Skip to content

Commit

Permalink
Imported matching program.
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-kaspar committed Jan 29, 2018
1 parent aed0591 commit bb07477
Show file tree
Hide file tree
Showing 3 changed files with 708 additions and 1 deletion.
193 changes: 193 additions & 0 deletions alignment_classes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
#include <cstring>

using namespace std;

//----------------------------------------------------------------------------------------------------

struct AlignmentResult
{
double sh_x, sh_x_unc; // mm
double sh_y, sh_y_unc; // mm

AlignmentResult(double _sh_x=0., double _sh_x_unc=0., double _sh_y=0., double _sh_y_unc=0.) :
sh_x(_sh_x), sh_x_unc(_sh_x_unc), sh_y(_sh_y), sh_y_unc(_sh_y_unc)
{
}

void Write(FILE *f) const
{
fprintf(f, "sh_x=%.3f,sh_x_unc=%.3f,sh_y=%.3f,sh_y_unc=%.3f\n", sh_x, sh_x_unc, sh_y, sh_y_unc);
}
};

//----------------------------------------------------------------------------------------------------

struct AlignmentResults : public map<unsigned int, AlignmentResult>
{
void Write(FILE *f) const
{
for (auto &p : *this)
{
fprintf(f, "id=%u,", p.first);
p.second.Write(f);
}
}

int Add(char *line)
{
bool idSet = false;
unsigned int id = 0;
AlignmentResult result;

// loop over entries separated by ","
char *p = strtok(line, ",");
while (p != NULL)
{
// isolate key and value strings
char *pe = strstr(p, "=");
if (pe == NULL)
{
printf("ERROR in AlignmentResults::Add > entry missing = sign: %s.\n", p);
return 2;
}

char *s_key = p;

p = strtok(NULL, ",");

*pe = 0;

char *s_val = pe+1;

// interprete keys
if (strcmp(s_key, "id") == 0)
{
idSet = true;
id = atoi(s_val);
continue;
}

if (strcmp(s_key, "sh_x") == 0)
{
result.sh_x = atof(s_val);
continue;
}

if (strcmp(s_key, "sh_x_unc") == 0)
{
result.sh_x_unc = atof(s_val);
continue;
}

if (strcmp(s_key, "sh_y") == 0)
{
result.sh_y = atof(s_val);
continue;
}

if (strcmp(s_key, "sh_y_unc") == 0)
{
result.sh_y_unc = atof(s_val);
continue;
}

printf("ERROR in AlignmentResults::Add > unknown key: %s.\n", s_key);
return 3;
}

if (!idSet)
{
printf("ERROR in AlignmentResults::Add > id not set on the following line:\n%s.\n", line);
return 4;
}

insert({id, result});

return 0;
}
};

//----------------------------------------------------------------------------------------------------

struct AlignmentResultsCollection : public map<string, AlignmentResults>
{
int Write(const string &fn) const
{
FILE *f = fopen(fn.c_str(), "w");
if (!f)
return -1;

Write(f);

fclose(f);

return 0;
}

void Write(FILE *f) const
{
for (auto &p : *this)
{
fprintf(f, "\n[%s]\n", p.first.c_str());
p.second.Write(f);
}
}

int Load(const string &fn)
{
FILE *f = fopen(fn.c_str(), "r");
if (!f)
return -1;

return Load(f);

fclose(f);
}

int Load(FILE *f)
{
string label = "unknown";
AlignmentResults block;

while (!feof(f))
{
char line[300];
char *success = fgets(line, 300, f);

if (success == NULL)
break;

if (line[0] == '\n')
continue;

if (line[0] == '[')
{
if (block.size() > 0)
insert({label, block});

block.clear();

char *end = strstr(line, "]");
if (end == NULL)
{
printf("ERROR in AlignmentResultsCollection::Load > missing closing ].\n");
return 1;
}

*end = 0;

label = line+1;

continue;
}

if (block.Add(line) != 0)
return 2;
}

if (block.size() > 0)
insert({label, block});

return 0;
}
};
10 changes: 9 additions & 1 deletion makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
all: distributions
all: distributions match

distributions: distributions.cc config.h stat.h
g++ `root-config --libs` -lMinuit `root-config --cflags` \
Expand All @@ -7,3 +7,11 @@ distributions: distributions.cc config.h stat.h
-L$(CMSSW_RELEASE_BASE)/lib/slc6_amd64_gcc630 \
-lDataFormatsFWLite -lDataFormatsCommon -lDataFormatsCTPPSDetId -lFWCoreParameterSet -lFWCorePythonParameterSet \
distributions.cc -o distributions

match: match.cc config.h stat.h alignment_classes.h
g++ `root-config --libs` -lMinuit `root-config --cflags` \
--std=c++14 -Wall -Wextra -Wno-attributes\
-I$(CMSSW_RELEASE_BASE)/src \
-L$(CMSSW_RELEASE_BASE)/lib/slc6_amd64_gcc630 \
-lDataFormatsFWLite -lDataFormatsCommon -lDataFormatsCTPPSDetId -lFWCoreParameterSet -lFWCorePythonParameterSet \
match.cc -o match
Loading

0 comments on commit bb07477

Please sign in to comment.