/[PAMELA software]/S4ND/flight/Level2/ND/ND_LEVEL2.cpp
ViewVC logotype

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

  ViewVC Help
Powered by ViewVC 1.1.23