summaryrefslogtreecommitdiff
path: root/fileio/root.C
blob: 07e50a103fac0027f5d8d2fdbe88e47ccb6c6693 (plain)
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
#include <TVector3.h>
#include <vector>
#include <TTree.h>
#include <string>

struct Photon {
  double time;
  TVector3 position;
  TVector3 direction;
  TVector3 polarization;
  double wavelength; // nm
  unsigned int history;
  int last_hit_triangle;

  ClassDef(Photon, 1);
};

struct Track {
  std::string particle;
  TVector3 position;
  TVector3 direction;
  double start_time;
  double total_energy;

  ClassDef(Track, 1);
};

struct MC {
  std::string particle;
  TVector3 gen_position;
  TVector3 gen_direction;
  double gen_total_energy;

  int nphoton;
  
  std::vector<Track> subtrack;
  std::vector<Photon> photon_start;
  std::vector<Photon> photon_stop;

  ClassDef(MC, 1);
};

struct Channel {
  Channel() : channel_id(-1), time(-9999.0), charge(-9999.0) { };
  int channel_id;
  double time;
  double charge;
  unsigned int mc_history;

  ClassDef(Channel, 1);
};
  
struct Event {
  int event_id;
  MC mc;
  int nhit;
  int max_channel_id;
  std::vector<Channel> channel;

  ClassDef(Event, 1);

  // Populate arrays of length nentries with hit, time, and charge
  // information, indexed by channel ID
  void get_channels(unsigned int nentries, int *hit, float *time,
		    float *charge, unsigned int *mc_history=0)
  {
    for (unsigned int i=0; i < nentries; i++) {
      hit[i] = 0;
      time[i] = -1e9f;
      charge[i] = -1e9f;
      if (mc_history)
	mc_history[i] = 0;
    }

    for (unsigned int i=0; i < channel.size(); i++) {
      unsigned int channel_id = channel[i].channel_id;

      if (channel_id < nentries) {
	hit[channel_id] = 1;
	time[channel_id] = channel[i].time;
	charge[channel_id] = channel[i].charge;
	if (mc_history)
	  mc_history[channel_id] = channel[i].mc_history;
      }
    }
  }

};

void get_photons(const std::vector<Photon> &photons, float *positions,
		 float *directions, float *polarizations, float *wavelengths,
		 float *times, unsigned int *histories, int *last_hit_triangles)
{
  for (unsigned int i=0; i < photons.size(); i++) {
    const Photon &photon = photons[i];
    positions[3*i] = photon.position.X();
    positions[3*i+1] = photon.position.Y();
    positions[3*i+2] = photon.position.Z();

    directions[3*i] = photon.direction.X();
    directions[3*i+1] = photon.direction.Y();
    directions[3*i+2] = photon.direction.Z();
    
    polarizations[3*i] = photon.polarization.X();
    polarizations[3*i+1] = photon.polarization.Y();
    polarizations[3*i+2] = photon.polarization.Z();

    wavelengths[i] = photon.wavelength;
    times[i] = photon.time;
    histories[i] = photon.history;
    last_hit_triangles[i] = photon.last_hit_triangle;
  }
}
		 

void fill_photons(std::vector<Photon> &photons,
		  unsigned int nphotons, float *pos, float *dir,
		  float *pol, float *wavelength, float *t0,
		  unsigned int *histories=0, int *last_hit_triangle=0)
{
  photons.resize(nphotons);
  
  for (unsigned int i=0; i < nphotons; i++) {
    Photon &photon = photons[i];
    photon.time = t0[i];
    photon.position.SetXYZ(pos[3*i], pos[3*i + 1], pos[3*i + 2]);
    photon.direction.SetXYZ(dir[3*i], dir[3*i + 1], dir[3*i + 2]);
    photon.polarization.SetXYZ(pol[3*i], pol[3*i + 1], pol[3*i + 2]);
    photon.wavelength = wavelength[i];
    if (histories)
      photon.history = histories[i];
    else
      photon.history = 0;

    if (last_hit_triangle)
      photon.last_hit_triangle = last_hit_triangle[i];
    else
      photon.last_hit_triangle = -1;
  }
}
		  

void fill_hits(Event *ev, unsigned int nchannels, float *time,
	       float *charge, unsigned int *history)
{
  ev->channel.resize(0);
  ev->nhit = 0;
  ev->max_channel_id = nchannels - 1;

  Channel ch;
  for (unsigned int i=0; i < nchannels; i++) {
    if (time[i] < 1e8) {
      ev->nhit++;
      ch.channel_id = i;
      ch.time = time[i];
      ch.charge = charge[i];
      ch.mc_history = history[i];
      ev->channel.push_back(ch);
    }
  }
}


#ifdef __MAKECINT__
#pragma link C++ class vector<Track>;
#pragma link C++ class vector<Photon>;
#pragma link C++ class vector<Channel>;
#endif