/[PAMELA software]/DarthVader/OrbitalInfo/src/OrbitalInfoCore.cpp
ViewVC logotype

Annotation of /DarthVader/OrbitalInfo/src/OrbitalInfoCore.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.58 - (hide annotations) (download)
Tue May 15 15:10:42 2012 UTC (12 years, 9 months ago) by mocchiut
Branch: MAIN
Changes since 1.57: +4 -3 lines
Printout commented

1 mocchiut 1.1 // C/C++ headers
2     //
3     #include <fstream>
4     #include <string.h>
5     #include <iostream>
6     #include <cstring>
7     #include <stdio.h>
8     //
9     // ROOT headers
10     //
11 mocchiut 1.44 //#include <TCanvas.h>
12     //#include <TH2F.h> //for test only. Vitaly.
13     //#include <TF1.h>
14    
15 mocchiut 1.1 #include <TTree.h>
16     #include <TClassEdit.h>
17     #include <TObject.h>
18     #include <TList.h>
19 mocchiut 1.5 #include <TArrayI.h>
20 mocchiut 1.1 #include <TSystem.h>
21     #include <TSystemDirectory.h>
22     #include <TString.h>
23     #include <TFile.h>
24     #include <TClass.h>
25     #include <TSQLServer.h>
26     #include <TSQLRow.h>
27     #include <TSQLResult.h>
28     //
29 mocchiut 1.8 // RunInfo header
30     //
31     #include <RunInfo.h>
32     #include <GLTables.h>
33     //
34 mocchiut 1.1 // YODA headers
35     //
36     #include <PamelaRun.h>
37     #include <PscuHeader.h>
38     #include <PscuEvent.h>
39     #include <EventHeader.h>
40 mocchiut 1.15 #include <mcmd/McmdEvent.h>
41     #include <mcmd/McmdRecord.h>
42 mocchiut 1.1 //
43     // This program headers
44     //
45     #include <OrbitalInfo.h>
46 mocchiut 1.7 #include <OrbitalInfoVerl2.h>
47 mocchiut 1.1 #include <OrbitalInfoCore.h>
48 mocchiut 1.15 #include <InclinationInfo.h>
49 mocchiut 1.1
50 mocchiut 1.40
51 mocchiut 1.1 using namespace std;
52    
53     //
54     // CORE ROUTINE
55     //
56     //
57 mocchiut 1.25 int OrbitalInfoCore(UInt_t run, TFile *file, GL_TABLES *glt, Int_t OrbitalInfoargc, char *OrbitalInfoargv[]){
58 mocchiut 1.15 //
59 mocchiut 1.1 Int_t i = 0;
60 mocchiut 1.25 TString host = glt->CGetHost();
61     TString user = glt->CGetUser();
62     TString psw = glt->CGetPsw();
63     TSQLServer *dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
64 mocchiut 1.1 //
65 mocchiut 1.26 stringstream myquery;
66     myquery.str("");
67     myquery << "SET time_zone='+0:00'";
68     dbc->Query(myquery.str().c_str());
69     //
70 mocchiut 1.16 TString processFolder = Form("OrbitalInfoFolder_%u",run);
71 mocchiut 1.1 //
72     // Set these to true to have a very verbose output.
73     //
74     Bool_t debug = false;
75     //
76     Bool_t verbose = false;
77 mocchiut 1.30 //
78 mocchiut 1.31 Bool_t standalone = false;
79 mocchiut 1.30 //
80 mocchiut 1.1 if ( OrbitalInfoargc > 0 ){
81     i = 0;
82     while ( i < OrbitalInfoargc ){
83     if ( !strcmp(OrbitalInfoargv[i],"-processFolder") ) {
84     if ( OrbitalInfoargc < i+1 ){
85     throw -3;
86     };
87     processFolder = (TString)OrbitalInfoargv[i+1];
88     i++;
89     };
90     if ( (!strcmp(OrbitalInfoargv[i],"--debug")) || (!strcmp(OrbitalInfoargv[i],"-g")) ) {
91     verbose = true;
92 mocchiut 1.15 debug = true;
93 mocchiut 1.1 };
94     if ( (!strcmp(OrbitalInfoargv[i],"--verbose")) || (!strcmp(OrbitalInfoargv[i],"-v")) ) {
95     verbose = true;
96     };
97 mocchiut 1.30 if ( (!strcmp(OrbitalInfoargv[i],"--standalone")) ) {
98     standalone = true;
99     };
100     if ( (!strcmp(OrbitalInfoargv[i],"--calculate-pitch")) ) {
101     standalone = false;
102     };
103 mocchiut 1.1 i++;
104     };
105     };
106     //
107     const char* outDir = gSystem->DirName(gSystem->DirName(file->GetPath()));
108     //
109     TTree *OrbitalInfotr = 0;
110 mocchiut 1.5 UInt_t nevents = 0;
111 mocchiut 1.15 UInt_t neventsm = 0;
112 mocchiut 1.1 //
113     // variables needed to reprocess data
114     //
115 mocchiut 1.6 Long64_t maxsize = 10000000000LL;
116     TTree::SetMaxTreeSize(maxsize);
117     //
118 mocchiut 1.1 TString OrbitalInfoversion;
119     ItoRunInfo *runinfo = 0;
120 mocchiut 1.5 TArrayI *runlist = 0;
121 mocchiut 1.1 TTree *OrbitalInfotrclone = 0;
122     Bool_t reproc = false;
123     Bool_t reprocall = false;
124     UInt_t nobefrun = 0;
125     UInt_t noaftrun = 0;
126     UInt_t numbofrun = 0;
127     stringstream ftmpname;
128     TString fname;
129 mocchiut 1.5 UInt_t totfileentries = 0;
130 mocchiut 1.15 UInt_t idRun = 0;
131 pam-fi 1.53 UInt_t anni5 = 60 * 60 * 24 * 365 * 5 ;//1576800
132 mocchiut 1.15 //
133     // My variables. Vitaly.
134     //
135 mocchiut 1.44 // UInt_t oi = 0;
136 mocchiut 1.15 Int_t tmpSize = 0;
137 mocchiut 1.1 //
138     // variables needed to handle error signals
139     //
140     Int_t code = 0;
141     Int_t sgnl;
142     //
143     // OrbitalInfo classes
144     //
145     OrbitalInfo *orbitalinfo = new OrbitalInfo();
146     OrbitalInfo *orbitalinfoclone = new OrbitalInfo();
147 mocchiut 1.44
148 mocchiut 1.1 //
149     // define variables for opening and reading level0 file
150     //
151     TFile *l0File = 0;
152     TTree *l0tr = 0;
153 mocchiut 1.37 // TTree *l0trm = 0;
154     TChain *ch = 0;
155 mocchiut 1.2 // EM: open also header branch
156     TBranch *l0head = 0;
157     pamela::EventHeader *eh = 0;
158     pamela::PscuHeader *ph = 0;
159 mocchiut 1.15 pamela::McmdEvent *mcmdev = 0;
160     pamela::McmdRecord *mcmdrc = 0;
161 mocchiut 1.2 // end EM
162 mocchiut 1.15
163     // pamela::RunHeaderEvent *reh = new pamela::RunHeaderEvent;
164     // pamela::EventHeader *eH = new pamela::EventHeader;
165    
166 mocchiut 1.1 //
167     // Define other basic variables
168     //
169     UInt_t procev = 0;
170     stringstream file2;
171     stringstream file3;
172     stringstream qy;
173     Int_t totevent = 0;
174 mocchiut 1.5 UInt_t atime = 0;
175     UInt_t re = 0;
176 mocchiut 1.15 UInt_t ik = 0;
177 mocchiut 1.7
178     // Position
179     Float_t lon, lat, alt;
180    
181     //
182     // IGRF stuff
183     //
184 mocchiut 1.40 Float_t dimo = 0.0; // dipole moment (computed from dat files)
185     Float_t bnorth, beast, bdown, babs;
186     Float_t xl; // L value
187     Float_t icode; // code value for L accuracy (see fortran code)
188     Float_t bab1; // What's the difference with babs?
189     Float_t stps = 0.005; // step size for field line tracing
190     Float_t bdel = 0.01; // required accuracy
191     Float_t bequ; // equatorial b value (also called b_0)
192     Bool_t value = 0; // false if bequ is not the minimum b value
193     Float_t rr0; // equatorial radius normalized to earth radius
194 mocchiut 1.7
195 mocchiut 1.1 //
196     // Working filename
197     //
198     TString outputfile;
199     stringstream name;
200     name.str("");
201     name << outDir << "/";
202     //
203     // temporary file and folder
204     //
205     TFile *tempfile = 0;
206     TTree *tempOrbitalInfo = 0;
207     stringstream tempname;
208     stringstream OrbitalInfofolder;
209 mocchiut 1.23 Bool_t myfold = false;
210 mocchiut 1.1 tempname.str("");
211     tempname << outDir;
212     tempname << "/" << processFolder.Data();
213     OrbitalInfofolder.str("");
214     OrbitalInfofolder << tempname.str().c_str();
215     tempname << "/OrbitalInfotree_run";
216     tempname << run << ".root";
217 mocchiut 1.40 UInt_t totnorun = 0;
218 mocchiut 1.1 //
219     // DB classes
220     //
221     GL_ROOT *glroot = new GL_ROOT();
222 mocchiut 1.5 GL_TIMESYNC *dbtime = 0;
223 mocchiut 1.7 GL_TLE *gltle = new GL_TLE();
224 mocchiut 1.15 //
225     //Quaternions classes
226     //
227 mocchiut 1.57 Quaternions *L_QQ_Q_l_lower = 0;
228     InclinationInfo *RYPang_lower = 0;
229     Quaternions *L_QQ_Q_l_upper = 0;
230     InclinationInfo *RYPang_upper = 0;
231 mocchiut 1.15
232     cEci eCi;
233    
234 mocchiut 1.7 // Initialize fortran routines!!!
235 mocchiut 1.52 Int_t ltp1 = 0;
236 mocchiut 1.7 Int_t ltp2 = 0;
237     Int_t ltp3 = 0;
238 mocchiut 1.55 // Int_t uno = 1;
239     // const char *niente = " ";
240 mocchiut 1.7 GL_PARAM *glparam = new GL_PARAM();
241 mocchiut 1.10 GL_PARAM *glparam2 = new GL_PARAM();
242 mocchiut 1.52 GL_PARAM *glparam3 = new GL_PARAM();
243 mocchiut 1.44
244 mocchiut 1.30 //
245 mocchiut 1.44 // Orientation variables. Vitaly
246 mocchiut 1.30 //
247     UInt_t evfrom = 0;
248     UInt_t jumped = 0;
249     Int_t itr = -1;
250     Double_t A1;
251     Double_t A2;
252     Double_t A3;
253     Double_t Px = 0;
254     Double_t Py = 0;
255     Double_t Pz = 0;
256     TTree *ttof = 0;
257     ToFLevel2 *tof = new ToFLevel2();
258     OrientationInfo *PO = new OrientationInfo();
259     Int_t nz = 6;
260     Float_t zin[6];
261     Int_t nevtofl2 = 0;
262 mocchiut 1.44 if ( verbose ) cout<<"Reading quaternions external file"<<endl;
263     cout.setf(ios::fixed,ios::floatfield);
264     /******Reading recovered quaternions...*********/
265     vector<Double_t> recqtime;
266     vector<Float_t> recq0;
267     vector<Float_t> recq1;
268     vector<Float_t> recq2;
269     vector<Float_t> recq3;
270     Float_t Norm = 1;
271     Int_t parerror=glparam->Query_GL_PARAM(1,303,dbc); // parameters stored in DB in GL_PRAM table
272 mocchiut 1.45 ifstream in((glparam->PATH+glparam->NAME).Data(),ios::in);
273 mocchiut 1.44 if ( parerror<0 ) {
274     code = parerror;
275 mocchiut 1.55 goto closeandexit;
276 pam-mep 1.54 }
277 mocchiut 1.44 while(!in.eof()){
278     recqtime.resize(recqtime.size()+1);
279     Int_t sizee = recqtime.size();
280     recq0.resize(sizee);
281     recq1.resize(sizee);
282     recq2.resize(sizee);
283     recq3.resize(sizee);
284     in>>recqtime[sizee-1];
285     in>>recq0[sizee-1];
286     in>>recq1[sizee-1];
287     in>>recq2[sizee-1];
288     in>>recq3[sizee-1];
289     in>>Norm;
290     }
291     if ( verbose ) cout<<"We have read recovered data"<<endl;
292    
293 mocchiut 1.55
294     // IGRF stuff moved inside run loop!
295    
296 mocchiut 1.30 for (Int_t ip=0;ip<nz;ip++){
297     zin[ip] = tof->GetZTOF(tof->GetToFPlaneID(ip));
298     };
299     //
300     if ( !standalone ){
301     //
302     // Does it contain the Tracker tree?
303     //
304     ttof = (TTree*)file->Get("ToF");
305     if ( !ttof ) {
306     if ( verbose ) printf(" OrbitalInfo - ERROR: no tof tree\n");
307     code = -900;
308     goto closeandexit;
309     };
310     ttof->SetBranchAddress("ToFLevel2",&tof);
311     nevtofl2 = ttof->GetEntries();
312     };
313 mocchiut 1.1 //
314     // Let's start!
315     //
316     // As a first thing we must check what we have to do: if run = 0 we must process all events in the file has been passed
317     // if run != 0 we must process only that run but first we have to check if the tree MyDetector2 already exist in the file
318     // if it exists we are reprocessing data and we must delete that entries, if not we must create it.
319     //
320 mocchiut 1.5 if ( run == 0 ) reproc = true;
321 mocchiut 1.1 //
322     //
323     // Output file is "outputfile"
324     //
325     if ( !file->IsOpen() ){
326     //printf(" OrbitalInfo - ERROR: cannot open file for writing\n");
327     throw -901;
328     };
329     //
330     // Retrieve GL_RUN variables from the level2 file
331     //
332     OrbitalInfoversion = OrbitalInfoInfo(false); // we should decide how to handle versioning system
333     //
334     // create an interface to RunInfo called "runinfo"
335     //
336     runinfo = new ItoRunInfo(file);
337     //
338     // open "Run" tree in level2 file, if not existing return an error (sngl != 0)
339     //
340     sgnl = 0;
341     sgnl = runinfo->Update(run, "ORB", OrbitalInfoversion);
342     //sgnl = runinfo->Read(run);
343    
344     if ( sgnl ){
345     //printf("OrbitalInfo - ERROR: RunInfo exited with non-zero status\n");
346     code = sgnl;
347     goto closeandexit;
348     } else {
349     sgnl = 0;
350     };
351     //
352     // number of events in the file BEFORE the first event of our run
353     //
354     nobefrun = runinfo->GetFirstEntry();
355     //
356     // total number of events in the file
357     //
358     totfileentries = runinfo->GetFileEntries();
359     //
360     // first file entry AFTER the last event of our run
361     //
362     noaftrun = runinfo->GetLastEntry() + 1;
363     //
364     // number of run to be processed
365     //
366     numbofrun = runinfo->GetNoRun();
367 mocchiut 1.40 totnorun = runinfo->GetRunEntries();
368 mocchiut 1.1 //
369     // Try to access the OrbitalInfo tree in the file, if it exists we are reprocessing data if not we are processing a new run
370     //
371     OrbitalInfotrclone = (TTree*)file->Get("OrbitalInfo");
372     //
373     if ( !OrbitalInfotrclone ){
374     //
375     // tree does not exist, we are not reprocessing
376     //
377     reproc = false;
378 mocchiut 1.5 if ( run == 0 ){
379 mocchiut 1.1 if (verbose) printf(" OrbitalInfo - WARNING: you are reprocessing data but OrbitalInfo tree does not exist!\n");
380     }
381 mocchiut 1.5 if ( runinfo->IsReprocessing() && run != 0 ) {
382 mocchiut 1.1 if (verbose) printf(" OrbitalInfo - WARNING: it seems you are not reprocessing data but OrbitalInfo\n versioning information already exists in RunInfo.\n");
383     }
384     } else {
385     //
386     // tree exists, we are reprocessing data. Are we reprocessing a single run or all the file?
387     //
388 mocchiut 1.6 OrbitalInfotrclone->SetAutoSave(900000000000000LL);
389 mocchiut 1.1 reproc = true;
390     //
391     //
392     if (verbose) printf("\n Preparing the pre-processing...\n");
393     //
394 mocchiut 1.22 if ( run == 0 || totnorun == 1 ){
395 mocchiut 1.1 //
396     // we are reprocessing all the file
397     // if we are reprocessing everything we don't need to copy any old event and we can just work with the new tree and delete the old one immediately
398     //
399     reprocall = true;
400     //
401 mocchiut 1.57 if (verbose) printf("\n OrbitalInfo - WARNING: Reprocessing all runs\n Deleting old tree...\n");
402 mocchiut 1.1 //
403     } else {
404     //
405     // we are reprocessing a single run, we must copy to the new tree the events in the file which preceed the first event of the run
406     //
407     reprocall = false;
408     //
409 mocchiut 1.5 if (verbose) printf("\n OrbitalInfo - WARNING: Reprocessing run number %u \n",run);
410 mocchiut 1.1 //
411     // copying old tree to a new file
412     //
413 mocchiut 1.23 gSystem->MakeDirectory(OrbitalInfofolder.str().c_str());
414     myfold = true;
415 mocchiut 1.1 tempfile = new TFile(tempname.str().c_str(),"RECREATE");
416     tempOrbitalInfo = OrbitalInfotrclone->CloneTree(-1,"fast");
417     tempOrbitalInfo->SetName("OrbitalInfo-old");
418     tempfile->Write();
419 mocchiut 1.57 tempOrbitalInfo->Delete();
420 mocchiut 1.1 tempfile->Close();
421     }
422     //
423     // Delete the old tree from old file and memory
424     //
425 mocchiut 1.57 OrbitalInfotrclone->Clear();
426 mocchiut 1.1 OrbitalInfotrclone->Delete("all");
427     //
428     if (verbose) printf(" ...done!\n");
429     //
430     };
431     //
432     // create mydetector tree mydect
433     //
434     file->cd();
435     OrbitalInfotr = new TTree("OrbitalInfo-new","PAMELA OrbitalInfo data");
436 mocchiut 1.6 OrbitalInfotr->SetAutoSave(900000000000000LL);
437 mocchiut 1.30 orbitalinfo->Set();//ELENA **TEMPORANEO?**
438 mocchiut 1.1 OrbitalInfotr->Branch("OrbitalInfo","OrbitalInfo",&orbitalinfo);
439     //
440     if ( reproc && !reprocall ){
441     //
442     // open new file and retrieve also tree informations
443     //
444     tempfile = new TFile(tempname.str().c_str(),"READ");
445     OrbitalInfotrclone = (TTree*)tempfile->Get("OrbitalInfo-old");
446 mocchiut 1.6 OrbitalInfotrclone->SetAutoSave(900000000000000LL);
447 mocchiut 1.1 OrbitalInfotrclone->SetBranchAddress("OrbitalInfo",&orbitalinfoclone);
448     //
449     if ( nobefrun > 0 ){
450     if (verbose){
451 mocchiut 1.7 printf("\n Pre-processing: copying events from the old tree before the processed run\n");
452     printf(" Copying %u events in the file which are before the beginning of the run %u \n",nobefrun,run);
453     printf(" Start copying at event number 0, end copying at event number %u \n",nobefrun);
454 mocchiut 1.1 }
455     for (UInt_t j = 0; j < nobefrun; j++){
456     //
457 mocchiut 1.43 if ( OrbitalInfotrclone->GetEntry(j) <= 0 ) throw -36;
458 mocchiut 1.1 //
459     // copy orbitalinfoclone to mydec
460     //
461 mocchiut 1.3 orbitalinfo->Clear();
462 mocchiut 1.5 //
463 mocchiut 1.1 memcpy(&orbitalinfo,&orbitalinfoclone,sizeof(orbitalinfoclone));
464     //
465     // Fill entry in the new tree
466     //
467     OrbitalInfotr->Fill();
468     //
469     };
470     if (verbose) printf(" Finished successful copying!\n");
471 mocchiut 1.57 };
472 mocchiut 1.1 };
473     //
474 mocchiut 1.44 //
475 mocchiut 1.1 // Get the list of run to be processed, if only one run has to be processed the list will contain one entry only.
476     //
477     runlist = runinfo->GetRunList();
478     //
479     // Loop over the run to be processed
480     //
481     for (UInt_t irun=0; irun < numbofrun; irun++){
482 mocchiut 1.57
483     L_QQ_Q_l_lower = new Quaternions();
484     RYPang_lower = new InclinationInfo();
485     L_QQ_Q_l_upper = new Quaternions();
486     RYPang_upper = new InclinationInfo();
487    
488 mocchiut 1.1 //
489     // retrieve the first run ID to be processed using the RunInfo list
490     //
491 mocchiut 1.15
492 mocchiut 1.1 idRun = runlist->At(irun);
493     if (verbose){
494     printf("\n\n\n ####################################################################### \n");
495     printf(" PROCESSING RUN NUMBER %i \n",(int)idRun);
496     printf(" ####################################################################### \n\n\n");
497     }
498     //
499 mocchiut 1.5 runinfo->ID_ROOT_L0 = 0;
500 mocchiut 1.1 //
501     // store in the runinfo class the GL_RUN variables for our run
502     //
503     sgnl = 0;
504     sgnl = runinfo->GetRunInfo(idRun);
505     if ( sgnl ){
506 mocchiut 1.5 if ( debug ) printf("\n OrbitalInfo - ERROR: RunInfo exited with non-zero status\n");
507 mocchiut 1.1 code = sgnl;
508     goto closeandexit;
509     } else {
510     sgnl = 0;
511     };
512     //
513     // now you can access that variables using the RunInfo class this way runinfo->ID_REG_RUN
514     //
515 mocchiut 1.5 if ( runinfo->ID_ROOT_L0 == 0 ){
516     if ( debug ) printf("\n OrbitalInfo - ERROR: no run with ID_RUN = %u \n\n Exiting... \n\n",idRun);
517 mocchiut 1.1 code = -5;
518     goto closeandexit;
519     };
520     //
521 mocchiut 1.5 // prepare the timesync for the db
522     //
523     dbtime = new GL_TIMESYNC(runinfo->ID_ROOT_L0,"ID",dbc);
524 mocchiut 1.15
525 mocchiut 1.5 //
526 mocchiut 1.1 // Search in the DB the path and name of the LEVEL0 file to be processed.
527     //
528 mocchiut 1.5 glroot->Query_GL_ROOT(runinfo->ID_ROOT_L0,dbc);
529 mocchiut 1.1 //
530     ftmpname.str("");
531     ftmpname << glroot->PATH.Data() << "/";
532     ftmpname << glroot->NAME.Data();
533     fname = ftmpname.str().c_str();
534 mocchiut 1.15 ftmpname.str("");
535 mocchiut 1.1 //
536 mocchiut 1.40 // print nout informations
537 mocchiut 1.1 //
538 mocchiut 1.5 totevent = runinfo->NEVENTS;
539 mocchiut 1.30 evfrom = runinfo->EV_FROM;
540 mocchiut 1.15 //cout<<"totevents = "<<totevent<<"\n";
541 mocchiut 1.1 if (verbose){
542     printf("\n LEVEL0 data file: %s \n",fname.Data());
543 mocchiut 1.5 printf(" RUN HEADER absolute time is: %u \n",runinfo->RUNHEADER_TIME);
544     printf(" RUN TRAILER absolute time is: %u \n",runinfo->RUNTRAILER_TIME);
545 mocchiut 1.15 printf(" %i events to be processed for run %u: from %i to %i \n\n",totevent,idRun,runinfo->EV_FROM+1,runinfo->EV_FROM+totevent);
546 mocchiut 1.1 }//
547 mocchiut 1.27 //
548 mocchiut 1.28 // if ( !totevent ) goto closeandexit;
549 mocchiut 1.1 // Open Level0 file
550     l0File = new TFile(fname.Data());
551     if ( !l0File ) {
552 mocchiut 1.5 if ( debug ) printf(" OrbitalInfo - ERROR: problems opening Level0 file\n");
553 mocchiut 1.1 code = -6;
554     goto closeandexit;
555     };
556     l0tr = (TTree*)l0File->Get("Physics");
557     if ( !l0tr ) {
558 mocchiut 1.5 if ( debug ) printf(" OrbitalInfo - ERROR: no Physics tree in Level0 file\n");
559 mocchiut 1.1 l0File->Close();
560     code = -7;
561     goto closeandexit;
562     };
563 mocchiut 1.2 // EM: open header branch as well
564     l0head = l0tr->GetBranch("Header");
565     if ( !l0head ) {
566 mocchiut 1.5 if ( debug ) printf(" OrbitalInfo - ERROR: no Header branch in Level0 tree\n");
567 mocchiut 1.2 l0File->Close();
568     code = -8;
569     goto closeandexit;
570     };
571     l0tr->SetBranchAddress("Header", &eh);
572     // end EM
573 mocchiut 1.5 nevents = l0head->GetEntries();
574 mocchiut 1.1 //
575 mocchiut 1.28 if ( nevents < 1 && totevent ) {
576 mocchiut 1.5 if ( debug ) printf(" OrbitalInfo - ERROR: Level0 file is empty\n\n");
577 mocchiut 1.1 l0File->Close();
578     code = -11;
579     goto closeandexit;
580     };
581 mocchiut 1.20 //
582 mocchiut 1.28 if ( runinfo->EV_TO > nevents-1 && totevent ) {
583 mocchiut 1.5 if ( debug ) printf(" OrbitalInfo - ERROR: too few entries in the registry tree\n");
584 mocchiut 1.1 l0File->Close();
585     code = -12;
586     goto closeandexit;
587     };
588 mocchiut 1.55
589     //
590     // open IGRF files and do it only once if we are processing a full level2 file
591     //
592     if ( irun == 0 ){
593     if ( l0head->GetEntry(runinfo->EV_FROM) <= 0 ) throw -36;
594     //
595     // absolute time of first event of the run (it should not matter a lot)
596     //
597     ph = eh->GetPscuHeader();
598     atime = dbtime->DBabsTime(ph->GetOrbitalTime());
599    
600     parerror=glparam->Query_GL_PARAM(atime-anni5,301,dbc); // parameters stored in DB in GL_PRAM table
601     if ( parerror<0 ) {
602     code = parerror;
603     goto closeandexit;
604     };
605     ltp1 = (Int_t)(glparam->PATH+glparam->NAME).Length();
606     if ( verbose ) printf(" Reading Earth's Magnetic Field parameter file: %s \n",(glparam->PATH+glparam->NAME).Data());
607     //
608     parerror=glparam2->Query_GL_PARAM(atime,301,dbc); // parameters stored in DB in GL_PRAM table
609     if ( parerror<0 ) {
610     code = parerror;
611     goto closeandexit;
612     };
613     ltp2 = (Int_t)(glparam2->PATH+glparam->NAME).Length();
614     if ( verbose ) printf(" Reading Earth's Magnetic Field parameter file: %s \n",(glparam2->PATH+glparam2->NAME).Data());
615     //
616     parerror=glparam3->Query_GL_PARAM(atime,302,dbc); // parameters stored in DB in GL_PRAM table
617     if ( parerror<0 ) {
618     code = parerror;
619     goto closeandexit;
620     };
621     ltp3 = (Int_t)(glparam3->PATH+glparam2->NAME).Length();
622     if ( verbose ) printf(" Reading Earth's Magnetic Field parameter file: %s \n",(glparam3->PATH+glparam3->NAME).Data());
623     //
624     initize_((char *)(glparam->PATH+glparam->NAME).Data(),&ltp1,(char *)(glparam2->PATH+glparam2->NAME).Data(),&ltp2,(char *)(glparam3->PATH+glparam3->NAME).Data(),&ltp3);
625     //
626     }
627     //
628     // End IGRF stuff//
629     //
630    
631 mocchiut 1.1 //
632 mocchiut 1.44 // TTree *tp = (TTree*)l0File->Get("RunHeader");
633     // tp->SetBranchAddress("Header", &eH);
634     // tp->SetBranchAddress("RunHeader", &reh);
635     // tp->GetEntry(0);
636     // ph = eH->GetPscuHeader();
637     // ULong_t TimeSync = reh->LAST_TIME_SYNC_INFO;
638     // ULong_t ObtSync = reh->OBT_TIME_SYNC;
639     // if ( debug ) printf(" 1 TimeSync %lu ObtSync %lu DeltaOBT %lu\n",TimeSync,ObtSync,TimeSync-ObtSync);
640     //
641 mocchiut 1.15 ULong_t TimeSync = (ULong_t)dbtime->GetTimesync();
642     ULong_t ObtSync = (ULong_t)(dbtime->GetObt0()/1000);
643     ULong_t DeltaOBT = TimeSync - ObtSync;
644    
645     if ( debug ) printf(" 2 TimeSync %lu ObtSync %lu DeltaOBT %lu\n",(ULong_t)(dbtime->GetTimesync()/1000),(ULong_t)dbtime->GetObt0(),TimeSync-ObtSync);
646 mocchiut 1.37 //
647     // Read MCMDs from up to 11 files, 5 before and 5 after the present one in order to have some kind of inclination information
648     //
649     ch = new TChain("Mcmd","Mcmd");
650     //
651     // look in the DB to find the closest files to this run
652     //
653     TSQLResult *pResult = 0;
654     TSQLRow *Row = 0;
655     stringstream myquery;
656     UInt_t l0fid[10];
657     Int_t i = 0;
658     memset(l0fid,0,10*sizeof(Int_t));
659     //
660     myquery.str("");
661     myquery << "select ID_ROOT_L0 from GL_RUN where RUNHEADER_TIME<=" << runinfo->RUNHEADER_TIME << " group by ID_ROOT_L0 order by RUNHEADER_TIME desc limit 5;";
662     //
663     pResult = dbc->Query(myquery.str().c_str());
664     //
665     i = 9;
666     if( pResult ){
667     //
668     Row = pResult->Next();
669     //
670     while ( Row ){
671     //
672     // store infos and exit
673     //
674     l0fid[i] = (UInt_t)atoll(Row->GetField(0));
675     i--;
676     Row = pResult->Next();
677     //
678     };
679     pResult->Delete();
680     };
681     //
682     myquery.str("");
683     myquery << "select ID_ROOT_L0 from GL_RUN where RUNHEADER_TIME>" << runinfo->RUNHEADER_TIME << " group by ID_ROOT_L0 order by RUNHEADER_TIME asc limit 5;";
684     //
685     pResult = dbc->Query(myquery.str().c_str());
686     //
687     i = 0;
688     if( pResult ){
689     //
690     Row = pResult->Next();
691     //
692     while ( Row ){
693     //
694     // store infos and exit
695     //
696     l0fid[i] = (UInt_t)atoll(Row->GetField(0));
697     i++;
698     Row = pResult->Next();
699     //
700     };
701     pResult->Delete();
702     };
703     //
704     i = 0;
705     UInt_t previd = 0;
706     while ( i < 10 ){
707     if ( l0fid[i] && previd != l0fid[i] ){
708     previd = l0fid[i];
709     myquery.str("");
710     myquery << "select PATH,NAME from GL_ROOT where ID=" << l0fid[i] << " ;";
711     //
712     pResult = dbc->Query(myquery.str().c_str());
713     //
714     if( pResult ){
715     //
716     Row = pResult->Next();
717     //
718     if ( debug ) printf(" Using inclination informations from file: %s \n",(((TString)gSystem->ExpandPathName(Row->GetField(0)))+"/"+(TString)Row->GetField(1)).Data());
719     ch->Add(((TString)gSystem->ExpandPathName(Row->GetField(0)))+"/"+(TString)Row->GetField(1));
720     //
721     pResult->Delete();
722     };
723     };
724     i++;
725     };
726     //
727     // l0trm = (TTree*)l0File->Get("Mcmd");
728     // ch->ls();
729     ch->SetBranchAddress("Mcmd",&mcmdev);
730     // printf(" entries %llu \n", ch->GetEntries());
731     // l0trm = ch->GetTree();
732     // neventsm = l0trm->GetEntries();
733     neventsm = ch->GetEntries();
734     if ( debug ) printf(" entries %u \n", neventsm);
735 mocchiut 1.18 // neventsm = 0;
736 mocchiut 1.15 //
737     if (neventsm == 0){
738 mocchiut 1.18 if ( debug ) printf("InclinationInfo - WARNING: No quaternions in this File");
739     // l0File->Close();
740     code = 900;
741     // goto closeandexit;
742 mocchiut 1.15 }
743     //
744    
745 mocchiut 1.37 // l0trm->SetBranchAddress("Mcmd", &mcmdev);
746 mocchiut 1.18 // l0trm->SetBranchAddress("Header", &eh);
747 mocchiut 1.15 //
748     //
749     //
750 mocchiut 1.44
751     // UInt_t mctren = 0;
752     // UInt_t mcreen = 0;
753 mocchiut 1.15 UInt_t numrec = 0;
754     //
755     Double_t upperqtime = 0;
756     Double_t lowerqtime = 0;
757    
758 mocchiut 1.44 // Double_t incli = 0;
759     // oi = 0;
760     // UInt_t ooi = 0;
761 mocchiut 1.15 //
762 mocchiut 1.44 // init quaternions information from mcmd-packets
763 mocchiut 1.15 //
764     Bool_t isf = true;
765 mocchiut 1.44 // Int_t fgh = 0;
766    
767     vector<Float_t> q0;
768     vector<Float_t> q1;
769     vector<Float_t> q2;
770     vector<Float_t> q3;
771     vector<Double_t> qtime;
772     vector<Float_t> qPitch;
773     vector<Float_t> qRoll;
774     vector<Float_t> qYaw;
775     vector<Int_t> qmode;
776    
777     Int_t nt = 0;
778    
779     //init sine-function interpolation
780    
781     //cout<<"Sine coeficient initialisation..."<<endl;
782     vector<Sine> q0sine;
783     vector<Sine> q1sine;
784     vector<Sine> q2sine;
785     vector<Sine> q3sine;
786     vector<Sine> Yawsine;
787    
788     /*TH2F* q0testing = new TH2F();
789     TH2F* q1testing = new TH2F();
790     TH2F* q2testing = new TH2F();
791     TH2F* q3testing = new TH2F();
792     TH2F* Pitchtesting = new TH2F();
793     */
794     UInt_t must = 0;
795    
796 mocchiut 1.15 //
797 mocchiut 1.1 // run over all the events of the run
798     //
799     if (verbose) printf("\n Ready to start! \n\n Processed events: \n\n");
800     //
801 mocchiut 1.44 //
802 mocchiut 1.5 for ( re = runinfo->EV_FROM; re < (runinfo->EV_FROM+runinfo->NEVENTS); re++){
803 mocchiut 1.1 //
804 mocchiut 1.5 if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000);
805 mocchiut 1.15 if ( debug ) printf(" %i \n",procev);
806 mocchiut 1.1 //
807 mocchiut 1.43 if ( l0head->GetEntry(re) <= 0 ) throw -36;
808 mocchiut 1.1 //
809     // absolute time of this event
810     //
811 mocchiut 1.5 ph = eh->GetPscuHeader();
812 mocchiut 1.15 atime = dbtime->DBabsTime(ph->GetOrbitalTime());
813 mocchiut 1.40 if ( debug ) printf(" %i absolute time \n",procev);
814 mocchiut 1.1 //
815     // paranoid check
816     //
817 mocchiut 1.34 if ( (atime > (runinfo->RUNTRAILER_TIME+1)) || (atime < (runinfo->RUNHEADER_TIME-1)) ) {
818 mocchiut 1.1 if (verbose) printf(" OrbitalInfo - WARNING: event at time outside the run time window, skipping it\n");
819 mocchiut 1.30 jumped++;
820 mocchiut 1.44 // debug = true;
821 mocchiut 1.7 continue;
822     }
823 mocchiut 1.30
824     //
825     // retrieve tof informations
826     //
827     if ( !reprocall ){
828     itr = nobefrun + (re - evfrom - jumped);
829     //itr = re-(46438+200241);
830     } else {
831     itr = runinfo->GetFirstEntry() + (re - evfrom - jumped);
832     };
833     //
834     if ( !standalone ){
835     if ( itr > nevtofl2 ){
836     if ( verbose ) printf(" OrbitalInfo - ERROR: no tof events with entry = %i in Level2 file\n",itr);
837     if ( debug ) printf(" nobefrun %u re %u evfrom %u jumped %u reprocall %i \n",nobefrun,re,evfrom,jumped,reprocall);
838     l0File->Close();
839     code = -901;
840     goto closeandexit;
841     };
842     //
843     tof->Clear();
844     //
845 mocchiut 1.43 if ( ttof->GetEntry(itr) <= 0 ) throw -36;
846 mocchiut 1.30 //
847     };
848 mocchiut 1.1 //
849     procev++;
850     //
851     // start processing
852     //
853 mocchiut 1.40 if ( debug ) printf(" %i start processing \n",procev);
854 mocchiut 1.3 orbitalinfo->Clear();
855 mocchiut 1.5 //
856 mocchiut 1.30 OrbitalInfoTrkVar *t_orb = new OrbitalInfoTrkVar();
857     if( !(orbitalinfo->OrbitalInfoTrk) ) orbitalinfo->OrbitalInfoTrk = new TClonesArray("OrbitalInfoTrkVar",2);
858     TClonesArray &tor = *orbitalinfo->OrbitalInfoTrk;
859     //
860 mocchiut 1.15 // Fill OBT, pkt_num and absTime
861     //
862 mocchiut 1.2 orbitalinfo->pkt_num = ph->GetCounter();
863     orbitalinfo->OBT = ph->GetOrbitalTime();
864 mocchiut 1.15 orbitalinfo->absTime = atime;
865 mocchiut 1.40 if ( debug ) printf(" %i pktnum obt abstime \n",procev);
866 mocchiut 1.15 //
867     // Propagate the orbit from the tle time to atime, using SGP(D)4.
868     //
869 mocchiut 1.40 if ( debug ) printf(" %i sgp4 \n",procev);
870 mocchiut 1.15 cCoordGeo coo;
871 mocchiut 1.40 Float_t jyear=0.;
872 mocchiut 1.7 //
873     if(atime >= gltle->GetToTime()) {
874 mocchiut 1.9 if ( !gltle->Query(atime, dbc) ){
875 mocchiut 1.15 //
876 mocchiut 1.9 // Compute the magnetic dipole moment.
877 mocchiut 1.15 //
878 mocchiut 1.40 if ( debug ) printf(" %i compute magnetic dipole moment \n",procev);
879 mocchiut 1.9 UInt_t year, month, day, hour, min, sec;
880 mocchiut 1.15 //
881 mocchiut 1.9 TTimeStamp t = TTimeStamp(atime, kTRUE);
882     t.GetDate(kTRUE, 0, &year, &month, &day);
883     t.GetTime(kTRUE, 0, &hour, &min, &sec);
884     jyear = (float) year
885     + (month*31.+ (float) day)/365.
886 mocchiut 1.44 + (hour*3600.+min*60.+(float)sec)/(24.*3600.*365.);
887 mocchiut 1.15 //
888 mocchiut 1.40 if ( debug ) printf(" %i compute magnetic dipole moment get dipole moment for year\n",procev);
889 mocchiut 1.57 if ( debug ) printf(" %i jyear %f dimo %f \n",procev,jyear,dimo);
890 mocchiut 1.9 feldcof_(&jyear, &dimo); // get dipole moment for year
891 mocchiut 1.40 if ( debug ) printf(" %i compute magnetic dipole moment end\n",procev);
892 mocchiut 1.9 } else {
893     code = -56;
894     goto closeandexit;
895     };
896 mocchiut 1.7 }
897 mocchiut 1.15 coo = getCoo(atime, gltle->GetFromTime(), gltle->GetTle());
898     //
899     cOrbit orbits(*gltle->GetTle());
900     //
901 mocchiut 1.18 if ( debug ) printf(" I am Here \n");
902     //
903 mocchiut 1.15 // synchronize with quaternions data
904     //
905 mocchiut 1.18 if ( isf && neventsm>0 ){
906 mocchiut 1.15 //
907     // First event
908     //
909     isf = false;
910     upperqtime = atime;
911     lowerqtime = runinfo->RUNHEADER_TIME;
912 mocchiut 1.44 for ( ik = 0; ik < neventsm; ik++){ //number of macrocommad packets
913 mocchiut 1.43 if ( ch->GetEntry(ik) <= 0 ) throw -36;
914 mocchiut 1.15 tmpSize = mcmdev->Records->GetEntries();
915     numrec = tmpSize;
916 mocchiut 1.44 for (Int_t j3 = 0;j3<tmpSize;j3++){ //number of subpackets
917 mocchiut 1.37 if ( debug ) printf(" ik %i j3 %i eh eh eh \n",ik,j3);
918 mocchiut 1.15 mcmdrc = (pamela::McmdRecord*)mcmdev->Records->At(j3);
919 mocchiut 1.38 if ( mcmdrc ){ // missing inclination bug [8RED 090116]
920 mocchiut 1.46 if ( debug ) printf(" pluto \n");
921 mocchiut 1.44 if ((int)mcmdrc->ID1 == 226 && mcmdrc->Mcmd_Block_crc_ok == 1){ //Check that it is Inclination Packet
922 mocchiut 1.47 L_QQ_Q_l_upper->fill(mcmdrc->McmdData);
923 mocchiut 1.38 for (UInt_t ui = 0; ui < 6; ui++){
924     if (ui>0){
925     if (L_QQ_Q_l_upper->time[ui]>L_QQ_Q_l_upper->time[0]){
926 mocchiut 1.46 if ( debug ) printf(" here1 %i \n",ui);
927 mocchiut 1.44 Double_t u_time = dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[ui]*1000-DeltaOBT*1000));
928     Int_t recSize = recqtime.size();
929 pam-mep 1.54 if(lowerqtime > recqtime[recSize-1]){
930     Int_t sizeqmcmd = qtime.size();
931     inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
932     qtime[sizeqmcmd]=u_time;
933     q0[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][0];
934     q1[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][1];
935     q2[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][2];
936     q3[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][3];
937     qmode[sizeqmcmd]=holeq(lowerqtime,qtime[sizeqmcmd],L_QQ_Q_l_lower,L_QQ_Q_l_upper,ui);
938     lowerqtime = u_time;
939     orbits.getPosition((double) (u_time - gltle->GetFromTime())/60., &eCi);
940     RYPang_upper->TransAngle(eCi.getPos().m_x,eCi.getPos().m_y,eCi.getPos().m_z,eCi.getVel().m_x,eCi.getVel().m_y,eCi.getVel().m_z,L_QQ_Q_l_upper->quat[ui][0],L_QQ_Q_l_upper->quat[ui][1],L_QQ_Q_l_upper->quat[ui][2],L_QQ_Q_l_upper->quat[ui][3]);
941     qRoll[sizeqmcmd]=RYPang_upper->Kren;
942     qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
943     qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
944     }
945 mocchiut 1.44 for(Int_t mu = nt;mu<recSize;mu++){
946     if(recqtime[mu]>lowerqtime && recqtime[mu]<u_time){
947     nt=mu;
948     Int_t sizeqmcmd = qtime.size();
949     inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
950     qtime[sizeqmcmd]=recqtime[mu];
951     q0[sizeqmcmd]=recq0[mu];
952     q1[sizeqmcmd]=recq1[mu];
953     q2[sizeqmcmd]=recq2[mu];
954     q3[sizeqmcmd]=recq3[mu];
955     qmode[sizeqmcmd]=-10;
956     orbits.getPosition((double) (qtime[sizeqmcmd] - gltle->GetFromTime())/60., &eCi);
957     RYPang_upper->TransAngle(eCi.getPos().m_x,eCi.getPos().m_y,eCi.getPos().m_z,eCi.getVel().m_x,eCi.getVel().m_y,eCi.getVel().m_z,recq0[mu],recq1[mu],recq2[mu],recq3[mu]);
958     qRoll[sizeqmcmd]=RYPang_upper->Kren;
959     qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
960     qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
961     }
962     if(recqtime[mu]>=u_time){
963     Int_t sizeqmcmd = qtime.size();
964     inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
965     qtime[sizeqmcmd]=u_time;
966     q0[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][0];
967     q1[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][1];
968     q2[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][2];
969     q3[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][3];
970     qmode[sizeqmcmd]=holeq(lowerqtime,qtime[sizeqmcmd],L_QQ_Q_l_lower,L_QQ_Q_l_upper,ui);
971     lowerqtime = u_time;
972     orbits.getPosition((double) (u_time - gltle->GetFromTime())/60., &eCi);
973     RYPang_upper->TransAngle(eCi.getPos().m_x,eCi.getPos().m_y,eCi.getPos().m_z,eCi.getVel().m_x,eCi.getVel().m_y,eCi.getVel().m_z,L_QQ_Q_l_upper->quat[ui][0],L_QQ_Q_l_upper->quat[ui][1],L_QQ_Q_l_upper->quat[ui][2],L_QQ_Q_l_upper->quat[ui][3]);
974     qRoll[sizeqmcmd]=RYPang_upper->Kren;
975     qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
976     qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
977     break;
978 mocchiut 1.38 }
979     }
980     }
981     }else{
982 mocchiut 1.46 if ( debug ) printf(" here2 %i \n",ui);
983 mocchiut 1.44 Double_t u_time = dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000));
984     if(lowerqtime>u_time)nt=0;
985     Int_t recSize = recqtime.size();
986 pam-mep 1.54 if(lowerqtime > recqtime[recSize-1]){
987     Int_t sizeqmcmd = qtime.size();
988     inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
989     qtime[sizeqmcmd]=u_time;
990     q0[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][0];
991     q1[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][1];
992     q2[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][2];
993     q3[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][3];
994     qmode[sizeqmcmd]=holeq(lowerqtime,qtime[sizeqmcmd],L_QQ_Q_l_lower,L_QQ_Q_l_upper,ui);
995     lowerqtime = u_time;
996     orbits.getPosition((double) (u_time - gltle->GetFromTime())/60., &eCi);
997     RYPang_upper->TransAngle(eCi.getPos().m_x,eCi.getPos().m_y,eCi.getPos().m_z,eCi.getVel().m_x,eCi.getVel().m_y,eCi.getVel().m_z,L_QQ_Q_l_upper->quat[0][0],L_QQ_Q_l_upper->quat[0][1],L_QQ_Q_l_upper->quat[0][2],L_QQ_Q_l_upper->quat[0][3]);
998     qRoll[sizeqmcmd]=RYPang_upper->Kren;
999     qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
1000     qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
1001     }
1002 mocchiut 1.44 for(Int_t mu = nt;mu<recSize;mu++){
1003     if(recqtime[mu]>lowerqtime && recqtime[mu]<u_time){
1004     nt=mu;
1005     Int_t sizeqmcmd = qtime.size();
1006     inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
1007     qtime[sizeqmcmd]=recqtime[mu];
1008     q0[sizeqmcmd]=recq0[mu];
1009     q1[sizeqmcmd]=recq1[mu];
1010     q2[sizeqmcmd]=recq2[mu];
1011     q3[sizeqmcmd]=recq3[mu];
1012     qmode[sizeqmcmd]=-10;
1013     orbits.getPosition((double) (qtime[sizeqmcmd] - gltle->GetFromTime())/60., &eCi);
1014     RYPang_upper->TransAngle(eCi.getPos().m_x,eCi.getPos().m_y,eCi.getPos().m_z,eCi.getVel().m_x,eCi.getVel().m_y,eCi.getVel().m_z,recq0[mu],recq1[mu],recq2[mu],recq3[mu]);
1015     qRoll[sizeqmcmd]=RYPang_upper->Kren;
1016     qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
1017     qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
1018     }
1019     if(recqtime[mu]>=u_time){
1020     Int_t sizeqmcmd = qtime.size();
1021     inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
1022     qtime[sizeqmcmd]=u_time;
1023     q0[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][0];
1024     q1[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][1];
1025     q2[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][2];
1026     q3[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][3];
1027     qmode[sizeqmcmd]=holeq(lowerqtime,qtime[sizeqmcmd],L_QQ_Q_l_lower,L_QQ_Q_l_upper,ui);
1028     lowerqtime = u_time;
1029     orbits.getPosition((double) (u_time - gltle->GetFromTime())/60., &eCi);
1030     RYPang_upper->TransAngle(eCi.getPos().m_x,eCi.getPos().m_y,eCi.getPos().m_z,eCi.getVel().m_x,eCi.getVel().m_y,eCi.getVel().m_z,L_QQ_Q_l_upper->quat[0][0],L_QQ_Q_l_upper->quat[0][1],L_QQ_Q_l_upper->quat[0][2],L_QQ_Q_l_upper->quat[0][3]);
1031     qRoll[sizeqmcmd]=RYPang_upper->Kren;
1032     qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
1033     qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
1034 mocchiut 1.15 CopyQ(L_QQ_Q_l_lower,L_QQ_Q_l_upper);
1035 mocchiut 1.44 break;
1036 mocchiut 1.15 }
1037     }
1038 mocchiut 1.44 }
1039 mocchiut 1.38 }
1040 mocchiut 1.15 }
1041 mocchiut 1.44 }
1042 mocchiut 1.46 if ( debug ) printf(" ciccio \n");
1043 mocchiut 1.15 }
1044 mocchiut 1.44 }
1045 mocchiut 1.47
1046     if(qtime.size()==0){
1047 mocchiut 1.50 for(UInt_t my=0;my<recqtime.size();my++){
1048 mocchiut 1.47 Int_t sizeqmcmd = qtime.size();
1049     inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
1050     qtime[sizeqmcmd]=recqtime[my];
1051     q0[sizeqmcmd]=recq0[my];
1052     q1[sizeqmcmd]=recq1[my];
1053     q2[sizeqmcmd]=recq2[my];
1054     q3[sizeqmcmd]=recq3[my];
1055     qmode[sizeqmcmd]=-10;
1056     orbits.getPosition((double) (qtime[sizeqmcmd] - gltle->GetFromTime())/60., &eCi);
1057     RYPang_upper->TransAngle(eCi.getPos().m_x,eCi.getPos().m_y,eCi.getPos().m_z,eCi.getVel().m_x,eCi.getVel().m_y,eCi.getVel().m_z,recq0[my],recq1[my],recq2[my],recq3[my]);
1058     qRoll[sizeqmcmd]=RYPang_upper->Kren;
1059     qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
1060     qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
1061     }
1062     }
1063    
1064 mocchiut 1.46 if ( debug ) printf(" fuffi \n");
1065 mocchiut 1.49
1066     //sineparam(q0sine,qtime,q0,qRoll,qPitch,0.60);
1067     //sineparam(q1sine,qtime,q1,qRoll,qPitch,0.82);
1068     //sineparam(q2sine,qtime,q2,qRoll,qPitch,0.82);
1069     //sineparam(q3sine,qtime,q3,qRoll,qPitch,0.60);
1070     //sineparam(Yawsine,qtime,qYaw,qRoll,qPitch,4);
1071    
1072 mocchiut 1.46 if ( debug ) printf(" puffi \n");
1073 mocchiut 1.44 Double_t tmin = 9999999999.;
1074     Double_t tmax = 0.;
1075     for(UInt_t tre = 0;tre<qtime.size();tre++){
1076     if(qtime[tre]>tmax)tmax = qtime[tre];
1077     if(qtime[tre]<tmin)tmin = qtime[tre];
1078     }
1079 mocchiut 1.46 if ( debug ) printf(" gnfuffi \n");
1080    
1081 mocchiut 1.44 //q0testing->SetName("q0testing");
1082     //q1testing->SetName("q1testing");
1083     //q2testing->SetName("q2testing");
1084     //q3testing->SetName("q3testing");
1085 mocchiut 1.15
1086 mocchiut 1.44 // Int_t ss=10.*(tmax-tmin);
1087     //q0testing->SetBins(ss,tmin,tmax,1000,-1.,1.);
1088     //Pitchtesting->SetBins(ss,tmin,tmax,1000,-40.,40.);
1089    
1090     // for(Int_t tre = 0;tre<qtime.size();tre++){
1091     //cout<<"q0["<<tre<<" = "<<q0[tre]<<endl;
1092     //q0testing->Fill(qtime[tre],q0[tre]);
1093     //q1testing->Fill(qtime[tre],q1[tre]);
1094     //Pitchtesting->Fill(qtime[tre],qPitch[tre],100);
1095     //if(qmode[tre] == -10)Pitchtesting->Fill(qtime[tre],10,100);
1096     //q2testing->Fill(qtime[tre],q2[tre],100);
1097     //q3testing->Fill(qtime[tre],q3[tre],100);
1098     // }
1099 mocchiut 1.15
1100 mocchiut 1.44 //for(Int_t tre=0;tre<q0sine.size();tre++)cout<<q1sine[tre].A<<"*sin("<<q1sine[tre].b<<"x+"<<q1sine[tre].c<<")\t time start: "<<q1sine[tre].startPoint<<"\ttime end: "<<q1sine[tre].finishPoint<<endl;
1101     //for(Int_t tre=0;tre<q0sine.size();tre++)cout<<q1sine[tre].A<<"*sin("<<q1sine[tre].b<<"x+"<<q1sine[tre].c<<")\t time start: "<<q0sine[tre].startPoint<<"\ttime end: "<<q0sine[tre].finishPoint<<endl;
1102     } // if we processed first event
1103    
1104     //Filling Inclination information
1105     Double_t incli = 0;
1106 mocchiut 1.46 if ( qtime.size() > 1 ){
1107 mocchiut 1.44 for(UInt_t mu = must;mu<qtime.size()-1;mu++){
1108 mocchiut 1.46 if ( debug ) printf(" ??grfuffi %i sixe %i must %i \n",mu,qtime.size()-1,must);
1109 mocchiut 1.44 if(qtime[mu+1]>qtime[mu]){
1110 mocchiut 1.46 if ( debug ) printf(" grfuffi2 %i \n",mu);
1111 mocchiut 1.44 if(atime<=qtime[mu+1] && atime>=qtime[mu]){
1112     must = mu;
1113     incli = (qPitch[mu+1]-qPitch[mu])/(qtime[mu+1]-qtime[mu]);
1114     orbitalinfo->theta = incli*atime+qPitch[mu+1]-incli*qtime[mu+1];
1115     incli = (qRoll[mu+1]-qRoll[mu])/(qtime[mu+1]-qtime[mu]);
1116     orbitalinfo->etha = incli*atime+qRoll[mu+1]-incli*qtime[mu+1];
1117     incli = (qYaw[mu+1]-qYaw[mu])/(qtime[mu+1]-qtime[mu]);
1118     orbitalinfo->phi = incli*atime+qYaw[mu+1]-incli*qtime[mu+1];
1119    
1120     incli = (q0[mu+1]-q0[mu])/(qtime[mu+1]-qtime[mu]);
1121 mocchiut 1.49 orbitalinfo->q0 = incli*atime+q0[mu+1]-incli*qtime[mu+1];
1122 mocchiut 1.44 incli = (q1[mu+1]-q1[mu])/(qtime[mu+1]-qtime[mu]);
1123 mocchiut 1.49 orbitalinfo->q1 = incli*atime+q1[mu+1]-incli*qtime[mu+1];
1124 mocchiut 1.44 incli = (q2[mu+1]-q2[mu])/(qtime[mu+1]-qtime[mu]);
1125 mocchiut 1.49 orbitalinfo->q2 = incli*atime+q2[mu+1]-incli*qtime[mu+1];
1126 mocchiut 1.44 incli = (q3[mu+1]-q3[mu])/(qtime[mu+1]-qtime[mu]);
1127 mocchiut 1.49 orbitalinfo->q3 = incli*atime+q3[mu+1]-incli*qtime[mu+1];
1128 mocchiut 1.44
1129     orbitalinfo->TimeGap = qtime[mu+1]-qtime[mu];
1130     orbitalinfo->mode = qmode[mu+1];
1131 mocchiut 1.49 //if(qmode[mu+1]==-10) orbitalinfo->R10r = true;else orbitalinfo->R10r = false;
1132     //reserved for next versions Vitaly.
1133     /*if(qmode[mu+1]==-10 || qmode[mu+1]==0 || qmode[mu+1]==1 || qmode[mu+1]==3 || qmode[mu+1]==4 || qmode[mu+1]==6){
1134 mocchiut 1.44 //linear interpolation
1135     incli = (q0[mu+1]-q0[mu])/(qtime[mu+1]-qtime[mu]);
1136     orbitalinfo->q0 = incli*atime+q0[mu+1]-incli*qtime[mu+1];
1137     incli = (q1[mu+1]-q1[mu])/(qtime[mu+1]-qtime[mu]);
1138     orbitalinfo->q1 = incli*atime+q1[mu+1]-incli*qtime[mu+1];
1139     incli = (q2[mu+1]-q2[mu])/(qtime[mu+1]-qtime[mu]);
1140     orbitalinfo->q2 = incli*atime+q2[mu+1]-incli*qtime[mu+1];
1141     incli = (q3[mu+1]-q3[mu])/(qtime[mu+1]-qtime[mu]);
1142     orbitalinfo->q3 = incli*atime+q3[mu+1]-incli*qtime[mu+1];
1143     }else{
1144     //sine interpolation
1145     for(UInt_t mt=0;mt<q0sine.size();mt++){
1146     if(atime<=q0sine[mt].finishPoint && atime>=q0sine[mt].startPoint){
1147     if(!q0sine[mt].NeedFit)orbitalinfo->q0=q0sine[mt].A*sin(q0sine[mt].b*atime+q0sine[mt].c);else{
1148     incli = (q0[mu+1]-q0[mu])/(qtime[mu+1]-qtime[mu]);
1149     orbitalinfo->q0 = incli*atime+q0[mu+1]-incli*qtime[mu+1];
1150     }
1151     }
1152     if(atime<=q1sine[mt].finishPoint && atime>=q1sine[mt].startPoint){
1153     if(!q1sine[mt].NeedFit)orbitalinfo->q1=q1sine[mt].A*sin(q1sine[mt].b*atime+q1sine[mt].c);else{
1154     incli = (q1[mu+1]-q1[mu])/(qtime[mu+1]-qtime[mu]);
1155     orbitalinfo->q1 = incli*atime+q1[mu+1]-incli*qtime[mu+1];
1156     }
1157     }
1158     if(atime<=q2sine[mt].finishPoint && atime>=q2sine[mt].startPoint){
1159     if(!q2sine[mt].NeedFit)orbitalinfo->q2=q0sine[mt].A*sin(q2sine[mt].b*atime+q2sine[mt].c);else{
1160     incli = (q2[mu+1]-q2[mu])/(qtime[mu+1]-qtime[mu]);
1161     orbitalinfo->q2 = incli*atime+q2[mu+1]-incli*qtime[mu+1];
1162     }
1163     }
1164     if(atime<=q3sine[mt].finishPoint && atime>=q3sine[mt].startPoint){
1165     if(!q3sine[mt].NeedFit)orbitalinfo->q3=q0sine[mt].A*sin(q3sine[mt].b*atime+q3sine[mt].c);else{
1166     incli = (q3[mu+1]-q3[mu])/(qtime[mu+1]-qtime[mu]);
1167     orbitalinfo->q3 = incli*atime+q3[mu+1]-incli*qtime[mu+1];
1168     }
1169     }
1170     if(atime<=Yawsine[mt].finishPoint && atime>=Yawsine[mt].startPoint){
1171     if(!Yawsine[mt].NeedFit)orbitalinfo->phi=Yawsine[mt].A*sin(Yawsine[mt].b*atime+Yawsine[mt].c);else{
1172     incli = (qYaw[mu+1]-qYaw[mu])/(qtime[mu+1]-qtime[mu]);
1173     orbitalinfo->phi = incli*atime+qYaw[mu+1]-incli*qtime[mu+1];
1174     }
1175     }
1176     }
1177 mocchiut 1.49 }*/
1178 mocchiut 1.44 //q0testing->Fill(atime,orbitalinfo->q0,100);
1179     //q1testing->Fill(atime,orbitalinfo->q1,100);
1180     //Pitchtesting->Fill(atime,orbitalinfo->etha);
1181     //q2testing->Fill(atime,orbitalinfo->q2);
1182     //q3testing->Fill(atime,orbitalinfo->q3);
1183     break;
1184     }
1185 mocchiut 1.15 }
1186 mocchiut 1.44 }
1187 mocchiut 1.46 }
1188 mocchiut 1.30 //
1189     // ops no inclination information
1190     //
1191 mocchiut 1.44
1192 mocchiut 1.30 if ( orbitalinfo->q0< -999 || orbitalinfo->q1 < -999 || orbitalinfo->q2 < -999 || orbitalinfo->q3 < -999 || orbitalinfo->q0 != orbitalinfo->q0 || orbitalinfo->q1 != orbitalinfo->q1 || orbitalinfo->q2 != orbitalinfo->q2 || orbitalinfo->q3 != orbitalinfo->q3 ){
1193     orbitalinfo->mode = 10;
1194     orbitalinfo->q0 = -1000.;
1195     orbitalinfo->q1 = -1000.;
1196     orbitalinfo->q2 = -1000.;
1197     orbitalinfo->q3 = -1000.;
1198     orbitalinfo->etha = -1000.;
1199     orbitalinfo->phi = -1000.;
1200     orbitalinfo->theta = -1000.;
1201 mocchiut 1.15 };
1202 mocchiut 1.30 //
1203     // #########################################################################################################################
1204 mocchiut 1.15 //
1205     // fill orbital positions
1206     //
1207 mocchiut 1.7 // Build coordinates in the right range. We want to convert,
1208     // longitude from (0, 2*pi) to (-180deg, 180deg). Altitude is
1209     // in meters.
1210     lon = (coo.m_Lon > M_PI) ? rad2deg(coo.m_Lon - 2*M_PI) : rad2deg(coo.m_Lon);
1211     lat = rad2deg(coo.m_Lat);
1212     alt = coo.m_Alt;
1213 mocchiut 1.15 //
1214 mocchiut 1.7 if( lon<180 && lon>-180 && lat<90 && lat>-90 && alt>0 ){
1215 mocchiut 1.15 //
1216 mocchiut 1.7 orbitalinfo->lon = lon;
1217     orbitalinfo->lat = lat;
1218     orbitalinfo->alt = alt ;
1219 mocchiut 1.15 //
1220 mocchiut 1.7 // compute mag field components and L shell.
1221 mocchiut 1.15 //
1222 mocchiut 1.7 feldg_(&lat, &lon, &alt, &bnorth, &beast, &bdown, &babs);
1223     shellg_(&lat, &lon, &alt, &dimo, &xl, &icode, &bab1);
1224     findb0_(&stps, &bdel, &value, &bequ, &rr0);
1225 mocchiut 1.15 //
1226 mocchiut 1.7 orbitalinfo->Bnorth = bnorth;
1227     orbitalinfo->Beast = beast;
1228     orbitalinfo->Bdown = bdown;
1229     orbitalinfo->Babs = babs;
1230     orbitalinfo->BB0 = babs/bequ;
1231 mocchiut 1.15 orbitalinfo->L = xl;
1232 mocchiut 1.7 // Set Stormer vertical cutoff using L shell.
1233 mocchiut 1.57 orbitalinfo->cutoffsvl = 14.295 / (xl*xl); //
1234     /*
1235     ---------- Forwarded message ----------
1236     Date: Wed, 09 May 2012 12:16:47 +0200
1237     From: Alessandro Bruno <alessandro.bruno@ba.infn.it>
1238     To: Mirko Boezio <mirko.boezio@ts.infn.it>
1239     Cc: Francesco S. Cafagna <Francesco.Cafagna@ba.infn.it>
1240     Subject: Störmer vertical cutoff
1241    
1242     Ciao Mirko,
1243     volevo segnalarti che il valore dello Störmer vertical cutoff nel Level2 è
1244     sovrastimato di circa il 4%.
1245     Dopo un'approfondita analisi con l'IGRF-05 abbiamo ricavano un valore pari
1246     a: 14.295 / L^2 anzichè 14.9 / L^2, valore obsoleto in quanto riferito agli
1247     anni '50.
1248     */
1249     //14.9/(xl*xl);
1250 mocchiut 1.56 orbitalinfo->igrf_icode = icode;
1251 mocchiut 1.15 //
1252     };
1253 pamelaprod 1.11 //
1254 mocchiut 1.40 if ( debug ) printf(" pitch angle \n");
1255     //
1256 mocchiut 1.30 // pitch angles
1257     //
1258 mocchiut 1.49 //if ( orbitalinfo->mode != 10 && orbitalinfo->mode != 5 && orbitalinfo->mode !=7 && orbitalinfo->mode != 9 ){
1259     if( orbitalinfo->TimeGap>0 && orbitalinfo->TimeGap<2000000){
1260 mocchiut 1.30 //
1261 mocchiut 1.49 Float_t Bx = -orbitalinfo->Bdown;
1262     Float_t By = orbitalinfo->Beast;
1263     Float_t Bz = orbitalinfo->Bnorth;
1264 mocchiut 1.30 //
1265     TMatrixD Fij = PO->ECItoGreenwich(PO->QuatoECI(orbitalinfo->q0,orbitalinfo->q1,orbitalinfo->q2,orbitalinfo->q3),orbitalinfo->absTime);
1266 mocchiut 1.49 TMatrixD Gij = PO->ColPermutation(Fij);
1267 mocchiut 1.30 TMatrixD Dij = PO->GreenwichtoGEO(orbitalinfo->lat,orbitalinfo->lon,Fij);
1268     TMatrixD Iij = PO->ColPermutation(Dij);
1269     //
1270 mocchiut 1.33 orbitalinfo->Iij.ResizeTo(Iij);
1271     orbitalinfo->Iij = Iij;
1272     //
1273 mocchiut 1.30 A1 = Iij(0,2);
1274     A2 = Iij(1,2);
1275     A3 = Iij(2,2);
1276 mocchiut 1.49 //
1277 mocchiut 1.33 // orbitalinfo->pamzenitangle = (Float_t)PO->GetPitchAngle(1,0,0,A1,A2,A3); // Angle between zenit and Pamela's main axiz
1278     // orbitalinfo->pamBangle = (Float_t)PO->GetPitchAngle(A1,A2,A3,Bx,By,Bz); // Angle between Pamela's main axiz and B
1279 mocchiut 1.30 //
1280     if ( !standalone && tof->ntrk() > 0 ){
1281     //
1282     Int_t nn = 0;
1283     for(Int_t nt=0; nt < tof->ntrk(); nt++){
1284     //
1285     ToFTrkVar *ptt = tof->GetToFTrkVar(nt);
1286     Double_t E11x = ptt->xtr_tof[0]; // tr->x[0];
1287     Double_t E11y = ptt->ytr_tof[0]; //tr->y[0];
1288     Double_t E11z = zin[0];
1289     Double_t E22x = ptt->xtr_tof[3];//tr->x[3];
1290     Double_t E22y = ptt->ytr_tof[3];//tr->y[3];
1291     Double_t E22z = zin[3];
1292 mocchiut 1.32 if ( (E11x < 100. && E11y < 100. && E22x < 100. && E22y < 100.) || ptt->trkseqno != -1 ){
1293 mocchiut 1.30 Double_t norm = sqrt(pow(E22x-E11x,2)+pow(E22y-E11y,2)+pow(E22z-E11z,2));
1294 mocchiut 1.44 // Double_t MyAzim = TMath::RadToDeg()*atan(TMath::Abs(E22y-E11y)/TMath::Abs(E22x-E11x));
1295     // if(E22x-E11x>=0 && E22y-E11y <0) MyAzim = 360. - MyAzim;
1296     // if(E22x-E11x>=0 && E22y-E11y >=0) MyAzim = MyAzim;
1297     // if(E22x-E11x<0 && E22y-E11y >0) MyAzim = 180. - MyAzim;
1298     // if(E22x-E11x<0 && E22y-E11y <0) MyAzim = 180. + MyAzim;
1299 mocchiut 1.30 Px = (E22x-E11x)/norm;
1300     Py = (E22y-E11y)/norm;
1301     Pz = (E22z-E11z)/norm;
1302     //
1303 mocchiut 1.33 t_orb->trkseqno = ptt->trkseqno;
1304     //
1305     TMatrixD Eij = PO->PamelatoGEO(Iij,Px,Py,Pz);
1306     t_orb->Eij.ResizeTo(Eij);
1307     t_orb->Eij = Eij;
1308     //
1309 pam-mep 1.51 TMatrixD Sij = PO->PamelatoGEO(Gij,Px,Py,Pz);
1310 mocchiut 1.33 t_orb->Sij.ResizeTo(Sij);
1311     t_orb->Sij = Sij;
1312 mocchiut 1.30 //
1313     t_orb->pitch = (Float_t)PO->GetPitchAngle(Eij(0,0),Eij(1,0),Eij(2,0),Bx,By,Bz);
1314 mocchiut 1.33 //
1315     //
1316     Double_t omega = PO->GetPitchAngle(Eij(0,0),Eij(1,0),Eij(2,0),cos(orbitalinfo->lon+TMath::Pi()/2)-sin(orbitalinfo->lon+TMath::Pi()/2),cos(orbitalinfo->lon+TMath::Pi()/2)+sin(orbitalinfo->lon+TMath::Pi()/2),1);
1317     //
1318     t_orb->cutoff = 59.3/(pow(orbitalinfo->L,2)*pow((1+sqrt(1-pow(orbitalinfo->L,-3/2)*cos(omega))),2));
1319     //
1320 mocchiut 1.36 if ( t_orb->pitch != t_orb->pitch ) t_orb->pitch = -1000.;
1321     if ( t_orb->cutoff != t_orb->cutoff ) t_orb->cutoff = -1000.;
1322     //
1323 mocchiut 1.33 if ( debug ) printf(" orbitalinfo->cutoffsvl %f vitaly %f \n",orbitalinfo->cutoffsvl,t_orb->cutoff);
1324 mocchiut 1.30 //
1325     new(tor[nn]) OrbitalInfoTrkVar(*t_orb);
1326     nn++;
1327     //
1328     t_orb->Clear();
1329     //
1330     };
1331     //
1332     };
1333     } else {
1334     if ( debug ) printf(" mmm... mode %u standalone %i ntrk %i \n",orbitalinfo->mode,standalone,tof->ntrk());
1335     };
1336     //
1337 mocchiut 1.35 } else {
1338     if ( !standalone && tof->ntrk() > 0 ){
1339     //
1340     Int_t nn = 0;
1341     for(Int_t nt=0; nt < tof->ntrk(); nt++){
1342     //
1343     ToFTrkVar *ptt = tof->GetToFTrkVar(nt);
1344     if ( ptt->trkseqno != -1 ){
1345     //
1346     t_orb->trkseqno = ptt->trkseqno;
1347     //
1348     t_orb->Eij = 0;
1349     //
1350     t_orb->Sij = 0;
1351     //
1352     t_orb->pitch = -1000.;
1353     //
1354     t_orb->cutoff = -1000.;
1355     //
1356     new(tor[nn]) OrbitalInfoTrkVar(*t_orb);
1357     nn++;
1358     //
1359     t_orb->Clear();
1360     //
1361     };
1362     //
1363     };
1364     };
1365 mocchiut 1.30 };
1366     //
1367 mocchiut 1.15 // Fill the class
1368 pamelaprod 1.11 //
1369 mocchiut 1.1 OrbitalInfotr->Fill();
1370 mocchiut 1.15 //
1371 mocchiut 1.30 delete t_orb;
1372     //
1373 mocchiut 1.15 }; // loop over the events in the run
1374 mocchiut 1.1 //
1375     // Here you may want to clear some variables before processing another run
1376     //
1377 mocchiut 1.44
1378     //gStyle->SetOptStat(000000);
1379     //gStyle->SetPalette(1);
1380    
1381     /*TCanvas* c1 = new TCanvas("c1","",1200,800);
1382     //c1->Divide(1,4);
1383     c1->cd(1);
1384     //q0testing->Draw("colz");
1385     //c1->cd(2);
1386     //q1testing->Draw("colz");
1387     //c1->cd(3);
1388     Pitchtesting->Draw("colz");
1389     //c1->cd(4);
1390     //q3testing->Draw("colz");
1391     c1->SaveAs("9.Rollhyst.png");
1392     delete c1;*/
1393    
1394 mocchiut 1.57 if ( verbose ) printf(" Clear before new run \n");
1395 mocchiut 1.5 delete dbtime;
1396 mocchiut 1.57
1397     mcmdrc->Clear();
1398     mcmdrc = 0;
1399    
1400     if ( verbose ) printf(" Clear before new run1 \n");
1401     if ( L_QQ_Q_l_lower ) delete L_QQ_Q_l_lower;
1402     if ( verbose ) printf(" Clear before new run2 \n");
1403 mocchiut 1.18 if ( L_QQ_Q_l_upper ) delete L_QQ_Q_l_upper;
1404 mocchiut 1.57 if ( verbose ) printf(" Clear before new run3 \n");
1405 mocchiut 1.18 if ( RYPang_upper ) delete RYPang_upper;
1406 mocchiut 1.57 if ( verbose ) printf(" Clear before new run4 \n");
1407 mocchiut 1.18 if ( RYPang_lower ) delete RYPang_lower;
1408 mocchiut 1.57
1409     if ( l0tr ) l0tr->Delete();
1410    
1411     if ( verbose ) printf(" End run \n");
1412    
1413 mocchiut 1.1 }; // process all the runs
1414 mocchiut 1.15
1415 mocchiut 1.1 if (verbose) printf("\n Finished processing data \n");
1416     //
1417     closeandexit:
1418     //
1419     // we have finished processing the run(s). If we processed a single run now we must copy all the events after our run from the old tree to the new one and delete the old tree.
1420     //
1421     if ( !reprocall && reproc && code >= 0 ){
1422     if ( totfileentries > noaftrun ){
1423     if (verbose){
1424     printf("\n Post-processing: copying events from the old tree after the processed run\n");
1425     printf(" Copying %i events in the file which are after the end of the run %i \n",(int)(totfileentries-noaftrun),(int)run);
1426     printf(" Start copying at event number %i end copying at event number %i \n",(int)noaftrun,(int)totfileentries);
1427     }
1428     for (UInt_t j = noaftrun; j < totfileentries; j++ ){
1429     //
1430     // Get entry from old tree
1431     //
1432 mocchiut 1.43 if ( OrbitalInfotrclone->GetEntry(j) <= 0 ) throw -36;
1433 mocchiut 1.1 //
1434     // copy orbitalinfoclone to OrbitalInfo
1435     //
1436 mocchiut 1.3 orbitalinfo->Clear();
1437 mocchiut 1.5 //
1438 mocchiut 1.1 memcpy(&orbitalinfo,&orbitalinfoclone,sizeof(orbitalinfoclone));
1439     //
1440     // Fill entry in the new tree
1441     //
1442     OrbitalInfotr->Fill();
1443     };
1444     if (verbose) printf(" Finished successful copying!\n");
1445     };
1446 mocchiut 1.57 //if ( OrbitalInfotrclone ) OrbitalInfotrclone->Clear();
1447     //if ( OrbitalInfotrclone ) OrbitalInfotrclone->Delete();
1448 mocchiut 1.1 };
1449     //
1450     // Close files, delete old tree(s), write and close level2 file
1451     //
1452     if ( l0File ) l0File->Close();
1453 mocchiut 1.23 if ( myfold ) gSystem->Unlink(tempname.str().c_str());
1454 mocchiut 1.5 //
1455 mocchiut 1.1 if ( OrbitalInfotr ) OrbitalInfotr->SetName("OrbitalInfo");
1456 mocchiut 1.30 //
1457 mocchiut 1.1 if ( file ){
1458     file->cd();
1459 mocchiut 1.57 OrbitalInfotr->Write("OrbitalInfo", TObject::kOverwrite);
1460 mocchiut 1.1 };
1461     //
1462 mocchiut 1.57 if (verbose) printf("\n Exiting...\n");
1463    
1464 mocchiut 1.23 if ( myfold ) gSystem->Unlink(OrbitalInfofolder.str().c_str());
1465 mocchiut 1.1 //
1466     // the end
1467     //
1468 mocchiut 1.25 if ( dbc ){
1469     dbc->Close();
1470     delete dbc;
1471     };
1472 mocchiut 1.57 //
1473 mocchiut 1.1 if (verbose) printf("\n Exiting...\n");
1474 mocchiut 1.57 if ( tempfile ) tempfile->Close();
1475    
1476 mocchiut 1.30 if ( PO ) delete PO;
1477 mocchiut 1.57 if ( gltle ) delete gltle;
1478     if ( glparam ) delete glparam;
1479     if ( glparam2 ) delete glparam2;
1480     if ( glparam3 ) delete glparam3;
1481     if (verbose) printf("\n Exiting3...\n");
1482 mocchiut 1.4 if ( glroot ) delete glroot;
1483 mocchiut 1.57 if (verbose) printf("\n Exiting4...\n");
1484     if ( runinfo ) runinfo->Close();
1485 mocchiut 1.4 if ( runinfo ) delete runinfo;
1486 mocchiut 1.58
1487     if ( debug ){
1488 mocchiut 1.57 cout << "1 0x" << OrbitalInfotr << endl;
1489     cout << "2 0x" << OrbitalInfotrclone << endl;
1490     cout << "3 0x" << l0tr << endl;
1491     cout << "4 0x" << tempOrbitalInfo << endl;
1492     cout << "5 0x" << ttof << endl;
1493 mocchiut 1.58 }
1494 mocchiut 1.57 //
1495 mocchiut 1.58 if ( debug ) file->ls();
1496 mocchiut 1.4 //
1497 mocchiut 1.1 if(code < 0) throw code;
1498     return(code);
1499     }
1500    
1501 mocchiut 1.7
1502     //
1503     // Returns the cCoordGeo structure holding the geographical
1504     // coordinates for the event (see sgp4.h).
1505     //
1506     // atime is the abstime of the event in UTC unix time.
1507     // tletime is the time of the tle in UTC unix time.
1508     // tle is the previous and nearest tle (compared to atime).
1509     cCoordGeo getCoo(UInt_t atime, UInt_t tletime, cTle *tle)
1510     {
1511     cEci eci;
1512     cOrbit orbit(*tle);
1513     orbit.getPosition((double) (atime - tletime)/60., &eci);
1514    
1515     return eci.toGeo();
1516     }
1517 mocchiut 1.15
1518     // function of copyng of quatrnions classes
1519    
1520     void CopyQ(Quaternions *Q1, Quaternions *Q2){
1521     for(UInt_t i = 0; i < 6; i++){
1522     Q1->time[i]=Q2->time[i];
1523     for (UInt_t j = 0; j < 4; j++)Q1->quat[i][j]=Q2->quat[i][j];
1524     }
1525     return;
1526     }
1527    
1528     // functions of copyng InclinationInfo classes
1529    
1530     void CopyAng(InclinationInfo *A1, InclinationInfo *A2){
1531     A1->Tangazh = A2->Tangazh;
1532     A1->Ryskanie = A2->Ryskanie;
1533     A1->Kren = A2->Kren;
1534     return;
1535     }
1536    
1537     UInt_t holeq(Double_t lower,Double_t upper,Quaternions *Qlower, Quaternions *Qupper, UInt_t f){
1538    
1539     UInt_t hole = 10;
1540 mocchiut 1.40 Bool_t R10l = false; // Sign of R10 mode in lower quaternions array
1541     Bool_t R10u = false; // Sign of R10 mode in upper quaternions array
1542     Bool_t insm = false; // Sign that we inside quaternions array
1543     Bool_t mxtml = false; // Sign of mixt mode in lower quaternions array
1544     Bool_t mxtmu = false; // Sign of mixt mode in upper quaternions array
1545     Bool_t npasm = false; // Sign of normall pass between R10 and non R10 or between non R10 and R10
1546 mocchiut 1.15 UInt_t NCQl = 6; // Number of correct quaternions in lower array
1547     UInt_t NCQu = 6; // Number of correct quaternions in upper array
1548     if (f>0){
1549     insm = true;
1550     if(Qupper->time[f]-Qupper->time[f-1]==30) R10u = false;
1551     if(Qupper->time[f]-Qupper->time[f-1]<1) R10u = true;
1552     }else{
1553     insm = false;
1554     if((Qlower->time[5]-Qlower->time[0]<2)&&(Qlower->time[1]-Qlower->time[0]<2)) R10l = true;
1555     if((Qupper->time[5]-Qupper->time[0]<2)&&(Qupper->time[1]-Qupper->time[0]<2)) R10u = true;
1556     if((Qlower->time[5]-Qlower->time[0]==150)&&(Qlower->time[1]-Qlower->time[0]==30)) R10l = false;
1557     if((Qupper->time[5]-Qupper->time[0]==150)&&(Qupper->time[1]-Qupper->time[0]==30)) R10u = false;
1558     if((Qlower->time[5]-Qlower->time[0]<2)&&(Qlower->time[1]-Qlower->time[0]==30)){
1559     mxtml = true;
1560     for(UInt_t i = 1; i < 6; i++){
1561     if(Qlower->time[i]-Qlower->time[0]==30*i) NCQl=i;
1562     }
1563     }
1564     if((Qupper->time[5]-Qupper->time[0]<2)&&(Qupper->time[1]-Qupper->time[0]==30)){
1565     mxtmu = true;
1566     for(UInt_t i = 1; i < 6; i++){
1567     if(Qupper->time[i]-Qupper->time[0]==30*i) NCQu=i;
1568     }
1569     }
1570     }
1571    
1572     if(((upper-lower==1.5)||(upper-lower==3.)||(upper-lower==30.)||(upper-lower==31.5)||(upper-lower==33.)||(upper-lower==181.5)||(upper-lower==210.)||(upper-lower==211.5))&&!insm) npasm = true;
1573    
1574    
1575     if (R10u&&insm) hole=0; // best event R10
1576     if ((upper-lower<=5)&&(!insm)&&R10l&&R10u) hole = 1; // when first of 6 quaternions in array is correct
1577     if (((!R10u)&&insm)||((!insm)&&(!R10u)&&(!R10l)&&((upper-lower==210+(6-NCQl)*30)||(upper-lower==30)))) hole = 2; //non R10
1578     if (npasm&&(!insm)&&((R10l&&!R10u)||(R10u&&!R10l))) hole = 3; //normall pass from R10 to non R10 or from non R10 to R10
1579     if ((!npasm)&&(upper-lower<=300)&&(!insm)&&((R10l&&!R10u)||(R10u&&!R10l))) hole = 4; // eliminable hole between R10 and non R10 or between non R10 and R10
1580     if ((upper-lower>=300)&&(!insm)&&((R10l&&!R10u)||(R10u&&!R10l))) hole = 5; //uneliminable hole between R10 and non R10 or between non R10 and R10
1581     if ((upper-lower>5)&&(upper-lower<=300)&&R10u&&R10l) hole = 6; // eliminable hole inside R10
1582     if ((upper-lower>300)&&R10u&&R10l) hole = 7; //uneliminable hole inside R10
1583     if ((upper-lower>210)&&(upper-lower<=1200)&&(!R10u)&&(!R10l)) hole = 8; //eliminable hole inside non R10
1584     if ((upper-lower>1200)&&!R10u&&!R10l) hole = 9; // uneliminable hole inside non R10
1585     return hole;
1586     }
1587    
1588 mocchiut 1.44 void inclresize(vector<Double_t>& t,vector<Float_t>& q0,vector<Float_t>& q1,vector<Float_t>& q2,vector<Float_t>& q3,vector<Int_t>& mode,vector<Float_t>& Roll,vector<Float_t>& Pitch,vector<Float_t>& Yaw){
1589     Int_t sizee = t.size()+1;
1590     t.resize(sizee);
1591     q0.resize(sizee);
1592     q1.resize(sizee);
1593     q2.resize(sizee);
1594     q3.resize(sizee);
1595     mode.resize(sizee);
1596     Roll.resize(sizee);
1597     Pitch.resize(sizee);
1598     Yaw.resize(sizee);
1599     }
1600    
1601     //Find fitting sine functions for q0,q1,q2,q3 and Yaw-angle;
1602     void sineparam(vector<Sine>& qsine, vector<Double_t>& qtime, vector<Float_t>& q, vector<Float_t>& Roll, vector<Float_t>& Pitch, Float_t limsin){
1603     UInt_t mulast = 0;
1604     UInt_t munow = 0;
1605     UInt_t munext = 0;
1606     Bool_t increase = false;
1607     Bool_t decrease = false;
1608     Bool_t Max_is_defined = false;
1609     Bool_t Start_point_is_defined = false;
1610     Bool_t Period_is_defined = false;
1611     Bool_t Large_gap = false;
1612     Bool_t normal_way = true;
1613     Bool_t small_gap_on_ridge = false;
1614     Double_t t1 = 0;
1615     Double_t t1A = 0;
1616     Int_t sinesize = 0;
1617     Int_t nfi = 0;
1618     for(UInt_t mu = 0;mu<qtime.size();mu++){
1619 mocchiut 1.47 //cout<<"Roll["<<mu<<"] = "<<Roll[mu]<<endl;
1620     if(TMath::Abs(Roll[mu])<1. && TMath::Abs(Pitch[mu])<1. && TMath::Abs(q[mu])<limsin){
1621     //cout<<"q["<<mu<<endl<<"] = "<<q[mu]<<endl;
1622     if(mulast!=0 && munow!=0 && munext!=0){mulast=munow;munow=munext;munext=mu;}
1623 mocchiut 1.44 if(munext==0 && munow!=0)munext=mu;
1624     if(munow==0 && mulast!=0)munow=mu;
1625     if(mulast==0)mulast=mu;
1626 mocchiut 1.47
1627     //cout<<"mulast = "<<mulast<<"\tmunow = "<<munow<<"\tmunext = "<<munext<<endl;
1628     //Int_t ref;
1629     //cin>>ref;
1630     if(TMath::Abs(q[munow])>TMath::Abs(q[mulast]) && TMath::Abs(q[munow])>TMath::Abs(q[munext]) && q[mulast]*q[munext]>0 && qtime[munext]-qtime[mulast]>400)small_gap_on_ridge = true;
1631 mocchiut 1.44 if(munext>mulast && (qtime[munext]-qtime[mulast]>=2000 || qtime[munext]-qtime[mulast]<0)){if(Large_gap){normal_way = false;Large_gap = false;}else{Large_gap = true;normal_way = false;}}else normal_way = true;
1632 mocchiut 1.47 //if(normal_way)cout<<"Normal_Way"<<endl;
1633 mocchiut 1.44 if(Large_gap || small_gap_on_ridge){
1634     //cout<<"Large gap..."<<endl;
1635     //if(small_gap_on_ridge)cout<<"small gap..."<<endl;
1636 mocchiut 1.47 //cout<<"q["<<mulast<<"] = "<<q[mulast]<<"\tq["<<munow<<"] = "<<q[munow]<<"\tq["<<munext<<"] = "<<q[munext]<<endl;
1637     //cout<<"qtime["<<mulast<<"] = "<<qtime[mulast]<<"\tqtime["<<munow<<"] = "<<qtime[munow]<<"\tqtime["<<munext<<"] = "<<qtime[munext]<<endl;
1638 mocchiut 1.44 increase = false;
1639     decrease = false;
1640     if(nfi>0){
1641     qsine.resize(qsine.size()-1);
1642     sinesize = qsine.size();
1643 mocchiut 1.47 //cout<<"nfi was larger then zero"<<endl;
1644 mocchiut 1.44 }else{
1645 mocchiut 1.47 //cout<<"nfi was not larger then zero :( nfi = "<<nfi<<endl;
1646     //cout<<"qsine.size = "<<qsine.size()<<endl;
1647 mocchiut 1.44 if(!Period_is_defined){
1648 mocchiut 1.47 //cout<<"Period was defined"<<endl;
1649 mocchiut 1.44 if(qsine.size()>1){
1650     qsine[sinesize-1].b = qsine[sinesize-2].b;
1651     qsine[sinesize-1].c = qsine[sinesize-2].c;
1652     }else{
1653     qsine[sinesize-1].b = TMath::Pi()/1591.54;
1654     qsine[sinesize-1].c = qsine[sinesize-1].startPoint;
1655     }
1656     }
1657     if(!Max_is_defined){
1658 mocchiut 1.47 //cout<<"Max was already defined"<<endl;
1659 mocchiut 1.44 if(qsine.size()>1)qsine[sinesize-1].A = qsine[sinesize-2].A;else qsine[sinesize-1].A = limsin;
1660     }
1661     qsine[sinesize-1].NeedFit = true;
1662     }
1663     qsine[sinesize-1].finishPoint = qtime[munow];
1664 mocchiut 1.47 //cout<<"finish point before large gap = "<<qtime[munow]<<endl;
1665 mocchiut 1.44 nfi = 0;
1666     Max_is_defined = false;
1667     Start_point_is_defined = false;
1668     Period_is_defined = false;
1669     small_gap_on_ridge = false;
1670     }
1671 mocchiut 1.47 //cout<<"Slope "<<increase<<"\t"<<decrease<<endl;
1672     //cout<<"mulast = "<<mulast<<"\tmunow = "<<munow<<"\tmunext = "<<munext<<endl;
1673     if((munext>munow) && (munow>mulast) && normal_way){
1674 mocchiut 1.44 if(!increase && !decrease){
1675 mocchiut 1.47 //cout<<"Normal way have started"<<endl;
1676 mocchiut 1.44 qsine.resize(qsine.size()+1);
1677     sinesize = qsine.size();
1678     qsine[sinesize-1].startPoint=qtime[mulast];
1679     if(q[munext]>q[munow] && q[munow]>q[mulast]) increase = true;
1680     if(q[munext]<q[munow] && q[munow]<q[mulast]) decrease = true;
1681     }
1682     //if(TMath::Abs(q[munow])>TMath::Abs(q[mulast]) && TMath::Abs(q[munow])>TMath::Abs(q[munext]) && TMath::Abs(q[munow])>limsin && qtime[munow]-qtime[mulast]>=400 || qtime[munext]-qtime[munow]>=400){small_gap_on_ridge = true;mu--;continue;}
1683 mocchiut 1.47 if(TMath::Abs(q[munow])>TMath::Abs(q[mulast]) && TMath::Abs(q[munow])>TMath::Abs(q[munext]) && TMath::Abs(q[munow])>0.9*limsin && qtime[munow]-qtime[mulast]<400 && qtime[munext]-qtime[munow]<400){
1684 mocchiut 1.44 //cout<<"Max point is qtime = "<<qtime[munow]<<"\tq = "<<q[munow]<<endl;
1685     if(q[munow]>q[mulast]){
1686     increase = false;
1687     decrease = true;
1688     }
1689     if(q[munow]<q[mulast]){
1690     increase = true;
1691     decrease = false;
1692     }
1693     if(Max_is_defined && !Start_point_is_defined){
1694     Double_t qPer = qtime[munow]-t1A;
1695     if(qPer>1000){
1696     //cout<<"qsine["<<sinesize-1<<"] = "<<qPer<<" = "<<qtime[munow]<<" - "<<t1A<<"\tlim = "<<limsin<<endl;
1697     qsine[sinesize-1].b=TMath::Pi()/qPer;
1698     if(decrease)qsine[sinesize-1].c=-qsine[sinesize-1].b*t1A;
1699     if(increase)qsine[sinesize-1].c=-qsine[sinesize-1].b*(t1A-qPer);
1700     Period_is_defined = true;
1701     }
1702     }
1703     Max_is_defined = true;
1704     qsine[sinesize-1].A = TMath::Abs(q[munow]);
1705     if(Start_point_is_defined && Period_is_defined){
1706     qsine[sinesize-1].finishPoint = qtime[munow];
1707     nfi++;
1708     qsine[sinesize-1].NeedFit = false;
1709     Max_is_defined = false;
1710     Start_point_is_defined = false;
1711     Period_is_defined = false;
1712     qsine.resize(qsine.size()+1);
1713     sinesize = qsine.size();
1714     qsine[sinesize-1].startPoint = qtime[munow];
1715     }
1716     if(!Start_point_is_defined) t1A=qtime[munow];
1717     }
1718     //if((q[munow]>=0 && q[mulast]<=0) || (q[munow]<=0 && q[mulast]>=0))cout<<"cross zero point diference = "<<qtime[munext] - qtime[mulast]<<"\tqlast = "<<qtime[mulast]<<"\tqnow = "<<qtime[munow]<<"\tqnext = "<<qtime[munext]<<endl;
1719     if(((q[munow]>=0 && q[mulast]<=0) || (q[munow]<=0 && q[mulast]>=0)) && qtime[munow]-qtime[mulast]<2000 && qtime[munext]-qtime[munow]<2000){
1720     Double_t tcrosszero = 0;
1721     //cout<<"cross zero point...qtime = "<<qtime[munow]<<endl;
1722     if(q[munow]==0.) tcrosszero = qtime[munow];else
1723     if(q[mulast]==0.)tcrosszero = qtime[mulast];else{
1724     Double_t k_ = (q[munow]-q[mulast])/(qtime[munow]-qtime[mulast]);
1725     Double_t b_ = q[munow]-k_*qtime[munow];
1726     tcrosszero = -b_/k_;
1727     }
1728     if(Start_point_is_defined){
1729     //cout<<"Start Point allready defined"<<endl;
1730     Double_t qPer = tcrosszero - t1;
1731     qsine[sinesize-1].b = TMath::Pi()/qPer;
1732     //cout<<"qsine["<<sinesize-1<<"].b = "<<TMath::Pi()/qPer<<endl;
1733     Period_is_defined = true;
1734     Float_t x0 = 0;
1735     if(decrease)x0 = t1;
1736     if(increase)x0 = tcrosszero;
1737     qsine[sinesize-1].c = -qsine[sinesize-1].b*x0;
1738     if(Max_is_defined){
1739     //cout<<"Max was previous defined"<<endl;
1740     qsine[sinesize-1].finishPoint = qtime[munow];
1741     nfi++;
1742     qsine[sinesize-1].NeedFit = false;
1743     Max_is_defined = false;
1744     t1 = tcrosszero;
1745     Start_point_is_defined = true;
1746     Period_is_defined = false;
1747     qsine.resize(qsine.size()+1);
1748     sinesize = qsine.size();
1749     qsine[sinesize-1].startPoint = qtime[munow];
1750     }
1751     }else{
1752     t1 = tcrosszero;
1753     Start_point_is_defined = true;
1754     }
1755     }
1756     }
1757     }
1758     }
1759    
1760     //cout<<"FINISH SINE INTERPOLATION FUNCTION..."<<endl<<endl;
1761     }

  ViewVC Help
Powered by ViewVC 1.1.23