Rev 193 | Rev 267 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
193 | f9daq | 1 | #include <TClonesArray.h> |
2 | #include <TH1F.h> |
||
3 | #include <TStopwatch.h> |
||
4 | #include <TDatime.h> |
||
5 | #include <TString.h> |
||
6 | #include <TFile.h> |
||
7 | #include <TTree.h> |
||
8 | #include <TBranch.h> |
||
9 | #include <TClonesArray.h> |
||
10 | |||
11 | #include "BParticle.h" |
||
12 | #include "BEvent.h" |
||
13 | |||
14 | class Hdr{ |
||
15 | public: |
||
16 | int id; |
||
17 | int len; |
||
18 | int progress; |
||
19 | }; |
||
20 | |||
21 | |||
22 | class Blab2 { |
||
23 | public: |
||
24 | UInt_t fNeve; |
||
266 | f9daq | 25 | UInt_t fNfirst; |
193 | f9daq | 26 | int fData; |
27 | TH1F *fH[100]; |
||
266 | f9daq | 28 | UInt_t fHtype[100]; |
193 | f9daq | 29 | TClonesArray *fList[100]; |
30 | |||
31 | Blab2(); |
||
32 | ~Blab2(); |
||
33 | void Init(); |
||
34 | void event(); |
||
35 | void Process(); |
||
36 | void h1d(const char *varname, const char *name, int nbins, double min, double max, int id ); |
||
37 | int selector(int pin, int charge, SIMPLEPID particlename, int hid, int pout ); |
||
38 | int combiner(int id0 ,int id1 , SIMPLEPID particlename, double min, double max, int hid, int id ); |
||
39 | int fix_mass(int id); |
||
266 | f9daq | 40 | int Fill(int hid, BParticle *p); |
193 | f9daq | 41 | void plist(int i); |
42 | |||
43 | |||
44 | ClassDef ( Blab2, 1 ) |
||
45 | }; |
||
46 | |||
47 | ClassImp(Blab2) |
||
48 | |||
266 | f9daq | 49 | Blab2::Blab2():fNfirst(0), fNeve(0), fData(0){ |
193 | f9daq | 50 | |
51 | Process(); |
||
52 | }; |
||
53 | |||
266 | f9daq | 54 | |
193 | f9daq | 55 | void Blab2::h1d(const char *varname, const char *name, int nbins, double min, double max, int id ){ |
266 | f9daq | 56 | TString svar(varname); |
57 | TString axis[3]={"mass (GeV/c2)","momentum (GeV/c)","energy (GeV)"}; |
||
58 | fHtype[id] = 0; |
||
59 | if (svar.Contains("GetMass" )) fHtype[id]=0; |
||
60 | if (svar.Contains("GetMomentum")) fHtype[id]=1; |
||
61 | if (svar.Contains("GetEnergy" )) fHtype[id]=2; |
||
62 | |||
63 | fH[id]= new TH1F(TString::Format("h%d",id), TString::Format("%s;%s;N",name,axis[fHtype[id]].Data()), nbins, min, max); |
||
193 | f9daq | 64 | } |
65 | |||
266 | f9daq | 66 | int Blab2::Fill(int hid, BParticle *p){ |
67 | if (hid>=0 && fH[hid]) { |
||
68 | float val; |
||
69 | switch (fHtype[hid]){ |
||
70 | case 0: val = p->GetMass(); break; |
||
71 | case 1: val = p->GetMomentum(); break; |
||
72 | case 2: val = p->e(); break; |
||
73 | default : val = 0 ; break; |
||
74 | } |
||
75 | fH[hid]->Fill(val); |
||
76 | } |
||
77 | |||
78 | return 0; |
||
79 | } |
||
80 | |||
193 | f9daq | 81 | int Blab2::combiner(int _p0, int _p1,SIMPLEPID pid, double min, double max, int hid, int _p2 ){ |
82 | // Loop over all the particles in both lists. |
||
83 | if (_p0 < 0 ) _p0 =0; |
||
84 | if (_p1 < 0 ) _p1 =0; |
||
85 | |||
86 | |||
87 | fList[_p2]->Clear(); |
||
88 | int nprt=0; |
||
89 | |||
90 | for(TIter next1(fList[_p0]);BParticle * p1 =(BParticle *) next1();) { |
||
91 | // the second loop |
||
92 | |||
93 | for(TIter next2 = (_p0!=_p1) ? TIter(fList[_p1]): TIter(next1) ; BParticle * p2 =(BParticle *) next2();) { |
||
94 | if (p1==p2) continue; |
||
95 | BParticle p = *p1 + *p2; // Combine two particles into a new particle |
||
96 | if (p.InMassRange(min, max)){ |
||
97 | //SIMPLEPID pid = PION; // should be set to particlename |
||
98 | p.SetPid(pid); |
||
99 | TClonesArray& list = *fList[_p2]; |
||
100 | new (list[nprt++]) BParticle ( p ); // create a new entry in kslist list of particles |
||
266 | f9daq | 101 | Fill(hid, &p); |
193 | f9daq | 102 | } |
103 | |||
104 | } |
||
105 | |||
106 | } |
||
107 | return _p2; |
||
108 | } |
||
109 | |||
110 | |||
111 | int Blab2::selector(int pin, int charge, SIMPLEPID type , int hid, int pout ){ |
||
112 | if (pin < 0 ) pin =0; |
||
113 | |||
114 | fList[pout]->Clear(); |
||
115 | int nprt=0; |
||
116 | |||
117 | for(TIter next(fList[pin]); BParticle * p =(BParticle *) next();) { |
||
266 | f9daq | 118 | if (p->charge()== charge && ( p->pid()== type || type == ALL )) { |
193 | f9daq | 119 | TClonesArray& list = *fList[pout]; |
120 | new (list[nprt++]) BParticle ( *p ); |
||
266 | f9daq | 121 | Fill(hid, p); |
193 | f9daq | 122 | } |
123 | } |
||
124 | return pout; |
||
125 | } |
||
126 | |||
127 | |||
128 | int Blab2::fix_mass(int pin){ |
||
129 | if (pin < 0 ) pin =0; |
||
130 | for(TIter next(fList[pin]); BParticle * p =(BParticle *) next();) p->SetEnergyFromPid(); |
||
131 | return pin; |
||
132 | } |
||
133 | |||
134 | void Blab2::plist(int i){ |
||
135 | fList[i]= new TClonesArray( "BParticle", 500 ); |
||
136 | } |
||
137 | Blab2::~Blab2(){}; |
||
138 | |||
139 | |||
140 | void send_message(int id, TString msg, int progress){ |
||
141 | static Hdr hdr; |
||
142 | |||
143 | hdr.id = id; |
||
144 | hdr.len= msg.Length(); |
||
145 | hdr.progress= progress; |
||
146 | fwrite(&hdr,3*sizeof(int),1,stdout); |
||
147 | fwrite(msg.Data(),hdr.len,1,stdout); |
||
148 | fflush(stdout); |
||
149 | |||
150 | } |
||
151 | |||
152 | |||
153 | void Blab2::Process(){ |
||
154 | |||
155 | for (int i=0;i<100;i++) fH[i]=NULL; |
||
266 | f9daq | 156 | for (int i=0;i<100;i++) fHtype[i]=0; |
193 | f9daq | 157 | for (int i=0;i<100;i++) fList[i]=NULL; |
158 | |||
159 | Init(); |
||
160 | |||
161 | TFile * f = new TFile(TString::Format("../../data/hadron-%d.root",fData)); // Open a data file |
||
162 | if(f->IsZombie()) { send_message(0,TString::Format("File %d not found\n",fData), 0 ); return; } |
||
163 | TTree * t =(TTree *) f-> Get( "T"); // Obtain a pointer to a tree of "event" data in the file |
||
164 | BEvent * mevent = new BEvent(); // Create a "BEvent" object where data from the file will be loaded |
||
165 | TBranch * branch = t-> GetBranch( "BEvent"); // Obtain a branch for "BEvent" in the tree |
||
166 | branch-> SetAddress(&mevent); // Register created "BEvent" object to the branch |
||
266 | f9daq | 167 | TH1F *fHnprt= new TH1F("h100", "Number of particles in the event;N particles;N events", 50, -0.5, 49.5); |
168 | TH1F *fHid= new TH1F("h101", "Particle type;ID;N particles", 6, -0, 6); |
||
193 | f9daq | 169 | |
266 | f9daq | 170 | const char *names[6]={"PHOTON", "ELECTRON", "PION", "MUON", "KAON", "PROTON"}; |
171 | for (int i=1;i<=6;i++) fHid->GetXaxis()->SetBinLabel(i,names[i-1]); |
||
172 | |||
193 | f9daq | 173 | send_message(0, TString::Format("Number of Entries in the file %lld\n", t->GetEntries() ),0); |
174 | TStopwatch timer; |
||
175 | timer.Start(); |
||
266 | f9daq | 176 | int i =TMath::Min(fNfirst, (UInt_t) t-> GetEntries()); |
177 | int cNeve=TMath::Min(fNfirst+fNeve, (UInt_t) t-> GetEntries()); |
||
193 | f9daq | 178 | while (i<cNeve){ |
179 | t-> GetEntry(i); // Read the content of the event from the file |
||
180 | fList[0]= mevent->GetParticleList(); |
||
266 | f9daq | 181 | |
193 | f9daq | 182 | event(); |
183 | |||
184 | if (i%10000==0) send_message(2, TString::Format("Event %d\n",i), (100*i)/cNeve); |
||
266 | f9daq | 185 | |
186 | int nprt=0; |
||
187 | for(TIter next(fList[0]); BParticle * p =(BParticle *) next();) { |
||
188 | nprt++; |
||
189 | fHid->Fill(p->pid()); |
||
190 | } |
||
191 | fHnprt->Fill(nprt); |
||
192 | |||
193 | f9daq | 193 | mevent-> Clear(); // Clear the memory. |
194 | for (int k=0;k<100;k++) if (fList[k]!=0) fList[k]->Clear(); |
||
195 | i++; |
||
196 | } |
||
197 | |||
198 | send_message(2, TString::Format("Number of events processed %d\n", i ),100); |
||
199 | for (int i=0;i<100;i++) if (fH[i]!=0) send_message(1,TBufferJSON::ConvertToJSON(fH[i]),100 ); |
||
266 | f9daq | 200 | send_message(1,TBufferJSON::ConvertToJSON(fHnprt),100 ); |
201 | send_message(1,TBufferJSON::ConvertToJSON(fHid),100 ); |
||
193 | f9daq | 202 | TDatime d; |
203 | timer.Stop(); |
||
204 | send_message(3, TString::Format("'%s', %d, %f, %f", d.AsSQLString(),i, timer.RealTime(), timer.CpuTime() ),100); |
||
205 | |||
206 | } |
||
207 | |||
208 |