/[PAMELA software]/S4ND/flight/Level2/S4/S4_LEVEL2.cpp
ViewVC logotype

Annotation of /S4ND/flight/Level2/S4/S4_LEVEL2.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (hide annotations) (download) (vendor branch)
Thu Apr 27 12:44:14 2006 UTC (18 years, 7 months ago) by malvezzi
Branch: Level2, MAIN
CVS Tags: LEVEL2S4ND, HEAD
Changes since 1.1: +0 -0 lines
processamento livello2 27 aprile 2006

1 malvezzi 1.1 // Script name: S4_LEVEL2.c
2     // Function name: S4LEVEL2
3     //
4     // Author: Malvezzi-Marcelli
5     // Version 2.0 - 26/04/2006
6     //
7     //
8    
9     //--- PAMELA classes --------------------------
10     #include <PamelaRun.h>
11     #include <PscuHeader.h>
12     #include <PscuEvent.h>
13     #include <EventHeader.h>
14     #include <RegistryEvent.h>
15     #include <CalibS4Event.h>
16     #include <physics/S4/S4Event.h>
17     #include <yodaUtility.h>
18    
19     #include <iostream>
20     #include <fstream>
21     #include <cstring>
22     #include <stdio.h>
23    
24     //---- standard root classes ---------------
25     #include <TTree.h>
26     #include <TClassEdit.h>
27     #include <TObject.h>
28     #include <TList.h>
29     #include <TSystem.h>
30     #include <TSystemDirectory.h>
31     #include <TString.h>
32     #include <TFile.h>
33     #include <TClass.h>
34     #include <TCanvas.h>
35     #include <TH1.h>
36     #include <TH1F.h>
37     #include <TH2D.h>
38     #include <TLatex.h>
39     #include <TPad.h>
40     #include <stdlib.h>
41     #include <math.h>
42     //---- classi per il DB ------
43     #include <TSQLServer.h>
44     #include <TSQLResult.h>
45     #include <TSQLRow.h>
46    
47     //---- classi per il livello 2 di S4 -----
48     #include <S4Level2.h>
49    
50    
51    
52     #define DEBUG 0
53     using namespace std;
54    
55     //---------- S4 level2 function ----------------------------------------------
56    
57     void S4LEVEL2(ULong64_t IDrun, TString outDir, TString processFolder, TString processFile, const char *host, const char *user, const char *psw){
58    
59     //----- Create tree, name = S4; number of branch 2 -------------
60     pamela::S4Level2 *S4l2 = new pamela::S4Level2();
61     Float_t S4adc=0, S4calibrated=0;
62    
63     stringstream filename;
64     filename.str("");
65     if(outDir=""){
66     filename<<IDrun<<".Level2.root";
67     }else{
68     filename<< outDir <<IDrun <<".Level2.root";
69     }
70    
71    
72     TFile *f=0;
73     TTree *trS4L2 =0;
74    
75     fstream myfile;
76     myfile.open(filename.str().c_str());
77     if(!myfile){
78     f = new TFile(filename.str().c_str(),"recreate");
79     trS4L2 = new TTree("S4","S4 level2 data");
80     trS4L2->Branch("S4calibrated", &S4l2->S4calibrated, "S4calibrated/F");
81     trS4L2->Branch("S4adc", &S4l2->S4adc, "S4adc/F");
82     if(!f){
83     printf("ERROR: No Level2 input file. \n");
84     exit(-500);
85     }
86     }else{
87     f = new TFile(filename.str().c_str(),"update");
88     if(!f){
89     printf("ERROR: Cannot open Level2 input file. \n");
90     exit(-501);
91     }
92     trS4L2 = (TTree *)f->Get("S4");
93     if(!trS4L2){
94     trS4L2 = new TTree("S4","S4 level2 data");
95     trS4L2->Branch("S4calibrated", &S4l2->S4calibrated, "S4calibrated/F");
96     trS4L2->Branch("S4adc", &S4l2->S4adc, "S4adc/F");
97     }else{
98     printf("WARNING: you are re-processing data; \n At this moment this process is not possible. \n ");
99     exit(-599);
100     }
101    
102     }
103    
104    
105     //------------------ Quary DB --------------------------------
106     //
107     // MySQL variables
108     //
109    
110     TSQLServer *conn;
111     TSQLResult *result;
112     TSQLRow *row;
113    
114    
115     //
116     //to open connection
117     //
118     conn = TSQLServer::Connect(host, user, psw);
119    
120     if(!conn){
121     printf("No DB connection (wrong USRN/PWD?). \n");
122     exit(-2);
123     }
124    
125     //
126     // Here we query the DB to obtain the informations needed to process the run number "IDrun"
127     //
128    
129     //
130     //define variables where to store DB variables (unsigned long long integers,
131     //when set to a number use ULL to store the correct number).
132     //
133     ULong64_t RunHeaderTime = 0ULL;
134     ULong64_t RunTrailerTime = 0ULL;
135     ULong64_t IdRegRunHeader = 0ULL;
136     ULong64_t Ev_Reg_Phys_From = 0ULL;
137     ULong64_t Ev_Reg_Phys_To = 0ULL;
138     Double_t ParamFit0 = 0ULL;
139     Double_t ParamFit1 = 0ULL;
140     ULong64_t atime = 0ULL;
141     Int_t i =0;
142    
143     //
144     // first query: which is the ID of the file which contains the run number "IDrun"?
145     // and at what absolute time the run header and run trailer time were recorded?
146     //
147     stringstream myquery;
148     myquery.str("");
149     myquery << "select ID_REG_RUN, RUNHEADER_TIME, RUNTRAILER_TIME, EV_REG_PHYS_TO, EV_REG_PHYS_FROM from GL_RUN where ID= " << IDrun;
150     myquery <<";";
151     result = conn->Query(myquery.str().c_str());
152    
153     row = result->Next();
154     if (row == NULL) {
155     printf("ERROR: Not result from GL_RUN \n");
156     f->Close();
157     exit(-5);
158     }
159     IdRegRunHeader = (ULong64_t)atof(row->GetField(0));
160     RunHeaderTime = (ULong64_t)atof(row->GetField(1));
161     RunTrailerTime = (ULong64_t)atof(row->GetField(2));
162     Ev_Reg_Phys_From = (ULong64_t)atof(row->GetField(4));
163     Ev_Reg_Phys_To = (ULong64_t)atof(row->GetField(3));
164     delete result;
165    
166     //
167     // second query: which is the value of paramfit relative to the calibration?
168     //
169     myquery.str("");
170     myquery <<"select PARAM_FIT0,PARAM_FIT1 from GL_S4_CALIB where FROM_TIME <"<< RunHeaderTime <<" and TO_TIME >"<< RunTrailerTime;
171     myquery <<";";
172     result = conn->Query(myquery.str().c_str());
173    
174     row = result->Next();
175     if (row == NULL) {
176     printf("ERROR: Not result from GL_S4_CALIB \n");
177     f->Close();
178     exit(-502);
179     }
180     ParamFit0 = atof(row->GetField(0));
181     ParamFit1 = atof(row->GetField(1));
182    
183     delete result;
184    
185    
186     //
187     // third query: where can I find the file with the id I have just recovered?
188     //
189     stringstream Path;
190     Path.str("");
191     stringstream Name;
192     Name.str("");
193     stringstream base;
194     base.str("");
195     myquery.str("");
196     myquery << "select PATH, NAME from GL_ROOT where ID=" << IdRegRunHeader ;
197     myquery <<";";
198     result = conn->Query(myquery.str().c_str());
199    
200    
201     row = result->Next();
202     if (row == NULL) {
203     printf("ERROR: Not result from GL_ROOT \n");
204     f->Close();
205     exit(-6);
206     }
207    
208     Path << row->GetField(0);
209     Name << row->GetField(1);
210     delete result;
211    
212     base << Path.str().c_str() <<"/"<<Name.str().c_str();
213    
214    
215    
216    
217     //---------------Open File from yoda files----------------------------
218     TFile *file = new TFile(base.str().c_str());
219     if (!file){
220     printf("ERROR: Level 0 file not found \n");
221     file->Close();
222     f->Close();
223     exit(-6);
224     }
225    
226    
227    
228     //---- Takes the branch of the header and calibration file and matchs the first with S4 file ----------------
229     pamela::EventHeader *eh = 0;
230     pamela::RegistryEvent *reg=0;
231     pamela::S4::S4Event *s4e = 0;
232     pamela::S4::S4Event *s4Record = 0;
233     pamela::PscuHeader *ph = 0;
234    
235     TTree *tree = (TTree*)file->Get("Physics");
236     TBranch *regBr = tree->GetBranch("Registry");
237     TBranch *headBr = tree->GetBranch("Header");
238     TBranch *S4Br = tree->GetBranch("S4");
239    
240     if(!tree){
241     printf("ERROR: No Physics TTree in Level0 file \n");
242     file->Close();
243     f->Close();
244     exit(-7);
245     }
246    
247     tree->SetBranchAddress("Registry", &reg);
248     tree->SetBranchAddress("S4", &s4e);
249     tree->SetBranchAddress("Header", &eh);
250    
251     if(!headBr){
252     printf("ERROR: No Header TBranch in Level0 file \n");
253     file->Close();
254     f->Close();
255     exit(-8);
256     }
257     if(!regBr){
258     printf("ERROR: No Registry TBranch in Level0 file \n");
259     file->Close();
260     f->Close();
261     exit(-9);
262     }
263    
264     Long64_t nevents = headBr->GetEntries();
265     Long64_t regevents = regBr->GetEntries();
266    
267     if (nevents<=0 || regevents<=0) {
268     printf("\n nevents = %i \n", nevents);
269     printf("\n regevents = %i \n", regevents);
270     printf("ERROR: Level0 file is empty \n");
271     file->Close();
272     f->Close();
273     exit(-11);
274     }
275    
276     //----- Starts the cicle to fill root file ----------------------
277     for (Int_t re = Ev_Reg_Phys_From; re < ((Ev_Reg_Phys_To)+1); re++){
278     regBr->GetEntry(re);
279     atime = reg->absTime;
280     if ( atime > RunTrailerTime || atime < RunHeaderTime ) continue ;
281     i = reg->event;
282     headBr->GetEntry(i);
283     S4Br->GetEntry(i);
284     if (s4e->unpackError == 1) continue;
285     S4l2->S4adc = s4e->S4_DATA;
286     if ((s4e->S4_DATA) > 31 ){
287     S4l2->S4calibrated = ParamFit0*((s4e->S4_DATA)-32)+ParamFit1;
288     }else{
289     S4l2->S4calibrated = 0;
290     }
291    
292     //cout<<S4l2->S4adc <<" "<<S4l2->S4calibrated<<"\n";
293    
294     trS4L2->Fill(); // Fill tree S4Level2 in the root file
295    
296     }
297    
298     conn->Close();
299     file->Close(); // Close file before go to next one
300     f->Write(); // Save the root file
301     f->Close();
302    
303     }
304    
305    
306     //------------------------- main function --------------------------
307    
308     int main(int argc, char* argv[]){
309     ULong64_t IDrun;
310     TString outDir = "./";
311     TString processFile = "";
312     TString processFolder = "";
313     const char *host = "mysql://localhost/pamelaprod"; //
314     const char *user = "anonymous"; // MySQL variables
315     const char *psw = ""; //
316    
317    
318     if (argc < 2){
319     printf("ERROR: You have to insert at least the number of run to be processed \n");
320     printf("Try '--help' for more information. \n");
321     exit(-1);
322     }
323    
324     if (!strcmp(argv[1], "--help")){
325     printf( "Usage: S4_Level2 FILE [OPTION] \n");
326     printf( "\t --help Print this help and exit \n");
327     printf( "\t -IDrun The number of run to process \n");
328     printf( "\t -outDir Path where to put the output [default ./] \n");
329     printf( "\t -processFolder Path where to put useful files [default ""] \n");
330     printf( "\t -processFile Name of the input file [default ID_RUN.Level2.root]; \n this parameter is necessary for the re-processing");
331     printf( "\t -host Name for the host [default = mysql://localhost/pamelaprod] \n");
332     printf( "\t -user Username for the DB [default = anonymous] \n");
333     printf( "\t -psw Password for the DB \n");
334     exit(-12);
335     }
336    
337     char* path=argv[1];
338    
339     for (int i = 2; i < argc; i++){
340    
341     if (!strcmp(argv[i], "-IDrun")){
342     if (++i >= argc){
343     printf( "ERROR: -IDrun needs arguments. \n");
344     printf( "Try '--help' for more information. \n");
345     exit(-1);
346     }
347     }
348     else{
349     IDrun = atoi(argv[i]);
350     continue;
351     }
352    
353     if (!strcmp(argv[i], "-outDir")){
354     if (++i >= argc){
355     printf( "ERROR: -outDir needs arguments. \n");
356     printf( "Try '--help' for more information. \n");
357     exit(-3);
358     }
359     } else {
360     outDir = argv[i];
361     continue;
362     }
363    
364     if (!strcmp(argv[i], "-processFolder")){
365     if (++i >= argc){
366     printf( "ERROR: -processFolder needs arguments. \n");
367     printf( "Try '--help' for more information. \n");
368     exit(-3);
369     }
370     } else {
371     processFolder = argv[i];
372     continue;
373     }
374    
375     if (!strcmp(argv[i], "-processFile")){
376     if (++i >= argc){
377     printf( "ERROR: -processFile needs arguments. \n");
378     printf( "Try '--help' for more information. \n");
379     exit(-3);
380     }
381     } else {
382     processFile = argv[i];
383     if((IDrun == 0) && (processFile = "")){
384     printf( "ERROR: -processFile needs arguments. \n");
385     exit(-4);
386     }
387     continue;
388     }
389    
390     if (!strcmp(argv[i], "-host")){
391     if (++i >= argc){
392     printf( "ERROR: -host needs arguments. \n");
393     printf( "Try '--help' for more information. \n");
394     exit(-3);
395     }
396     }
397     else{
398     host = argv[i];
399     continue;
400     }
401    
402     if (!strcmp(argv[i], "-user")){
403     if (++i >= argc){
404     printf( "ERROR: -user needs arguments. \n");
405     printf( "Try '--help' for more information. \n");
406     exit(-3);
407     }
408     } else {
409     user = argv[i];
410     continue;
411     }
412    
413     if (!strcmp(argv[i], "-psw")){
414     if (++i >= argc){
415     printf( "ERROR: -psw needs arguments. \n");
416     printf( "Try '--help' for more information. \n");
417     exit(-3);
418     }
419     } else {
420     psw = argv[i];
421     continue;
422     }
423     }
424    
425     S4LEVEL2(IDrun, outDir, processFolder, processFile, host, user, psw);
426    
427     exit(0);
428     }

  ViewVC Help
Powered by ViewVC 1.1.23