/[PAMELA software]/PamelaDigitizer/Digitizer.cxx
ViewVC logotype

Contents of /PamelaDigitizer/Digitizer.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (show annotations) (download)
Wed May 21 09:50:43 2008 UTC (16 years, 6 months ago) by pamelats
Branch: MAIN
Changes since 1.6: +344 -2965 lines
*** empty log message ***

1 // ------ PAMELA Digitizer ------
2 //
3 // Date, release and how-to: see file Pamelagp2Digits.cxx
4 //
5 // NB: Check length physics packet [packet type (0x10 = physics data)]
6 //
7 #include <sstream>
8 #include <fstream>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <ctype.h>
13 #include <time.h>
14 #include "Riostream.h"
15 #include "TFile.h"
16 #include "TDirectory.h"
17 #include "TTree.h"
18 #include "TLeafI.h"
19 #include "TH1.h"
20 #include "TH2.h"
21 #include "TF1.h"
22 #include "TMath.h"
23 #include "TRandom.h"
24 #include "TSQLServer.h"
25 #include "TSystem.h"
26 #include "CalibTrk1Event.h"
27 #include "CalibTrk2Event.h"
28 //
29 #include "Digitizer.h"
30 #include "CRC.h"
31 //
32 #include <PamelaRun.h>
33 #include <physics/calorimeter/CalorimeterEvent.h>
34 #include <CalibCalPedEvent.h>
35 #include "GLTables.h"
36 //
37 extern "C"{
38 short crc(short, short);
39 };
40 //
41
42 Digitizer::Digitizer(TTree* tree, char* &file_raw){
43 fhBookTree = tree;
44 fFilename = file_raw;
45 fCounter = 0;
46 fCounterPhys = 0; // SO 5/12/'07
47 fOBT = 0;
48
49 //
50 // DB connections
51 //
52 TString host = "mysql://localhost/pamelaprod";
53 TString user = "anonymous";
54 TString psw = "";
55 //
56 const char *pamdbhost=gSystem->Getenv("PAM_DBHOST");
57 const char *pamdbuser=gSystem->Getenv("PAM_DBUSER");
58 const char *pamdbpsw=gSystem->Getenv("PAM_DBPSW");
59 if ( !pamdbhost ) pamdbhost = "";
60 if ( !pamdbuser ) pamdbuser = "";
61 if ( !pamdbpsw ) pamdbpsw = "";
62 if ( strcmp(pamdbhost,"") ) host = pamdbhost;
63 if ( strcmp(pamdbuser,"") ) user = pamdbuser;
64 if ( strcmp(pamdbpsw,"") ) psw = pamdbpsw;
65 fDbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
66 //
67 GL_TABLES *glt = new GL_TABLES(host,user,psw);
68 if ( glt->IsConnected(fDbc) ) printf("\n DB INFORMATION:\n SQL: %s Version: %s Host %s Port %i \n\n",fDbc->GetDBMS(),fDbc->ServerInfo(),fDbc->GetHost(),fDbc->GetPort());
69 //
70 // Use UTC in the DB and make timeout bigger
71 //
72 stringstream myquery;
73 myquery.str("");
74 myquery << "SET time_zone='+0:00'";
75 fDbc->Query(myquery.str().c_str());
76 myquery.str("");
77 myquery << "SET wait_timeout=173000;";
78 fDbc->Query(myquery.str().c_str());
79 //
80
81 std:: cout << "preparing tree" << endl;
82
83 // prepare tree//modified by E.Vannuccini 03/08
84 if(fhBookTree->GetBranch("Irun"))fhBookTree->SetBranchAddress("Irun",&Irun);
85 if(fhBookTree->GetBranch("Ievnt"))fhBookTree->SetBranchAddress("Ievnt",&Ievnt);
86 if(fhBookTree->GetBranch("Ipa"))fhBookTree->SetBranchAddress("Ipa",&Ipa);
87 if(fhBookTree->GetBranch("X0"))fhBookTree->SetBranchAddress("X0",&X0);
88 if(fhBookTree->GetBranch("Y0"))fhBookTree->SetBranchAddress("Y0",&Y0);
89 if(fhBookTree->GetBranch("Z0"))fhBookTree->SetBranchAddress("Z0",&Z0);
90 if(fhBookTree->GetBranch("Theta"))fhBookTree->SetBranchAddress("Theta",&Theta);
91 if(fhBookTree->GetBranch("Phi"))fhBookTree->SetBranchAddress("Phi",&Phi);
92 if(fhBookTree->GetBranch("P0"))fhBookTree->SetBranchAddress("P0",&P0);
93 if(fhBookTree->GetBranch("Nthtof"))fhBookTree->SetBranchAddress("Nthtof",&Nthtof);
94 if(fhBookTree->GetBranch("Ipltof"))fhBookTree->SetBranchAddress("Ipltof",Ipltof);
95 if(fhBookTree->GetBranch("Ipaddle"))fhBookTree->SetBranchAddress("Ipaddle",Ipaddle);
96 if(fhBookTree->GetBranch("Ipartof"))fhBookTree->SetBranchAddress("Ipartof",Ipartof);
97 if(fhBookTree->GetBranch("Xintof"))fhBookTree->SetBranchAddress("Xintof",Xintof);
98 if(fhBookTree->GetBranch("Yintof"))fhBookTree->SetBranchAddress("Yintof",Yintof);
99 if(fhBookTree->GetBranch("Zintof"))fhBookTree->SetBranchAddress("Zintof",Zintof);
100 if(fhBookTree->GetBranch("Xouttof"))fhBookTree->SetBranchAddress("Xouttof",Xouttof);
101 if(fhBookTree->GetBranch("Youttof"))fhBookTree->SetBranchAddress("Youttof",Youttof);
102 if(fhBookTree->GetBranch("Zouttof"))fhBookTree->SetBranchAddress("Zouttof",Zouttof);
103 if(fhBookTree->GetBranch("Ereltof"))fhBookTree->SetBranchAddress("Ereltof",Ereltof);
104 if(fhBookTree->GetBranch("Timetof"))fhBookTree->SetBranchAddress("Timetof",Timetof);
105 if(fhBookTree->GetBranch("Pathtof"))fhBookTree->SetBranchAddress("Pathtof",Pathtof);
106 if(fhBookTree->GetBranch("P0tof"))fhBookTree->SetBranchAddress("P0tof",P0tof);
107 if(fhBookTree->GetBranch("Nthcat"))fhBookTree->SetBranchAddress("Nthcat",&Nthcat);
108 if(fhBookTree->GetBranch("Iparcat"))fhBookTree->SetBranchAddress("Iparcat",Iparcat);
109 if(fhBookTree->GetBranch("Icat"))fhBookTree->SetBranchAddress("Icat",Icat);
110 if(fhBookTree->GetBranch("Xincat"))fhBookTree->SetBranchAddress("Xincat",Xincat);
111 if(fhBookTree->GetBranch("Yincat"))fhBookTree->SetBranchAddress("Yincat",Yincat);
112 if(fhBookTree->GetBranch("Zincat"))fhBookTree->SetBranchAddress("Zincat",Zincat);
113 if(fhBookTree->GetBranch("Xoutcat"))fhBookTree->SetBranchAddress("Xoutcat",Xoutcat);
114 if(fhBookTree->GetBranch("Youtcat"))fhBookTree->SetBranchAddress("Youtcat",Youtcat);
115 if(fhBookTree->GetBranch("Zoutcat"))fhBookTree->SetBranchAddress("Zoutcat",Zoutcat);
116 if(fhBookTree->GetBranch("Erelcat"))fhBookTree->SetBranchAddress("Erelcat",Erelcat);
117 if(fhBookTree->GetBranch("Timecat"))fhBookTree->SetBranchAddress("Timecat",Timecat);
118 if(fhBookTree->GetBranch("Pathcat"))fhBookTree->SetBranchAddress("Pathcat",Pathcat);
119 if(fhBookTree->GetBranch("P0cat"))fhBookTree->SetBranchAddress("P0cat",P0cat);
120 if(fhBookTree->GetBranch("Nthcas"))fhBookTree->SetBranchAddress("Nthcas",&Nthcas);
121 if(fhBookTree->GetBranch("Iparcas"))fhBookTree->SetBranchAddress("Iparcas",Iparcas);
122 if(fhBookTree->GetBranch("Icas"))fhBookTree->SetBranchAddress("Icas",Icas);
123 if(fhBookTree->GetBranch("Xincas"))fhBookTree->SetBranchAddress("Xincas",Xincas);
124 if(fhBookTree->GetBranch("Yincas"))fhBookTree->SetBranchAddress("Yincas",Yincas);
125 if(fhBookTree->GetBranch("Zincas"))fhBookTree->SetBranchAddress("Zincas",Zincas);
126 if(fhBookTree->GetBranch("Xoutcas"))fhBookTree->SetBranchAddress("Xoutcas",Xoutcas);
127 if(fhBookTree->GetBranch("Youtcas"))fhBookTree->SetBranchAddress("Youtcas",Youtcas);
128 if(fhBookTree->GetBranch("Zoutcas"))fhBookTree->SetBranchAddress("Zoutcas",Zoutcas);
129 if(fhBookTree->GetBranch("Erelcas"))fhBookTree->SetBranchAddress("Erelcas",Erelcas);
130 if(fhBookTree->GetBranch("Timecas"))fhBookTree->SetBranchAddress("Timecas",Timecas);
131 if(fhBookTree->GetBranch("Pathcas"))fhBookTree->SetBranchAddress("Pathcas",Pathcas);
132 if(fhBookTree->GetBranch("P0cas"))fhBookTree->SetBranchAddress("P0cas",P0cas);
133 if(fhBookTree->GetBranch("Nthspe"))fhBookTree->SetBranchAddress("Nthspe",&Nthspe);
134 if(fhBookTree->GetBranch("Iparspe"))fhBookTree->SetBranchAddress("Iparspe",Iparspe);
135 if(fhBookTree->GetBranch("Itrpb"))fhBookTree->SetBranchAddress("Itrpb",Itrpb);
136 if(fhBookTree->GetBranch("Itrsl"))fhBookTree->SetBranchAddress("Itrsl",Itrsl);
137 if(fhBookTree->GetBranch("Itspa"))fhBookTree->SetBranchAddress("Itspa",Itspa);
138 if(fhBookTree->GetBranch("Xinspe"))fhBookTree->SetBranchAddress("Xinspe",Xinspe);
139 if(fhBookTree->GetBranch("Yinspe"))fhBookTree->SetBranchAddress("Yinspe",Yinspe);
140 if(fhBookTree->GetBranch("Zinspe"))fhBookTree->SetBranchAddress("Zinspe",Zinspe);
141 if(fhBookTree->GetBranch("Xoutspe"))fhBookTree->SetBranchAddress("Xoutspe",Xoutspe);
142 if(fhBookTree->GetBranch("Youtspe"))fhBookTree->SetBranchAddress("Youtspe",Youtspe);
143 if(fhBookTree->GetBranch("Zoutspe"))fhBookTree->SetBranchAddress("Zoutspe",Zoutspe);
144 if(fhBookTree->GetBranch("Xavspe"))fhBookTree->SetBranchAddress("Xavspe",Xavspe);
145 if(fhBookTree->GetBranch("Yavspe"))fhBookTree->SetBranchAddress("Yavspe",Yavspe);
146 if(fhBookTree->GetBranch("Zavspe"))fhBookTree->SetBranchAddress("Zavspe",Zavspe);
147 if(fhBookTree->GetBranch("Erelspe"))fhBookTree->SetBranchAddress("Erelspe",Erelspe);
148 if(fhBookTree->GetBranch("Pathspe"))fhBookTree->SetBranchAddress("Pathspe",Pathspe);
149 if(fhBookTree->GetBranch("P0spe"))fhBookTree->SetBranchAddress("P0spe",P0spe);
150 if(fhBookTree->GetBranch("Nxmult"))fhBookTree->SetBranchAddress("Nxmult",Nxmult);
151 if(fhBookTree->GetBranch("Nymult"))fhBookTree->SetBranchAddress("Nymult",Nymult);
152 if(fhBookTree->GetBranch("Nstrpx"))fhBookTree->SetBranchAddress("Nstrpx",&Nstrpx);
153 if(fhBookTree->GetBranch("Npstripx"))fhBookTree->SetBranchAddress("Npstripx",Npstripx);
154 if(fhBookTree->GetBranch("Ntstripx"))fhBookTree->SetBranchAddress("Ntstripx",Ntstripx);
155 if(fhBookTree->GetBranch("Istripx"))fhBookTree->SetBranchAddress("Istripx",Istripx);
156 if(fhBookTree->GetBranch("Qstripx"))fhBookTree->SetBranchAddress("Qstripx",Qstripx);
157 if(fhBookTree->GetBranch("Xstripx"))fhBookTree->SetBranchAddress("Xstripx",Xstripx);
158 if(fhBookTree->GetBranch("Nstrpy"))fhBookTree->SetBranchAddress("Nstrpy",&Nstrpy);
159 if(fhBookTree->GetBranch("Npstripy"))fhBookTree->SetBranchAddress("Npstripy",Npstripy);
160 if(fhBookTree->GetBranch("Ntstripy"))fhBookTree->SetBranchAddress("Ntstripy",Ntstripy);
161 if(fhBookTree->GetBranch("Istripy"))fhBookTree->SetBranchAddress("Istripy",Istripy);
162 if(fhBookTree->GetBranch("Qstripy"))fhBookTree->SetBranchAddress("Qstripy",Qstripy);
163 if(fhBookTree->GetBranch("Ystripy"))fhBookTree->SetBranchAddress("Ystripy",Ystripy);
164 if(fhBookTree->GetBranch("Nthcali"))fhBookTree->SetBranchAddress("Nthcali",&Nthcali);
165 if(fhBookTree->GetBranch("Icaplane"))fhBookTree->SetBranchAddress("Icaplane",Icaplane);
166 if(fhBookTree->GetBranch("Icastrip"))fhBookTree->SetBranchAddress("Icastrip",Icastrip);
167 if(fhBookTree->GetBranch("Icamod"))fhBookTree->SetBranchAddress("Icamod",Icamod);
168 if(fhBookTree->GetBranch("Enestrip"))fhBookTree->SetBranchAddress("Enestrip",Enestrip);
169 if(fhBookTree->GetBranch("Nthcal"))fhBookTree->SetBranchAddress("Nthcal",&Nthcal);
170 if(fhBookTree->GetBranch("Icapl"))fhBookTree->SetBranchAddress("Icapl",Icapl);
171 if(fhBookTree->GetBranch("Icasi"))fhBookTree->SetBranchAddress("Icasi",Icasi);
172 if(fhBookTree->GetBranch("Icast"))fhBookTree->SetBranchAddress("Icast",Icast);
173 if(fhBookTree->GetBranch("Xincal"))fhBookTree->SetBranchAddress("Xincal",Xincal);
174 if(fhBookTree->GetBranch("Yincal"))fhBookTree->SetBranchAddress("Yincal",Yincal);
175 if(fhBookTree->GetBranch("Zincal"))fhBookTree->SetBranchAddress("Zincal",Zincal);
176 if(fhBookTree->GetBranch("Erelcal"))fhBookTree->SetBranchAddress("Erelcal",Erelcal);
177 if(fhBookTree->GetBranch("Nthnd"))fhBookTree->SetBranchAddress("Nthnd",&Nthnd);
178 if(fhBookTree->GetBranch("Itubend"))fhBookTree->SetBranchAddress("Itubend",Itubend);
179 if(fhBookTree->GetBranch("Iparnd"))fhBookTree->SetBranchAddress("Iparnd",Iparnd);
180 if(fhBookTree->GetBranch("Xinnd"))fhBookTree->SetBranchAddress("Xinnd",Xinnd);
181 if(fhBookTree->GetBranch("Yinnd"))fhBookTree->SetBranchAddress("Yinnd",Yinnd);
182 if(fhBookTree->GetBranch("Zinnd"))fhBookTree->SetBranchAddress("Zinnd",Zinnd);
183 if(fhBookTree->GetBranch("Xoutnd"))fhBookTree->SetBranchAddress("Xoutnd",Xoutnd);
184 if(fhBookTree->GetBranch("Youtnd"))fhBookTree->SetBranchAddress("Youtnd",Youtnd);
185 if(fhBookTree->GetBranch("Zoutnd"))fhBookTree->SetBranchAddress("Zoutnd",Zoutnd);
186 if(fhBookTree->GetBranch("Erelnd"))fhBookTree->SetBranchAddress("Erelnd",Erelnd);
187 if(fhBookTree->GetBranch("Timend"))fhBookTree->SetBranchAddress("Timend",Timend);
188 if(fhBookTree->GetBranch("Pathnd"))fhBookTree->SetBranchAddress("Pathnd",Pathnd);
189 if(fhBookTree->GetBranch("P0nd"))fhBookTree->SetBranchAddress("P0nd",P0nd);
190 if(fhBookTree->GetBranch("Nthcard"))fhBookTree->SetBranchAddress("Nthcard",&Nthcard);
191 if(fhBookTree->GetBranch("Iparcard"))fhBookTree->SetBranchAddress("Iparcard",Iparcard);
192 if(fhBookTree->GetBranch("Icard"))fhBookTree->SetBranchAddress("Icard",Icard);
193 if(fhBookTree->GetBranch("Xincard"))fhBookTree->SetBranchAddress("Xincard",Xincard);
194 if(fhBookTree->GetBranch("Yincard"))fhBookTree->SetBranchAddress("Yincard",Yincard);
195 if(fhBookTree->GetBranch("Zincard"))fhBookTree->SetBranchAddress("Zincard",Zincard);
196 if(fhBookTree->GetBranch("Xoutcard"))fhBookTree->SetBranchAddress("Xoutcard",Xoutcard);
197 if(fhBookTree->GetBranch("Youtcard"))fhBookTree->SetBranchAddress("Youtcard",Youtcard);
198 if(fhBookTree->GetBranch("Zoutcard"))fhBookTree->SetBranchAddress("Zoutcard",Zoutcard);
199 if(fhBookTree->GetBranch("Erelcard"))fhBookTree->SetBranchAddress("Erelcard",Erelcard);
200 if(fhBookTree->GetBranch("Timecard"))fhBookTree->SetBranchAddress("Timecard",Timecard);
201 if(fhBookTree->GetBranch("Pathcard"))fhBookTree->SetBranchAddress("Pathcard",Pathcard);
202 if(fhBookTree->GetBranch("P0card"))fhBookTree->SetBranchAddress("P0card",P0card);
203 // fhBookTree->SetBranchStatus("*",0); //modified by E.Vannuccini 03/08
204 }
205
206 void Digitizer::Close(){
207 delete fhBookTree;
208 }
209
210 void Digitizer::Loop() {
211 //
212 // opens the raw output file and loops over the events
213 //
214 fOutputfile.open(fFilename, ios::out | ios::binary);
215 //fOutputfile.open(Form("Output%s",fFilename), ios::out | ios::binary);
216 //
217 // Load in memory and save at the beginning of file the calorimeter calibration
218 //
219 CaloLoadCalib();
220 DigitizeCALOCALIB();
221
222 // load, digitize and write tracker calibration
223 LoadTrackCalib();
224
225 DigitizeTrackCalib(1);
226 UInt_t length=fTracklength*2;
227 DigitizePSCU(length,0x12,fDataPSCU);
228 AddPadding();
229 WriteTrackCalib();
230
231 DigitizeTrackCalib(2);
232 length=fTracklength*2;
233 DigitizePSCU(length,0x13,fDataPSCU);
234 AddPadding();
235 WriteTrackCalib();
236
237 DigitizeRunHeader();
238 WriteRunHeader();
239
240 LoadMipCor(); // some initialization of parameters -not used now-
241 // end loading, digitizing and writing tracker calibration
242 // TOF ------ read calibration file (get A1, A2, lambda1, lambda2)
243 const int np=48;
244 Float_t *atte1,*atte2,*lambda1,*lambda2;
245 atte1=(float *)malloc(np *sizeof(float));
246 atte2=(float *)malloc(np *sizeof(float));
247 lambda1=(float *)malloc(np *sizeof(float));
248 lambda2=(float *)malloc(np *sizeof(float));
249 LoadTOFCalib(np,atte1,atte2,lambda1,lambda2);
250 //end tof calib
251 //
252 // loops over the events
253 //
254
255 Int_t nentries = fhBookTree->GetEntriesFast();
256 Long64_t nbytes = 0;
257 for (Int_t i=0; i<nentries;i++) {
258 //
259 nbytes += fhBookTree->GetEntry(i);
260 // read detectors sequentially:
261 // http://www.ts.infn.it/fileadmin/documents/physics/experiments/wizard/cpu/gen_arch/RM_Acquisition.pdf
262 // on pamelatov: /cvs/yoda/techmodel/physics/NeutronDetectorReader.cpp
263 //DigitizeTRIGGER();
264 DigitizeTOF(np,atte1,atte2,lambda1,lambda2);
265 DigitizeAC();
266 DigitizeCALO();
267 DigitizeTrack();
268 DigitizeS4();
269 DigitizeND();
270 //
271 // Add padding to 64 bits
272 //
273 AddPadding();
274 //
275 // Create CPU header, we need packet type (0x10 = physics data) and packet length.
276 //
277 UInt_t length=2*(fCALOlength+fACbuffer+fTracklength+fNDbuffer+fS4buffer)+fPadding+fTOFbuffer+fTRIGGERbuffer;
278 //UInt_t length=2*(fCALOlength+fACbuffer+fTracklength+fNDbuffer)+fPadding+fTOFbuffer+fTRIGGERbuffer;
279 DigitizePSCU(length,0x10,fDataPSCU);
280 if ( !i%100 ) std::cout << "writing event " << i << endl;
281 WriteData();
282 };
283
284 DigitizeRunTrailer();
285 WriteRunTrailer();
286
287 fOutputfile.close();
288 std::cout << "files closed" << endl << flush;
289
290 };
291
292
293 //void Digitizer::DigitizeTRIGGER() {
294 //fDataTrigger: 152 bytes
295 // corrected 30/11/'07 SO (was 153)
296 //for (Int_t j=0; j < 152; j++)
297 // fDataTrigger[j]=0x00;
298 //};
299
300
301
302 void Digitizer::ReadData(){
303
304 UShort_t InData[64];
305
306 // for debuggigng purposes only, write your own routine if you like (many
307 // hardwired things.
308
309 ifstream InputFile;
310
311 // if (!InputFile) {
312
313 // std::cout << "ERROR" << endl;
314 // // An error occurred!
315 // // myFile.gcount() returns the number of bytes read.
316 // // calling myFile.clear() will reset the stream state
317 // // so it is usable again.
318 // };
319
320
321
322 //InputFile.seekg(0);
323
324 InputFile.open(fFilename, ios::in | ios::binary);
325 // fOutputfile.seekg(0);
326 if (!InputFile.is_open()) std::cout << "ERROR" << endl;
327
328 InputFile.seekg(0);
329
330 for (Int_t k=0; k<=1000; k++){
331 InputFile.read(reinterpret_cast<char*>(InData),384*sizeof(UShort_t));
332
333 std::cout << "Read back: " << endl << endl;
334
335 for (Int_t i=0; i<=384; i++){
336 printf("%4x ", InData[i]);
337 if ((i+1)%8 ==0) cout << endl;
338 }
339
340 }
341 cout << endl;
342 InputFile.close();
343
344 };

  ViewVC Help
Powered by ViewVC 1.1.23