3D matrix occurrences

Hello! I'm trying to use a 3D Matrix ([3]x[37]x[64]), at the end I have to know the occurrences of this Matrix, I'm not experienced about 3D Matrix so I have difficulties (segmentation fault) using it. I'm doing it inside a function that I called int PDMPixels(int pdm, int pixel)

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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <libgen.h>
#include <unistd.h>
#include <iostream>
#include <cmath>
#include <fstream>
#include <iterator>
#include <vector>
#include <algorithm> 
#include <sstream>
#include <memory>
#include <list>
#include <deque>
#include <string.h>

using namespace std;

std::vector<std::string> split(std::string str,std::string sep){
  char* cstr=const_cast<char*>(str.c_str());
  char* current;
  std::vector<std::string> arr;
  current=strtok(cstr,sep.c_str());
  while(current!=NULL){
    arr.push_back(current);
    current=strtok(NULL,sep.c_str());
  }
  return arr;
}

//Matrix 48x48 
int **matrPDM = new int *[48];
int **matrPixel = new int *[48];

//Event Number in the input file
int numEventi=0;

//energy, zenith, etc in the input file for each event
int ievent = 0;
float energy = 0;
float zenith = 0;
float azimuth = 0;
float coredistance = 0;
int nphotons = 0;

//Arrays of energy, zenith, etc for each event
int *IEvent;
float *Energy;
float *Zenith;
float *Azimuth;
float *Coredistance;
int *Nphotons;
int *X;
int *Y;
float *Time_ns;

string line1;
int x=0;
int y=0;
float time_ns=0;
float time_s=0;
float flag=0;
int nline;  

unsigned long line_count = 0;

int countEvent() {
  int count1 = 0;
  int currLineIndex1 = 0;
  int skipIndex1 = -1;
  ifstream myfile("pippo.out");
  if(myfile.is_open()){
    while (std::getline(myfile, line1)) {
      if (currLineIndex1 == skipIndex1) {
	//skippa la linea perche quella adiacente ai "-"
      } else {
	if((line1.length()==31||line1[1]=='0')) {
	  //skippa la linea perche tutti 0
	}else if(line1.length()==39||line1[1]=='-') {
	  //skippa la linea perche riga -100
	  skipIndex1 = currLineIndex1+1;
	}
	else{
	  std::vector<std::string> arr;
	  arr=split(line1," ");
	  if (arr.size() == 6) {
	    count1++;
	  }
	}
      }
    }
  }
  myfile.close();
  return count1;
}

int creaPDMReference() {
  string line;
  for (int i = 0; i < 48; i ++) {
    matrPDM[i] = new int[48];
  }
  
  ifstream pdmfile("matrice_modcod_8x8.dat");
  if((pdmfile.is_open())){
    int riga = 0;
    int colonna = 0;
    
    while (std::getline(pdmfile, line)) {
      std::vector<std::string> arr;
      arr=split(line,"  ");
      for (int j = 0; j <48; j++) {
	matrPDM[j][colonna] =  atoi(arr[j].c_str());
      }
      colonna++;
    }
    pdmfile.close();
  }
  return 0;
}

int creaPixelReference() {
  string line;
  for (int i = 0; i < 48; i ++) {
    matrPixel[i] = new int[48];
  }
  ifstream pixelfile("matrice_pixel.dat");
  if((pixelfile.is_open())){
    int riga = 0;
    int colonna = 0;
    while (std::getline(pixelfile, line)) {
      std::vector<std::string> arr;
      arr=split(line,"  ");
      for (int j = 0; j <48; j++) {
	matrPixel[j][colonna] =  atoi(arr[j].c_str());
      }
      colonna++;
    }
    pixelfile.close();
  }
  return 0;
}


int convertPDM(int x, int y) {
  int xt = x + 23;
  int yt = 24 - y;
  int pdm = matrPDM[xt][yt];
  return pdm;
}

int convertPixel(int x, int y) {
  int xt = x + 23;
  int yt = 24 - y;
  int pixel = matrPixel[xt][yt];
  return pixel;
}

//Matrix 3x37x64
int dimX = 3; int dimY = 37; int dimZ = 64; 
int ***matrPDMPixel = new int**[dimX];

int PDMPixels(int pdm, int pixel){
  int PDMPIXEL;
  for(int x=0; x<dimX; ++x){
    matrPDMPixel[x] = new int*[dimY];
    for(int y=0; y<dimY; ++y) {
      matrPDMPixel[x][y] = new int[dimZ];
      for(int z=0; z<dimZ; ++z) { 
	matrPDMPixel[x][y][z] = 0;
	matrPDMPixel[x][y][z] = x*pdm*pixel;
	PDMPIXEL = matrPDMPixel[x][y][z];
      }
    }
  }
  return PDMPIXEL; 
}

int main(){
  creaPDMReference();
  creaPixelReference();
  
  numEventi = countEvent();
  IEvent = new int[numEventi];
  Energy = new float[numEventi];
  Zenith = new float[numEventi];
  Azimuth = new float[numEventi];
  Coredistance = new float[numEventi];
  Nphotons = new int[numEventi];

  return 0;
}
Last edited on
Topic archived. No new replies allowed.