R4sImg.cpp 2 KB
Newer Older
rohe's avatar
rohe committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
/* -------------------------------------------------------------
 *
 *  file:        R4sImg.cpp
 *
 *  description: Converts rawdata from dtb into a "readable" Image
 *
 *  author:      Beat Meier
 *  modified:    31.8.2007
 *
 *  rev:         Tilman Rohe
 *  dat:         28.08.2017 
 *
 * -------------------------------------------------------------
 */




#include "cmd.h"
#include "datalink.h"
#include "R4sImg.h"

//#include "pixelAlive.h"
#include "TROOT.h"
#include "TH1D.h"
#include "TH2D.h"
#include "TH2F.h"
#include "TCanvas.h"
#include "TFile.h"
#include "TString.h"
#include "TTree.h"
#include "TBranch.h"
#include "TLeaf.h"
#include "TGraph.h"
#include "TMultiGraph.h"
#include "TLegend.h"
#include "TCanvas.h"




int R4sImg::Get(int x, int y) {

  if ( y>IMG_HEIGHT || x>IMG_WIDTH )
    {printf("ERROR: Out of range");return -9999;}

 return data[y*IMG_WIDTH + x]; 
}

bool R4sImg::CreateRaw(const vector<uint16_t> &rawdata)
{
	Clear();
	if (rawdata.size() < IMG_WIDTH*IMG_HEIGHT) return false;
	data = new int[IMG_WIDTH*IMG_HEIGHT];

	for (int pos=0; pos<IMG_WIDTH*IMG_HEIGHT; pos++)
	{
		int value = rawdata[pos];

		if (value & 0x1000) // ADC overrange
			value = R4S_VALUE_OR;
		else if (value & 0x0800) // negative
			value -= 0x1000;

		data[pos] = value;
		dataNEW.push_back(value);
	}
	return true;
};


void R4sImg::Print(unsigned int count)
{
	if (!data) 
	{
		printf("<<empty>>\n");
		return;
	}

	if (count > IMG_WIDTH*IMG_HEIGHT) count = IMG_WIDTH*IMG_HEIGHT;

	for (unsigned int i=0; i<count; i++)
	{
		int v = data[i];
		if (v == R4S_VALUE_OR) printf("   or"); else printf(" %4i", v);
	}
	printf("\n");

	tb.Daq_Close();
}


void R4sImg::Save(const string &filename)
{

	if (!data) 
	{
		printf("<<empty>>\n");
		return;
	}

	FILE *f = fopen(filename.c_str(), "wt");
	if (!f) return;

	int i = 0, x, y;

	for (y=IMG_HEIGHT-1; y>=0; y--)
	{
		for (x=0; x<IMG_WIDTH; x++)
		{
			int	v = Get(x,y);
			if (v == R4S_VALUE_OR) fprintf(f, "   or"); else fprintf(f, " %4i", v);
		}
		fputs("\n", f);
	}

	fclose(f);
}