/[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.92 - (hide annotations) (download)
Tue Nov 17 10:10:06 2015 UTC (9 years, 3 months ago) by pamela
Branch: MAIN
Changes since 1.91: +66 -16 lines
Bug related to events before first quaternions packet fixed

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 pam-mep 1.60 #include <TH2F.h> //for test only. Vitaly.
13     #include <TVector3.h>
14 mocchiut 1.44 //#include <TF1.h>
15    
16 mocchiut 1.1 #include <TTree.h>
17     #include <TClassEdit.h>
18     #include <TObject.h>
19     #include <TList.h>
20 mocchiut 1.5 #include <TArrayI.h>
21 mocchiut 1.1 #include <TSystem.h>
22     #include <TSystemDirectory.h>
23     #include <TString.h>
24     #include <TFile.h>
25     #include <TClass.h>
26     #include <TSQLServer.h>
27     #include <TSQLRow.h>
28     #include <TSQLResult.h>
29 mocchiut 1.81 #include <TObjectTable.h>
30 mocchiut 1.1 //
31 mocchiut 1.8 // RunInfo header
32     //
33     #include <RunInfo.h>
34     #include <GLTables.h>
35     //
36 mocchiut 1.1 // YODA headers
37     //
38     #include <PamelaRun.h>
39     #include <PscuHeader.h>
40     #include <PscuEvent.h>
41     #include <EventHeader.h>
42 mocchiut 1.15 #include <mcmd/McmdEvent.h>
43     #include <mcmd/McmdRecord.h>
44 mocchiut 1.1 //
45     // This program headers
46     //
47     #include <OrbitalInfo.h>
48 mocchiut 1.7 #include <OrbitalInfoVerl2.h>
49 mocchiut 1.1 #include <OrbitalInfoCore.h>
50 mocchiut 1.15 #include <InclinationInfo.h>
51 mocchiut 1.1
52 pam-mep 1.69 //
53     // Tracker and ToF classes headers and definitions
54     //
55     #include <ToFLevel2.h>
56     #include <TrkLevel2.h>
57 mocchiut 1.73 #include <ExtTrack.h> // new tracking code
58 mocchiut 1.40
59 mocchiut 1.1 using namespace std;
60    
61     //
62     // CORE ROUTINE
63     //
64     //
65 mocchiut 1.81 int OrbitalInfoCore(UInt_t run, TFile *file, GL_TABLES *glt, Int_t OrbitalInfoargc, char *OrbitalInfoargv[]){
66 mocchiut 1.15 //
67 mocchiut 1.1 Int_t i = 0;
68 mocchiut 1.25 TString host = glt->CGetHost();
69     TString user = glt->CGetUser();
70     TString psw = glt->CGetPsw();
71     TSQLServer *dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
72 mocchiut 1.1 //
73 mocchiut 1.26 stringstream myquery;
74     myquery.str("");
75 mocchiut 1.79 myquery << "SET time_zone='+0:00';";
76 mocchiut 1.59 delete dbc->Query(myquery.str().c_str());
77 mocchiut 1.79 delete dbc->Query("SET sql_mode = 'NO_UNSIGNED_SUBTRACTION';");
78 mocchiut 1.26 //
79 mocchiut 1.16 TString processFolder = Form("OrbitalInfoFolder_%u",run);
80 mocchiut 1.1 //
81     // Set these to true to have a very verbose output.
82     //
83     Bool_t debug = false;
84     //
85     Bool_t verbose = false;
86 mocchiut 1.30 //
87 mocchiut 1.31 Bool_t standalone = false;
88 mocchiut 1.30 //
89 mocchiut 1.1 if ( OrbitalInfoargc > 0 ){
90     i = 0;
91     while ( i < OrbitalInfoargc ){
92     if ( !strcmp(OrbitalInfoargv[i],"-processFolder") ) {
93     if ( OrbitalInfoargc < i+1 ){
94     throw -3;
95 mocchiut 1.81 }
96 mocchiut 1.1 processFolder = (TString)OrbitalInfoargv[i+1];
97     i++;
98 mocchiut 1.81 }
99 mocchiut 1.1 if ( (!strcmp(OrbitalInfoargv[i],"--debug")) || (!strcmp(OrbitalInfoargv[i],"-g")) ) {
100     verbose = true;
101 mocchiut 1.15 debug = true;
102 mocchiut 1.81 }
103 mocchiut 1.1 if ( (!strcmp(OrbitalInfoargv[i],"--verbose")) || (!strcmp(OrbitalInfoargv[i],"-v")) ) {
104     verbose = true;
105 mocchiut 1.81 }
106 mocchiut 1.30 if ( (!strcmp(OrbitalInfoargv[i],"--standalone")) ) {
107     standalone = true;
108 mocchiut 1.81 }
109 mocchiut 1.30 if ( (!strcmp(OrbitalInfoargv[i],"--calculate-pitch")) ) {
110     standalone = false;
111 mocchiut 1.81 }
112 mocchiut 1.1 i++;
113 mocchiut 1.81 }
114     }
115     if ( debug ){
116     printf("START\n");
117     gObjectTable->Print();
118     }
119 mocchiut 1.1 //
120     const char* outDir = gSystem->DirName(gSystem->DirName(file->GetPath()));
121     //
122     TTree *OrbitalInfotr = 0;
123 mocchiut 1.5 UInt_t nevents = 0;
124 mocchiut 1.15 UInt_t neventsm = 0;
125 mocchiut 1.1 //
126     // variables needed to reprocess data
127     //
128 mocchiut 1.6 Long64_t maxsize = 10000000000LL;
129     TTree::SetMaxTreeSize(maxsize);
130     //
131 mocchiut 1.1 TString OrbitalInfoversion;
132     ItoRunInfo *runinfo = 0;
133 mocchiut 1.5 TArrayI *runlist = 0;
134 mocchiut 1.1 TTree *OrbitalInfotrclone = 0;
135     Bool_t reproc = false;
136     Bool_t reprocall = false;
137 mocchiut 1.66 Bool_t igrfloaded = false;
138 mocchiut 1.1 UInt_t nobefrun = 0;
139     UInt_t noaftrun = 0;
140     UInt_t numbofrun = 0;
141     stringstream ftmpname;
142     TString fname;
143 mocchiut 1.5 UInt_t totfileentries = 0;
144 mocchiut 1.15 UInt_t idRun = 0;
145 pam-fi 1.53 UInt_t anni5 = 60 * 60 * 24 * 365 * 5 ;//1576800
146 mocchiut 1.15 //
147     // My variables. Vitaly.
148     //
149 mocchiut 1.44 // UInt_t oi = 0;
150 mocchiut 1.15 Int_t tmpSize = 0;
151 mocchiut 1.1 //
152     // variables needed to handle error signals
153     //
154     Int_t code = 0;
155     Int_t sgnl;
156     //
157     // OrbitalInfo classes
158     //
159     OrbitalInfo *orbitalinfo = new OrbitalInfo();
160     OrbitalInfo *orbitalinfoclone = new OrbitalInfo();
161 mocchiut 1.44
162 mocchiut 1.1 //
163     // define variables for opening and reading level0 file
164     //
165     TFile *l0File = 0;
166     TTree *l0tr = 0;
167 mocchiut 1.37 // TTree *l0trm = 0;
168     TChain *ch = 0;
169 mocchiut 1.2 // EM: open also header branch
170     TBranch *l0head = 0;
171     pamela::EventHeader *eh = 0;
172     pamela::PscuHeader *ph = 0;
173 mocchiut 1.15 pamela::McmdEvent *mcmdev = 0;
174     pamela::McmdRecord *mcmdrc = 0;
175 mocchiut 1.2 // end EM
176 mocchiut 1.15
177     // pamela::RunHeaderEvent *reh = new pamela::RunHeaderEvent;
178     // pamela::EventHeader *eH = new pamela::EventHeader;
179    
180 mocchiut 1.1 //
181     // Define other basic variables
182     //
183     UInt_t procev = 0;
184     stringstream file2;
185     stringstream file3;
186     stringstream qy;
187     Int_t totevent = 0;
188 mocchiut 1.5 UInt_t atime = 0;
189     UInt_t re = 0;
190 mocchiut 1.15 UInt_t ik = 0;
191 mocchiut 1.7
192     // Position
193     Float_t lon, lat, alt;
194    
195     //
196     // IGRF stuff
197     //
198 mocchiut 1.81 Float_t dimo = 0.0; // dipole moment (computed from dat files) // EM GCC 4.7
199 mocchiut 1.40 Float_t bnorth, beast, bdown, babs;
200     Float_t xl; // L value
201 mocchiut 1.85 Int_t icode; // code value for L accuracy (see fortran code)
202 mocchiut 1.40 Float_t bab1; // What's the difference with babs?
203     Float_t stps = 0.005; // step size for field line tracing
204     Float_t bdel = 0.01; // required accuracy
205     Float_t bequ; // equatorial b value (also called b_0)
206     Bool_t value = 0; // false if bequ is not the minimum b value
207     Float_t rr0; // equatorial radius normalized to earth radius
208 mocchiut 1.7
209 mocchiut 1.1 //
210     // Working filename
211     //
212     TString outputfile;
213     stringstream name;
214     name.str("");
215     name << outDir << "/";
216     //
217     // temporary file and folder
218     //
219     TFile *tempfile = 0;
220     TTree *tempOrbitalInfo = 0;
221     stringstream tempname;
222     stringstream OrbitalInfofolder;
223 mocchiut 1.23 Bool_t myfold = false;
224 mocchiut 1.1 tempname.str("");
225     tempname << outDir;
226     tempname << "/" << processFolder.Data();
227     OrbitalInfofolder.str("");
228     OrbitalInfofolder << tempname.str().c_str();
229     tempname << "/OrbitalInfotree_run";
230     tempname << run << ".root";
231 mocchiut 1.40 UInt_t totnorun = 0;
232 mocchiut 1.1 //
233     // DB classes
234     //
235     GL_ROOT *glroot = new GL_ROOT();
236 mocchiut 1.5 GL_TIMESYNC *dbtime = 0;
237 mocchiut 1.7 GL_TLE *gltle = new GL_TLE();
238 mocchiut 1.15 //
239     //Quaternions classes
240     //
241 mocchiut 1.57 Quaternions *L_QQ_Q_l_lower = 0;
242     InclinationInfo *RYPang_lower = 0;
243     Quaternions *L_QQ_Q_l_upper = 0;
244     InclinationInfo *RYPang_upper = 0;
245 mocchiut 1.15
246     cEci eCi;
247    
248 mocchiut 1.7 // Initialize fortran routines!!!
249 mocchiut 1.52 Int_t ltp1 = 0;
250 mocchiut 1.7 Int_t ltp2 = 0;
251 pam-mep 1.69 GL_PARAM *glparam0 = new GL_PARAM();
252 mocchiut 1.7 GL_PARAM *glparam = new GL_PARAM();
253 mocchiut 1.10 GL_PARAM *glparam2 = new GL_PARAM();
254 mocchiut 1.44
255 mocchiut 1.30 //
256 mocchiut 1.44 // Orientation variables. Vitaly
257 mocchiut 1.30 //
258 malakhov 1.74
259 mocchiut 1.30 UInt_t evfrom = 0;
260     UInt_t jumped = 0;
261     Int_t itr = -1;
262 mocchiut 1.64 // Double_t A1;
263     // Double_t A2;
264     // Double_t A3;
265 mocchiut 1.30 Double_t Px = 0;
266     Double_t Py = 0;
267     Double_t Pz = 0;
268     TTree *ttof = 0;
269     ToFLevel2 *tof = new ToFLevel2();
270 pam-mep 1.69 TTree *ttrke = 0;
271     TrkLevel2 *trke = new TrkLevel2();
272 mocchiut 1.30 OrientationInfo *PO = new OrientationInfo();
273     Int_t nz = 6;
274     Float_t zin[6];
275     Int_t nevtofl2 = 0;
276 pam-mep 1.69 Int_t nevtrkl2 = 0;
277 mocchiut 1.44 if ( verbose ) cout<<"Reading quaternions external file"<<endl;
278     cout.setf(ios::fixed,ios::floatfield);
279     /******Reading recovered quaternions...*********/
280     vector<Double_t> recqtime;
281     vector<Float_t> recq0;
282     vector<Float_t> recq1;
283     vector<Float_t> recq2;
284     vector<Float_t> recq3;
285     Float_t Norm = 1;
286 mocchiut 1.81 recqtime.reserve(1500000);
287     recq0.reserve(1500000);
288     recq1.reserve(1500000);
289     recq2.reserve(1500000);
290     recq3.reserve(1500000);
291 mocchiut 1.73
292     vector<UInt_t> RTtime1;
293     vector<UInt_t> RTtime2;
294     vector<Double_t> RTbank1;
295     vector<Double_t> RTbank2;
296 malakhov 1.74 vector<Double_t> RTbpluto1;
297     vector<Double_t> RTbpluto2;
298 mocchiut 1.73 vector<Int_t> RTazim;
299 mocchiut 1.75 vector<UInt_t> RTstart;
300     vector<UInt_t> RTpluto2;
301     vector<UInt_t> RTpluto1;
302 mocchiut 1.73 vector<Int_t> RTerrq;
303 malakhov 1.82 vector<Int_t> RTqual;
304 mocchiut 1.81 RTtime1.reserve(200000);
305     RTtime2.reserve(200000);
306     RTbank1.reserve(200000);
307     RTbank2.reserve(200000);
308     RTbpluto1.reserve(200000);
309     RTbpluto2.reserve(200000);
310     RTazim.reserve(200000);
311     RTstart.reserve(200000);
312     RTpluto1.reserve(200000);
313     RTpluto2.reserve(200000);
314     RTerrq.reserve(200000);
315 malakhov 1.82 RTqual.reserve(200000);
316 mocchiut 1.73
317     TClonesArray *tcNucleiTrk = NULL;
318     TClonesArray *tcExtNucleiTrk = NULL;
319     TClonesArray *tcExtTrk = NULL;
320     TClonesArray *tcNucleiTof = NULL;
321     TClonesArray *tcExtNucleiTof = NULL;
322     TClonesArray *tcExtTof = NULL;
323     TClonesArray *torbNucleiTrk = NULL;
324     TClonesArray *torbExtNucleiTrk = NULL;
325     TClonesArray *torbExtTrk = NULL;
326     Bool_t hasNucleiTrk = false;
327     Bool_t hasExtNucleiTrk = false;
328     Bool_t hasExtTrk = false;
329     Bool_t hasNucleiTof = false;
330     Bool_t hasExtNucleiTof = false;
331     Bool_t hasExtTof = false;
332    
333     ifstream in;
334     ifstream an;
335     // ofstream mc;
336     // TString gr;
337     Int_t parerror2=0;
338    
339 pam-mep 1.69 Int_t parerror=glparam0->Query_GL_PARAM(1,303,dbc); // parameters stored in DB in GL_PRAM table
340 mocchiut 1.72 if ( verbose ) cout<<parerror<<"\t"<<(char*)(glparam0->PATH+glparam0->NAME).Data()<<endl;
341 mocchiut 1.44 if ( parerror<0 ) {
342     code = parerror;
343 mocchiut 1.73 goto closeandexit;
344 pam-mep 1.54 }
345 mocchiut 1.73 in.open((char*)(glparam0->PATH+glparam0->NAME).Data(),ios::in);
346 mocchiut 1.44 while(!in.eof()){
347     recqtime.resize(recqtime.size()+1);
348     Int_t sizee = recqtime.size();
349     recq0.resize(sizee);
350     recq1.resize(sizee);
351     recq2.resize(sizee);
352     recq3.resize(sizee);
353     in>>recqtime[sizee-1];
354     in>>recq0[sizee-1];
355     in>>recq1[sizee-1];
356     in>>recq2[sizee-1];
357     in>>recq3[sizee-1];
358     in>>Norm;
359 malakhov 1.82 /* CHECK RECOVERED QUATERNIONS PROBLEM
360     if(recqtime[sizee-1]>=1160987921.75 && recqtime[sizee-1]<=1160987932.00){
361     cout<<"We found it at start"<<"\t"<<recqtime[sizee-1]<<endl;
362     } */
363 mocchiut 1.44 }
364 pam-mep 1.69 in.close();
365 mocchiut 1.44 if ( verbose ) cout<<"We have read recovered data"<<endl;
366 pam-mep 1.69 if (debug) cout << "size of recovered quaterions data set is " << recqtime.size() << endl;
367 mocchiut 1.81 if ( debug ) printf(" RQ size %i RQ capacity %i \n",(int)recqtime.size(),(int)recqtime.capacity());
368    
369 pam-mep 1.69 if ( verbose ) cout<<"read Rotation Table"<<endl;
370 mocchiut 1.73
371     parerror2=glparam0->Query_GL_PARAM(1,305,dbc);
372 malakhov 1.74
373 mocchiut 1.71 if ( verbose ) cout<<parerror2<<"\t"<<(char*)(glparam0->PATH+glparam0->NAME).Data()<<endl;
374 mocchiut 1.73 if ( parerror2<0 ) {
375     code = parerror;
376     goto closeandexit;
377     }
378     an.open((char*)(glparam0->PATH+glparam0->NAME).Data(),ios::in);
379 pam-mep 1.69 while(!an.eof()){
380     RTtime1.resize(RTtime1.size()+1);
381     Int_t sizee = RTtime1.size();
382     RTbank1.resize(sizee+1);
383     RTazim.resize(sizee+1);
384     RTerrq.resize(sizee+1);
385 malakhov 1.74 RTstart.resize(sizee+1);
386     RTpluto1.resize(sizee+1);
387     RTbpluto1.resize(sizee+1);
388 malakhov 1.82 RTqual.resize(sizee+1);
389 pam-mep 1.69 an>>RTtime1[sizee-1];
390     an>>RTbank1[sizee-1];
391 malakhov 1.74 an>>RTstart[sizee-1];
392     an>>RTpluto1[sizee-1];
393     an>>RTbpluto1[sizee-1];
394 pam-mep 1.69 an>>RTazim[sizee-1];
395     an>>RTerrq[sizee-1];
396 malakhov 1.82 an>>RTqual[sizee-1];
397 pam-mep 1.69 if(sizee>1) {
398     RTtime2.resize(sizee+1);
399     RTbank2.resize(sizee+1);
400 malakhov 1.74 RTpluto2.resize(sizee+1);
401     RTbpluto2.resize(sizee+1);
402 pam-mep 1.69 RTtime2[sizee-2]=RTtime1[sizee-1];
403 malakhov 1.74 RTpluto2[sizee-2]=RTpluto1[sizee-1];
404 pam-mep 1.69 RTbank2[sizee-2]=RTbank1[sizee-1];
405 malakhov 1.74 RTbpluto2[sizee-2]=RTbpluto1[sizee-1];
406 pam-mep 1.69 }
407     }
408     an.close();
409     //cout<<"put some number here"<<endl;
410     //Int_t yupi;
411     //cin>>yupi;
412    
413     if ( verbose ) cout<<"We have read Rotation Table"<<endl;
414     //Geomagnetic coordinates calculations staff
415 mocchiut 1.81
416     if ( debug ) printf(" RT size %i RT capacity %i \n",(int)RTtime2.size(),(int)RTtime2.capacity());
417 pam-mep 1.69
418     GMtype_CoordGeodetic location;
419 mocchiut 1.70 // GMtype_CoordDipole GMlocation;
420 pam-mep 1.69 GMtype_Ellipsoid Ellip;
421     GMtype_Data G0, G1, H1;
422    
423     // { // this braces is necessary to avoid jump to label 'closeandexit' error // but it is wrong since the variable "igpath" will not exist outside. To overcome the "jump to label 'closeandexit' error" it is necessary to set the "igpath" before line 276
424     // TString igpath="/data03/Malakhov/pam9Malakhov/installed10/calib/orb-param/";
425     // }
426    
427     //cout << G0.element[0] << "\t" << G1.element[0] << "\t" << H1.element[0] << endl;
428     //cout << G0.element[5] << "\t" << G1.element[5] << "\t" << H1.element[5] << endl;
429    
430     GM_SetEllipsoid(&Ellip);
431 mocchiut 1.44
432 mocchiut 1.55 // IGRF stuff moved inside run loop!
433    
434 mocchiut 1.30 for (Int_t ip=0;ip<nz;ip++){
435     zin[ip] = tof->GetZTOF(tof->GetToFPlaneID(ip));
436     };
437     //
438     if ( !standalone ){
439     //
440 mocchiut 1.73 // Does it contain the Tracker and ToF trees?
441 mocchiut 1.30 //
442     ttof = (TTree*)file->Get("ToF");
443     if ( !ttof ) {
444     if ( verbose ) printf(" OrbitalInfo - ERROR: no tof tree\n");
445     code = -900;
446     goto closeandexit;
447 pam-mep 1.69 }
448 mocchiut 1.30 ttof->SetBranchAddress("ToFLevel2",&tof);
449     nevtofl2 = ttof->GetEntries();
450 pam-mep 1.69
451 mocchiut 1.73 //
452     // Look for extended tracking algorithm
453     //
454     if ( verbose ) printf("Look for extended and nuclei tracking algorithms in ToF\n");
455     // Nuclei tracking algorithm
456     Int_t checkAlgo = 0;
457     tcNucleiTof = new TClonesArray("ToFTrkVar");
458     checkAlgo = ttof->SetBranchAddress("TrackNuclei",&tcNucleiTof);
459     if ( !checkAlgo ){
460     if ( verbose ) printf(" Nuclei tracking algorithm ToF branch found! :D \n");
461     hasNucleiTof = true;
462     } else {
463     if ( verbose ) printf(" Nuclei tracking algorithm ToF branch not found :( !\n");
464     printf(" ok, this is not a problem (it depends on tracker settings) \n");
465     delete tcNucleiTof;
466 mocchiutti 1.77 tcNucleiTof=NULL; // 10RED reprocessing bug
467 mocchiut 1.73 }
468     // Nuclei tracking algorithm using calorimeter points
469     tcExtNucleiTof = new TClonesArray("ToFTrkVar");
470     checkAlgo = ttof->SetBranchAddress("RecoveredTrackNuclei",&tcExtNucleiTof);
471     if ( !checkAlgo ){
472     if ( verbose ) printf(" Recovered nuclei tracking algorithm ToF branch found! :D \n");
473     hasExtNucleiTof = true;
474     } else {
475     if ( verbose ) printf(" Recovered nuclei tracking algorithm ToF branch not found :( !\n");
476     printf(" ok, this is not a problem (it depends on tracker settings) \n");
477     delete tcExtNucleiTof;
478 mocchiutti 1.77 tcExtNucleiTof=NULL; // 10RED reprocessing bug
479 mocchiut 1.73 }
480     // Tracking algorithm using calorimeter points
481     tcExtTof = new TClonesArray("ToFTrkVar");
482     checkAlgo = ttof->SetBranchAddress("RecoveredTrack",&tcExtTof);
483     if ( !checkAlgo ){
484     if ( verbose ) printf(" Recovered track algorithm ToF branch found! :D \n");
485     hasExtTof = true;
486     } else {
487     if ( verbose ) printf(" Recovered track algorithm ToF branch not found :( !\n");
488     printf(" ok, this is not a problem (it depends on tracker settings) \n");
489     delete tcExtTof;
490 mocchiutti 1.77 tcExtTof=NULL; // 10RED reprocessing bug
491 mocchiut 1.73 }
492    
493 pam-mep 1.69 ttrke = (TTree*)file->Get("Tracker");
494     if ( !ttrke ) {
495     if ( verbose ) printf(" OrbitalInfo - ERROR: no trk tree\n");
496     code = -903;
497     goto closeandexit;
498     }
499     ttrke->SetBranchAddress("TrkLevel2",&trke);
500     nevtrkl2 = ttrke->GetEntries();
501 mocchiut 1.73
502     //
503     // Look for extended tracking algorithm
504     //
505     if ( verbose ) printf("Look for extended and nuclei tracking algorithms\n");
506     // Nuclei tracking algorithm
507     checkAlgo = 0;
508     tcNucleiTrk = new TClonesArray("TrkTrack");
509     checkAlgo = ttrke->SetBranchAddress("TrackNuclei",&tcNucleiTrk);
510     if ( !checkAlgo ){
511     if ( verbose ) printf(" Nuclei tracking algorithm branch found! :D \n");
512     hasNucleiTrk = true;
513     } else {
514     if ( verbose ) printf(" Nuclei tracking algorithm branch not found :( !\n");
515     printf(" ok, this is not a problem (it depends on tracker settings) \n");
516     delete tcNucleiTrk;
517 mocchiutti 1.77 tcNucleiTrk=NULL; // 10RED reprocessing bug
518 mocchiut 1.73 }
519     // Nuclei tracking algorithm using calorimeter points
520     tcExtNucleiTrk = new TClonesArray("ExtTrack");
521     checkAlgo = ttrke->SetBranchAddress("RecoveredTrackNuclei",&tcExtNucleiTrk);
522     if ( !checkAlgo ){
523     if ( verbose ) printf(" Recovered nuclei tracking algorithm branch found! :D \n");
524     hasExtNucleiTrk = true;
525     } else {
526     if ( verbose ) printf(" Recovered nuclei tracking algorithm branch not found :( !\n");
527     printf(" ok, this is not a problem (it depends on tracker settings) \n");
528     delete tcExtNucleiTrk;
529 mocchiutti 1.77 tcExtNucleiTrk=NULL; // 10RED reprocessing bug
530 mocchiut 1.73 }
531     // Tracking algorithm using calorimeter points
532     tcExtTrk = new TClonesArray("ExtTrack");
533     checkAlgo = ttrke->SetBranchAddress("RecoveredTrack",&tcExtTrk);
534     if ( !checkAlgo ){
535     if ( verbose ) printf(" Recovered track algorithm branch found! :D \n");
536     hasExtTrk = true;
537     } else {
538     if ( verbose ) printf(" Recovered track algorithm branch not found :( !\n");
539     printf(" ok, this is not a problem (it depends on tracker settings) \n");
540     delete tcExtTrk;
541 mocchiutti 1.77 tcExtTrk=NULL; // 10RED reprocessing bug
542 mocchiut 1.73 }
543    
544     if ( (hasNucleiTrk && !hasNucleiTof) || (!hasNucleiTrk && hasNucleiTof) ||
545     (hasExtNucleiTrk && !hasExtNucleiTof) || (!hasExtNucleiTrk && hasExtNucleiTof) ||
546     (hasExtTrk && !hasExtTof) || (!hasExtTrk && hasExtTof)
547     ){
548     if ( verbose ) printf(" ERROR: Mismatch between tracker and tof tree branches concerning extended tracking algorithm(s)\n");
549     if ( debug ) printf("hasNucleiTrk %i hasExtNucleiTrk %i hasExtTrk %i \n",hasNucleiTrk,hasExtNucleiTrk,hasExtTrk);
550     if ( debug ) printf("hasNucleiTof %i hasExtNucleiTof %i hasExtTof %i \n",hasNucleiTof,hasExtNucleiTof,hasExtTof);
551     throw -901;
552     }
553    
554 pam-mep 1.69 }
555 mocchiut 1.1 //
556     // Let's start!
557     //
558     // 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
559     // if run != 0 we must process only that run but first we have to check if the tree MyDetector2 already exist in the file
560     // if it exists we are reprocessing data and we must delete that entries, if not we must create it.
561     //
562 mocchiut 1.5 if ( run == 0 ) reproc = true;
563 mocchiut 1.1 //
564     //
565     // Output file is "outputfile"
566     //
567     if ( !file->IsOpen() ){
568     //printf(" OrbitalInfo - ERROR: cannot open file for writing\n");
569     throw -901;
570     };
571     //
572     // Retrieve GL_RUN variables from the level2 file
573     //
574     OrbitalInfoversion = OrbitalInfoInfo(false); // we should decide how to handle versioning system
575     //
576     // create an interface to RunInfo called "runinfo"
577     //
578     runinfo = new ItoRunInfo(file);
579     //
580     // open "Run" tree in level2 file, if not existing return an error (sngl != 0)
581     //
582     sgnl = 0;
583     sgnl = runinfo->Update(run, "ORB", OrbitalInfoversion);
584     //sgnl = runinfo->Read(run);
585    
586     if ( sgnl ){
587     //printf("OrbitalInfo - ERROR: RunInfo exited with non-zero status\n");
588     code = sgnl;
589     goto closeandexit;
590     } else {
591     sgnl = 0;
592     };
593     //
594     // number of events in the file BEFORE the first event of our run
595     //
596     nobefrun = runinfo->GetFirstEntry();
597     //
598     // total number of events in the file
599     //
600     totfileentries = runinfo->GetFileEntries();
601     //
602     // first file entry AFTER the last event of our run
603     //
604     noaftrun = runinfo->GetLastEntry() + 1;
605     //
606     // number of run to be processed
607     //
608     numbofrun = runinfo->GetNoRun();
609 mocchiut 1.40 totnorun = runinfo->GetRunEntries();
610 mocchiut 1.1 //
611     // Try to access the OrbitalInfo tree in the file, if it exists we are reprocessing data if not we are processing a new run
612     //
613     OrbitalInfotrclone = (TTree*)file->Get("OrbitalInfo");
614     //
615     if ( !OrbitalInfotrclone ){
616     //
617     // tree does not exist, we are not reprocessing
618     //
619     reproc = false;
620 mocchiut 1.5 if ( run == 0 ){
621 mocchiut 1.1 if (verbose) printf(" OrbitalInfo - WARNING: you are reprocessing data but OrbitalInfo tree does not exist!\n");
622     }
623 mocchiut 1.5 if ( runinfo->IsReprocessing() && run != 0 ) {
624 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");
625     }
626     } else {
627     //
628     // tree exists, we are reprocessing data. Are we reprocessing a single run or all the file?
629     //
630 mocchiut 1.6 OrbitalInfotrclone->SetAutoSave(900000000000000LL);
631 mocchiut 1.1 reproc = true;
632     //
633     //
634     if (verbose) printf("\n Preparing the pre-processing...\n");
635     //
636 mocchiut 1.22 if ( run == 0 || totnorun == 1 ){
637 mocchiut 1.1 //
638     // we are reprocessing all the file
639     // 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
640     //
641     reprocall = true;
642     //
643 mocchiut 1.57 if (verbose) printf("\n OrbitalInfo - WARNING: Reprocessing all runs\n Deleting old tree...\n");
644 mocchiut 1.1 //
645     } else {
646     //
647     // 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
648     //
649     reprocall = false;
650     //
651 mocchiut 1.5 if (verbose) printf("\n OrbitalInfo - WARNING: Reprocessing run number %u \n",run);
652 mocchiut 1.1 //
653     // copying old tree to a new file
654     //
655 mocchiut 1.23 gSystem->MakeDirectory(OrbitalInfofolder.str().c_str());
656     myfold = true;
657 mocchiut 1.1 tempfile = new TFile(tempname.str().c_str(),"RECREATE");
658     tempOrbitalInfo = OrbitalInfotrclone->CloneTree(-1,"fast");
659     tempOrbitalInfo->SetName("OrbitalInfo-old");
660     tempfile->Write();
661 mocchiut 1.57 tempOrbitalInfo->Delete();
662 mocchiut 1.1 tempfile->Close();
663     }
664     //
665     // Delete the old tree from old file and memory
666     //
667 mocchiut 1.57 OrbitalInfotrclone->Clear();
668 mocchiut 1.1 OrbitalInfotrclone->Delete("all");
669     //
670     if (verbose) printf(" ...done!\n");
671     //
672     };
673     //
674     // create mydetector tree mydect
675     //
676     file->cd();
677     OrbitalInfotr = new TTree("OrbitalInfo-new","PAMELA OrbitalInfo data");
678 mocchiut 1.6 OrbitalInfotr->SetAutoSave(900000000000000LL);
679 mocchiut 1.30 orbitalinfo->Set();//ELENA **TEMPORANEO?**
680 mocchiut 1.1 OrbitalInfotr->Branch("OrbitalInfo","OrbitalInfo",&orbitalinfo);
681     //
682 mocchiut 1.73 // create new branches for new tracking algorithms
683     //
684     if ( hasNucleiTrk ){
685     torbNucleiTrk = new TClonesArray("OrbitalInfoTrkVar",1);
686     OrbitalInfotr->Branch("TrackNuclei",&torbNucleiTrk);
687     }
688     if ( hasExtNucleiTrk ){
689     torbExtNucleiTrk = new TClonesArray("OrbitalInfoTrkVar",1);
690     OrbitalInfotr->Branch("RecoveredTrackNuclei",&torbExtNucleiTrk);
691     }
692     if ( hasExtTrk ){
693     torbExtTrk = new TClonesArray("OrbitalInfoTrkVar",1);
694     OrbitalInfotr->Branch("RecoveredTrack",&torbExtTrk);
695     }
696    
697     //
698 mocchiut 1.1 if ( reproc && !reprocall ){
699     //
700     // open new file and retrieve also tree informations
701     //
702     tempfile = new TFile(tempname.str().c_str(),"READ");
703     OrbitalInfotrclone = (TTree*)tempfile->Get("OrbitalInfo-old");
704 mocchiut 1.6 OrbitalInfotrclone->SetAutoSave(900000000000000LL);
705 mocchiut 1.1 OrbitalInfotrclone->SetBranchAddress("OrbitalInfo",&orbitalinfoclone);
706     //
707     if ( nobefrun > 0 ){
708     if (verbose){
709 mocchiut 1.7 printf("\n Pre-processing: copying events from the old tree before the processed run\n");
710     printf(" Copying %u events in the file which are before the beginning of the run %u \n",nobefrun,run);
711     printf(" Start copying at event number 0, end copying at event number %u \n",nobefrun);
712 mocchiut 1.1 }
713     for (UInt_t j = 0; j < nobefrun; j++){
714     //
715 mocchiut 1.43 if ( OrbitalInfotrclone->GetEntry(j) <= 0 ) throw -36;
716 mocchiut 1.1 //
717     // copy orbitalinfoclone to mydec
718     //
719 mocchiut 1.76 // orbitalinfo->Clear();
720 mocchiut 1.5 //
721 mocchiut 1.1 memcpy(&orbitalinfo,&orbitalinfoclone,sizeof(orbitalinfoclone));
722     //
723     // Fill entry in the new tree
724     //
725     OrbitalInfotr->Fill();
726     //
727     };
728     if (verbose) printf(" Finished successful copying!\n");
729 mocchiut 1.57 };
730 mocchiut 1.1 };
731     //
732 mocchiut 1.44 //
733 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.
734     //
735     runlist = runinfo->GetRunList();
736 mocchiut 1.81 if ( debug ){
737     printf("BEFORE LOOP ON RUN\n");
738     gObjectTable->Print();
739     }
740 mocchiut 1.1 //
741     // Loop over the run to be processed
742     //
743 mocchiut 1.81 for (UInt_t irun=0; irun < numbofrun; irun++){ //===>
744 mocchiut 1.57
745     L_QQ_Q_l_lower = new Quaternions();
746     RYPang_lower = new InclinationInfo();
747     L_QQ_Q_l_upper = new Quaternions();
748     RYPang_upper = new InclinationInfo();
749    
750 mocchiut 1.1 //
751     // retrieve the first run ID to be processed using the RunInfo list
752     //
753 mocchiut 1.15
754 mocchiut 1.1 idRun = runlist->At(irun);
755     if (verbose){
756     printf("\n\n\n ####################################################################### \n");
757     printf(" PROCESSING RUN NUMBER %i \n",(int)idRun);
758     printf(" ####################################################################### \n\n\n");
759     }
760     //
761 mocchiut 1.5 runinfo->ID_ROOT_L0 = 0;
762 mocchiut 1.1 //
763     // store in the runinfo class the GL_RUN variables for our run
764     //
765     sgnl = 0;
766     sgnl = runinfo->GetRunInfo(idRun);
767     if ( sgnl ){
768 mocchiut 1.5 if ( debug ) printf("\n OrbitalInfo - ERROR: RunInfo exited with non-zero status\n");
769 mocchiut 1.1 code = sgnl;
770     goto closeandexit;
771     } else {
772     sgnl = 0;
773     };
774     //
775     // now you can access that variables using the RunInfo class this way runinfo->ID_REG_RUN
776     //
777 mocchiut 1.5 if ( runinfo->ID_ROOT_L0 == 0 ){
778     if ( debug ) printf("\n OrbitalInfo - ERROR: no run with ID_RUN = %u \n\n Exiting... \n\n",idRun);
779 mocchiut 1.1 code = -5;
780     goto closeandexit;
781     };
782     //
783 mocchiut 1.5 // prepare the timesync for the db
784     //
785     dbtime = new GL_TIMESYNC(runinfo->ID_ROOT_L0,"ID",dbc);
786 mocchiut 1.15
787 mocchiut 1.5 //
788 mocchiut 1.1 // Search in the DB the path and name of the LEVEL0 file to be processed.
789     //
790 mocchiut 1.5 glroot->Query_GL_ROOT(runinfo->ID_ROOT_L0,dbc);
791 mocchiut 1.1 //
792     ftmpname.str("");
793     ftmpname << glroot->PATH.Data() << "/";
794     ftmpname << glroot->NAME.Data();
795     fname = ftmpname.str().c_str();
796 mocchiut 1.15 ftmpname.str("");
797 mocchiut 1.1 //
798 mocchiut 1.40 // print nout informations
799 mocchiut 1.1 //
800 mocchiut 1.5 totevent = runinfo->NEVENTS;
801 mocchiut 1.30 evfrom = runinfo->EV_FROM;
802 mocchiut 1.15 //cout<<"totevents = "<<totevent<<"\n";
803 mocchiut 1.1 if (verbose){
804     printf("\n LEVEL0 data file: %s \n",fname.Data());
805 mocchiut 1.5 printf(" RUN HEADER absolute time is: %u \n",runinfo->RUNHEADER_TIME);
806     printf(" RUN TRAILER absolute time is: %u \n",runinfo->RUNTRAILER_TIME);
807 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);
808 mocchiut 1.1 }//
809 mocchiut 1.27 //
810 mocchiut 1.28 // if ( !totevent ) goto closeandexit;
811 mocchiut 1.1 // Open Level0 file
812 mocchiut 1.59 if ( l0File ) l0File->Close();
813 mocchiut 1.1 l0File = new TFile(fname.Data());
814     if ( !l0File ) {
815 mocchiut 1.5 if ( debug ) printf(" OrbitalInfo - ERROR: problems opening Level0 file\n");
816 mocchiut 1.1 code = -6;
817     goto closeandexit;
818     };
819     l0tr = (TTree*)l0File->Get("Physics");
820     if ( !l0tr ) {
821 mocchiut 1.5 if ( debug ) printf(" OrbitalInfo - ERROR: no Physics tree in Level0 file\n");
822 mocchiut 1.1 l0File->Close();
823     code = -7;
824     goto closeandexit;
825     };
826 mocchiut 1.2 // EM: open header branch as well
827     l0head = l0tr->GetBranch("Header");
828     if ( !l0head ) {
829 mocchiut 1.5 if ( debug ) printf(" OrbitalInfo - ERROR: no Header branch in Level0 tree\n");
830 mocchiut 1.2 l0File->Close();
831     code = -8;
832     goto closeandexit;
833     };
834     l0tr->SetBranchAddress("Header", &eh);
835     // end EM
836 mocchiut 1.5 nevents = l0head->GetEntries();
837 mocchiut 1.1 //
838 mocchiut 1.28 if ( nevents < 1 && totevent ) {
839 mocchiut 1.5 if ( debug ) printf(" OrbitalInfo - ERROR: Level0 file is empty\n\n");
840 mocchiut 1.1 l0File->Close();
841     code = -11;
842     goto closeandexit;
843     };
844 mocchiut 1.20 //
845 mocchiut 1.28 if ( runinfo->EV_TO > nevents-1 && totevent ) {
846 mocchiut 1.5 if ( debug ) printf(" OrbitalInfo - ERROR: too few entries in the registry tree\n");
847 mocchiut 1.1 l0File->Close();
848     code = -12;
849     goto closeandexit;
850     };
851 mocchiut 1.55
852 mocchiut 1.15 ULong_t TimeSync = (ULong_t)dbtime->GetTimesync();
853     ULong_t ObtSync = (ULong_t)(dbtime->GetObt0()/1000);
854     ULong_t DeltaOBT = TimeSync - ObtSync;
855    
856     if ( debug ) printf(" 2 TimeSync %lu ObtSync %lu DeltaOBT %lu\n",(ULong_t)(dbtime->GetTimesync()/1000),(ULong_t)dbtime->GetObt0(),TimeSync-ObtSync);
857 mocchiut 1.37 //
858     // Read MCMDs from up to 11 files, 5 before and 5 after the present one in order to have some kind of inclination information
859     //
860     ch = new TChain("Mcmd","Mcmd");
861     //
862     // look in the DB to find the closest files to this run
863     //
864     TSQLResult *pResult = 0;
865     TSQLRow *Row = 0;
866     stringstream myquery;
867     UInt_t l0fid[10];
868     Int_t i = 0;
869     memset(l0fid,0,10*sizeof(Int_t));
870     //
871     myquery.str("");
872     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;";
873     //
874     pResult = dbc->Query(myquery.str().c_str());
875     //
876     i = 9;
877     if( pResult ){
878     //
879     Row = pResult->Next();
880     //
881     while ( Row ){
882     //
883     // store infos and exit
884     //
885     l0fid[i] = (UInt_t)atoll(Row->GetField(0));
886     i--;
887 mocchiut 1.81 if (Row){ // memleak!
888     delete Row;
889     Row = 0;
890     }
891 mocchiut 1.37 Row = pResult->Next();
892     //
893 mocchiut 1.81 }
894     if (Row) delete Row;
895 mocchiut 1.37 pResult->Delete();
896 mocchiut 1.81 }
897 mocchiut 1.37 //
898     myquery.str("");
899     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;";
900     //
901     pResult = dbc->Query(myquery.str().c_str());
902     //
903     i = 0;
904     if( pResult ){
905     //
906     Row = pResult->Next();
907     //
908     while ( Row ){
909     //
910     // store infos and exit
911     //
912     l0fid[i] = (UInt_t)atoll(Row->GetField(0));
913     i++;
914 mocchiut 1.81 if (Row){ // memleak!
915     delete Row;
916     Row = 0;
917     }
918 mocchiut 1.37 Row = pResult->Next();
919     //
920 mocchiut 1.81 }
921     if (Row) delete Row;
922 mocchiut 1.37 pResult->Delete();
923 mocchiut 1.81 }
924 mocchiut 1.37 //
925     i = 0;
926     UInt_t previd = 0;
927     while ( i < 10 ){
928     if ( l0fid[i] && previd != l0fid[i] ){
929     previd = l0fid[i];
930     myquery.str("");
931     myquery << "select PATH,NAME from GL_ROOT where ID=" << l0fid[i] << " ;";
932     //
933     pResult = dbc->Query(myquery.str().c_str());
934     //
935     if( pResult ){
936     //
937     Row = pResult->Next();
938     //
939     if ( debug ) printf(" Using inclination informations from file: %s \n",(((TString)gSystem->ExpandPathName(Row->GetField(0)))+"/"+(TString)Row->GetField(1)).Data());
940     ch->Add(((TString)gSystem->ExpandPathName(Row->GetField(0)))+"/"+(TString)Row->GetField(1));
941     //
942 mocchiut 1.81 if (Row) delete Row;
943 mocchiut 1.37 pResult->Delete();
944 mocchiut 1.81 }
945     }
946 mocchiut 1.37 i++;
947 mocchiut 1.81 }
948 mocchiut 1.37 //
949     ch->SetBranchAddress("Mcmd",&mcmdev);
950     neventsm = ch->GetEntries();
951     if ( debug ) printf(" entries %u \n", neventsm);
952 mocchiut 1.15 //
953     if (neventsm == 0){
954 mocchiut 1.18 if ( debug ) printf("InclinationInfo - WARNING: No quaternions in this File");
955     code = 900;
956 mocchiut 1.15 }
957     //
958 mocchiut 1.73 Double_t lowerqtime = 0;
959 mocchiut 1.15 //
960 mocchiut 1.44 // init quaternions information from mcmd-packets
961 mocchiut 1.15 //
962     Bool_t isf = true;
963 mocchiut 1.44
964     vector<Float_t> q0;
965     vector<Float_t> q1;
966     vector<Float_t> q2;
967     vector<Float_t> q3;
968     vector<Double_t> qtime;
969     vector<Float_t> qPitch;
970     vector<Float_t> qRoll;
971     vector<Float_t> qYaw;
972     vector<Int_t> qmode;
973    
974 mocchiut 1.81 q0.reserve(4096);
975     q1.reserve(4096);
976     q2.reserve(4096);
977     q3.reserve(4096);
978     qtime.reserve(4096);
979     qPitch.reserve(4096);
980     qRoll.reserve(4096);
981     qYaw.reserve(4096);
982     qmode.reserve(4096);
983     if ( debug ) printf(" q0 capa %i \n",(int)q0.capacity());
984 mocchiut 1.44 Int_t nt = 0;
985     UInt_t must = 0;
986    
987 mocchiut 1.81 Int_t currentYear = 0;
988     Int_t previousYear = 0;
989    
990 mocchiut 1.15 //
991 mocchiut 1.1 // run over all the events of the run
992     //
993     if (verbose) printf("\n Ready to start! \n\n Processed events: \n\n");
994 mocchiut 1.81 if ( debug ){
995     printf("BEFORE LOOP ON EVENTS\n");
996     gObjectTable->Print();
997     }
998 mocchiut 1.1 //
999 mocchiut 1.44 //
1000 mocchiut 1.5 for ( re = runinfo->EV_FROM; re < (runinfo->EV_FROM+runinfo->NEVENTS); re++){
1001 mocchiut 1.81 //for ( re = runinfo->EV_FROM; re < (runinfo->EV_FROM+10); re++){
1002    
1003 mocchiut 1.1 //
1004 mocchiut 1.5 if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000);
1005 mocchiut 1.15 if ( debug ) printf(" %i \n",procev);
1006 mocchiut 1.1 //
1007 mocchiut 1.43 if ( l0head->GetEntry(re) <= 0 ) throw -36;
1008 mocchiut 1.1 //
1009     // absolute time of this event
1010     //
1011 mocchiut 1.5 ph = eh->GetPscuHeader();
1012 mocchiut 1.15 atime = dbtime->DBabsTime(ph->GetOrbitalTime());
1013 mocchiut 1.40 if ( debug ) printf(" %i absolute time \n",procev);
1014 mocchiut 1.1 //
1015     // paranoid check
1016     //
1017 mocchiut 1.34 if ( (atime > (runinfo->RUNTRAILER_TIME+1)) || (atime < (runinfo->RUNHEADER_TIME-1)) ) {
1018 mocchiut 1.1 if (verbose) printf(" OrbitalInfo - WARNING: event at time outside the run time window, skipping it\n");
1019 mocchiut 1.30 jumped++;
1020 mocchiut 1.44 // debug = true;
1021 mocchiut 1.7 continue;
1022     }
1023 mocchiut 1.30
1024 mocchiut 1.81 // just for testing:
1025     // if (re >= 5+runinfo->EV_FROM) atime += anni5;
1026     // if (re >= 7+runinfo->EV_FROM) atime += anni5;
1027     // if (re >= 9+runinfo->EV_FROM) atime += anni5;
1028    
1029     //
1030     // open IGRF files and do it only once if we are processing a full level2 file
1031     //
1032     Float_t kkyear;
1033     UInt_t kyear, kmonth, kday, khour, kmin, ksec;
1034     //
1035     TTimeStamp kt = TTimeStamp(atime, kTRUE);
1036     kt.GetDate(kTRUE, 0, &kyear, &kmonth, &kday);
1037     kt.GetTime(kTRUE, 0, &khour, &kmin, &ksec);
1038     kkyear = (float) kyear
1039     + (kmonth*31.+ (float) kday)/365.
1040     + (khour*3600.+kmin*60.+(float)ksec)/(24.*3600.*365.);
1041     currentYear = int(kkyear/5.) * 5;
1042     if ( debug ) printf(" prevy %i curry %i igrfloaded %i \n",previousYear,currentYear,igrfloaded);
1043     if ( currentYear != previousYear ) igrfloaded = false;
1044     previousYear = currentYear;
1045     if ( debug ) printf(" prevy %i curry %i igrfloaded %i \n",previousYear,currentYear,igrfloaded);
1046     //
1047     if ( !igrfloaded ){
1048    
1049     igrfloaded = true;
1050    
1051     parerror=glparam->Query_GL_PARAM(atime,302,dbc); // parameters stored in DB in GL_PRAM table
1052     if ( parerror<0 ) {
1053     code = parerror;
1054     goto closeandexit;
1055     }
1056     ltp1 = (Int_t)(glparam->PATH+glparam->NAME).Length();
1057     if ( verbose ) printf(" Reading Earth's Magnetic Field parameter file: %s \n",(glparam->PATH+glparam->NAME).Data());
1058     //
1059     if ( glparam->NAME.EndsWith("s.txt") || glparam->NAME.EndsWith("s.dat") ){
1060     if ( verbose ) printf("ERROR: Current date is beyond the latest secular variation file time span. Please update IGRF files to process data\n");
1061     throw -906;
1062     }
1063     //
1064     int isSecular = false;
1065     //
1066     parerror=glparam2->Query_GL_PARAM(atime+anni5,302,dbc); // parameters stored in DB in GL_PRAM table
1067     if ( parerror<0 ) {
1068     code = parerror;
1069     goto closeandexit;
1070     }
1071     ltp2 = (Int_t)(glparam2->PATH+glparam2->NAME).Length();
1072     if ( verbose ) printf(" Reading Earth's Magnetic Field parameter file: %s \n",(glparam2->PATH+glparam2->NAME).Data());
1073     if ( glparam2->NAME.EndsWith("s.txt") || glparam2->NAME.EndsWith("s.dat") ){
1074     isSecular = true;
1075     if ( verbose ) printf(" Using secular variation file and hence fortran subroutine 'extrapolation'\n");
1076     } else {
1077     if ( verbose ) printf(" Using two field measurement files and hence fortran subroutine 'interpolation'\n");
1078     }
1079     //
1080     initize_(&isSecular,(char *)(glparam->PATH+glparam->NAME).Data(),&ltp1,(char *)(glparam2->PATH+glparam2->NAME).Data(),&ltp2);
1081     //
1082     if (debug) cout<<"initize: "<<(char *)(glparam->PATH+glparam->NAME).Data()<<"\t"<<(char *)(glparam2->PATH+glparam2->NAME).Data()<<"\t isSecular? "<<isSecular<<endl;
1083    
1084     // GM_ScanIGRF(dbc, &G0, &G1, &H1);
1085     TString igrfFile1 = glparam->PATH+glparam->NAME;
1086     TString igrfFile2 = glparam2->PATH+glparam2->NAME;
1087     GM_SetIGRF(isSecular,igrfFile1,igrfFile2, &G0, &G1, &H1);
1088     }
1089     //
1090     // End IGRF stuff//
1091     //
1092    
1093 mocchiut 1.30 //
1094     // retrieve tof informations
1095     //
1096     if ( !reprocall ){
1097     itr = nobefrun + (re - evfrom - jumped);
1098     //itr = re-(46438+200241);
1099     } else {
1100     itr = runinfo->GetFirstEntry() + (re - evfrom - jumped);
1101     };
1102     //
1103     if ( !standalone ){
1104     if ( itr > nevtofl2 ){
1105     if ( verbose ) printf(" OrbitalInfo - ERROR: no tof events with entry = %i in Level2 file\n",itr);
1106     if ( debug ) printf(" nobefrun %u re %u evfrom %u jumped %u reprocall %i \n",nobefrun,re,evfrom,jumped,reprocall);
1107     l0File->Close();
1108 pam-mep 1.69 code = -904;
1109 mocchiut 1.30 goto closeandexit;
1110     };
1111     //
1112     tof->Clear();
1113     //
1114 mocchiut 1.73 // Clones array must be cleared before going on
1115     //
1116     if ( hasNucleiTof ){
1117     tcNucleiTof->Delete();
1118     }
1119     if ( hasExtNucleiTof ){
1120     tcExtNucleiTof->Delete();
1121     }
1122     if ( hasExtTof ){
1123     tcExtTof->Delete();
1124     }
1125     //
1126     if ( verbose ) printf(" get tof tree entries... entry = %i in Level2 file\n",itr);
1127 emocchiutti 1.67 if ( ttof->GetEntry(itr) <= 0 ){
1128 mocchiut 1.73 if ( verbose ) printf(" problems with tof tree entries... entry = %i in Level2 file\n",itr);
1129     if ( verbose ) printf(" nobefrun %u re %u evfrom %u jumped %u reprocall %i \n",nobefrun,re,evfrom,jumped,reprocall);
1130     throw -36;
1131 emocchiutti 1.67 }
1132 mocchiut 1.73 if ( verbose ) printf(" gat0\n");
1133 mocchiut 1.30 //
1134 pam-mep 1.69 }
1135     //
1136     // retrieve tracker informations
1137     //
1138     if ( !standalone ){
1139     if ( itr > nevtrkl2 ){
1140     if ( verbose ) printf(" OrbitalInfo - ERROR: no trk events with entry = %i in Level2 file\n",itr);
1141     if ( debug ) printf(" nobefrun %u re %u evfrom %u jumped %u reprocall %i \n",nobefrun,re,evfrom,jumped,reprocall);
1142     l0File->Close();
1143     code = -905;
1144     goto closeandexit;
1145 mocchiut 1.73 }
1146 pam-mep 1.69 //
1147 mocchiut 1.73 if ( verbose ) printf(" gat1\n");
1148 pam-mep 1.69 trke->Clear();
1149     //
1150 mocchiut 1.73 // Clones array must be cleared before going on
1151     //
1152     if ( hasNucleiTrk ){
1153     if ( verbose ) printf(" gat2\n");
1154     tcNucleiTrk->Delete();
1155     if ( verbose ) printf(" gat3\n");
1156     torbNucleiTrk->Delete();
1157     }
1158     if ( hasExtNucleiTrk ){
1159     if ( verbose ) printf(" gat4\n");
1160     tcExtNucleiTrk->Delete();
1161     if ( verbose ) printf(" gat5\n");
1162     torbExtNucleiTrk->Delete();
1163     }
1164     if ( hasExtTrk ){
1165     if ( verbose ) printf(" gat6\n");
1166     tcExtTrk->Delete();
1167     if ( verbose ) printf(" gat7\n");
1168     torbExtTrk->Delete();
1169     }
1170     //
1171     if ( verbose ) printf(" get trk tree entries... entry = %i in Level2 file\n",itr);
1172 pam-mep 1.69 if ( ttrke->GetEntry(itr) <= 0 ) throw -36;
1173     //
1174     }
1175    
1176 mocchiut 1.1 //
1177     procev++;
1178     //
1179     // start processing
1180     //
1181 mocchiut 1.40 if ( debug ) printf(" %i start processing \n",procev);
1182 mocchiut 1.3 orbitalinfo->Clear();
1183 pamela 1.92
1184 mocchiut 1.5 //
1185 mocchiut 1.30 OrbitalInfoTrkVar *t_orb = new OrbitalInfoTrkVar();
1186     if( !(orbitalinfo->OrbitalInfoTrk) ) orbitalinfo->OrbitalInfoTrk = new TClonesArray("OrbitalInfoTrkVar",2);
1187     TClonesArray &tor = *orbitalinfo->OrbitalInfoTrk;
1188 pam-mep 1.69
1189     // Geomagnetic coordinates calculation variables
1190     GMtype_CoordSpherical CoordSpherical, DipoleSpherical;
1191     GMtype_CoordCartesian CoordCartesian, DipoleCartesian;
1192     GMtype_Model Model;
1193     GMtype_Pole Pole;
1194    
1195 mocchiut 1.30 //
1196 mocchiut 1.15 // Fill OBT, pkt_num and absTime
1197     //
1198 mocchiut 1.2 orbitalinfo->pkt_num = ph->GetCounter();
1199     orbitalinfo->OBT = ph->GetOrbitalTime();
1200 mocchiut 1.15 orbitalinfo->absTime = atime;
1201 mocchiut 1.40 if ( debug ) printf(" %i pktnum obt abstime \n",procev);
1202 mocchiut 1.15 //
1203     // Propagate the orbit from the tle time to atime, using SGP(D)4.
1204     //
1205 mocchiut 1.40 if ( debug ) printf(" %i sgp4 \n",procev);
1206 mocchiut 1.15 cCoordGeo coo;
1207 pamela 1.92 Float_t jyear=0.;
1208 mocchiut 1.7 //
1209 mocchiut 1.84 if(atime >= gltle->GetToTime() || atime < gltle->GetFromTime() ) { // AGH! bug when reprocessing??
1210 pamela 1.92
1211 mocchiut 1.9 if ( !gltle->Query(atime, dbc) ){
1212 mocchiut 1.15 //
1213 mocchiut 1.9 // Compute the magnetic dipole moment.
1214 mocchiut 1.15 //
1215 mocchiut 1.40 if ( debug ) printf(" %i compute magnetic dipole moment \n",procev);
1216 mocchiut 1.9 UInt_t year, month, day, hour, min, sec;
1217 mocchiut 1.15 //
1218 mocchiut 1.9 TTimeStamp t = TTimeStamp(atime, kTRUE);
1219     t.GetDate(kTRUE, 0, &year, &month, &day);
1220     t.GetTime(kTRUE, 0, &hour, &min, &sec);
1221     jyear = (float) year
1222     + (month*31.+ (float) day)/365.
1223 mocchiut 1.44 + (hour*3600.+min*60.+(float)sec)/(24.*3600.*365.);
1224 mocchiut 1.15 //
1225 pam-mep 1.69 if ( debug ) printf(" %i compute magnetic dipole moment get dipole moment for year\n",procev);
1226 mocchiut 1.70 if ( debug ) printf(" %i jyear %f dimo %f \n",procev,jyear,dimo);
1227 mocchiut 1.9 feldcof_(&jyear, &dimo); // get dipole moment for year
1228 pam-mep 1.69 if ( debug ) printf(" %i compute magnetic dipole moment end\n",procev);
1229    
1230 mocchiut 1.81 // GM_TimeAdjustCoefs(year, jyear, G0, G1, H1, &Model);
1231     GM_TimeAdjustCoefs(GM_STARTYEAR, (jyear-currentYear+GM_STARTYEAR), G0, G1, H1, &Model); // EM: input this way due to the new way of storing data into Gn,H1 and to avoid changing GM_Time...
1232 pam-mep 1.69 GM_PoleLocation(Model, &Pole);
1233    
1234 mocchiut 1.9 } else {
1235     code = -56;
1236     goto closeandexit;
1237     };
1238 mocchiut 1.7 }
1239 mocchiut 1.15 coo = getCoo(atime, gltle->GetFromTime(), gltle->GetTle());
1240     //
1241     cOrbit orbits(*gltle->GetTle());
1242     //
1243     // synchronize with quaternions data
1244     //
1245 mocchiut 1.18 if ( isf && neventsm>0 ){
1246 mocchiut 1.15 //
1247     // First event
1248     //
1249     isf = false;
1250 mocchiut 1.64 // upperqtime = atime;
1251 mocchiut 1.15 lowerqtime = runinfo->RUNHEADER_TIME;
1252 mocchiut 1.44 for ( ik = 0; ik < neventsm; ik++){ //number of macrocommad packets
1253 mocchiut 1.43 if ( ch->GetEntry(ik) <= 0 ) throw -36;
1254 mocchiut 1.15 tmpSize = mcmdev->Records->GetEntries();
1255 mocchiut 1.64 // numrec = tmpSize;
1256 mocchiut 1.70 if ( debug ) cout << "packet number " << ik <<"\tnumber of subpackets is " << tmpSize << endl;
1257 mocchiut 1.44 for (Int_t j3 = 0;j3<tmpSize;j3++){ //number of subpackets
1258 mocchiut 1.15 mcmdrc = (pamela::McmdRecord*)mcmdev->Records->At(j3);
1259 mocchiut 1.38 if ( mcmdrc ){ // missing inclination bug [8RED 090116]
1260 mocchiut 1.70 if ( debug ) printf(" pluto \n");
1261 mocchiut 1.44 if ((int)mcmdrc->ID1 == 226 && mcmdrc->Mcmd_Block_crc_ok == 1){ //Check that it is Inclination Packet
1262 pam-mep 1.69 L_QQ_Q_l_upper->fill(mcmdrc->McmdData);
1263 mocchiut 1.38 for (UInt_t ui = 0; ui < 6; ui++){
1264     if (ui>0){
1265     if (L_QQ_Q_l_upper->time[ui]>L_QQ_Q_l_upper->time[0]){
1266 mocchiut 1.44 Double_t u_time = dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[ui]*1000-DeltaOBT*1000));
1267     Int_t recSize = recqtime.size();
1268 pam-mep 1.54 if(lowerqtime > recqtime[recSize-1]){
1269 pam-mep 1.69 // to avoid interpolation between bad quaternions arrays
1270     if(sqrt(pow(L_QQ_Q_l_upper->quat[ui][0],2)+pow(L_QQ_Q_l_upper->quat[ui][1],2)+pow(L_QQ_Q_l_upper->quat[ui][2],2)+pow(L_QQ_Q_l_upper->quat[ui][3],2))>0.99999){
1271     Int_t sizeqmcmd = qtime.size();
1272     inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
1273     qtime[sizeqmcmd]=u_time;
1274     q0[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][0];
1275     q1[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][1];
1276     q2[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][2];
1277     q3[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][3];
1278     qmode[sizeqmcmd]=holeq(lowerqtime,qtime[sizeqmcmd],L_QQ_Q_l_lower,L_QQ_Q_l_upper,ui);
1279     lowerqtime = u_time;
1280     orbits.getPosition((double) (u_time - gltle->GetFromTime())/60., &eCi);
1281     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]);
1282     qRoll[sizeqmcmd]=RYPang_upper->Kren;
1283     qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
1284     qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
1285     }
1286 pam-mep 1.54 }
1287 mocchiut 1.44 for(Int_t mu = nt;mu<recSize;mu++){
1288     if(recqtime[mu]>lowerqtime && recqtime[mu]<u_time){
1289 pam-mep 1.69 if(sqrt(pow(recq0[mu],2)+pow(recq1[mu],2)+pow(recq2[mu],2)+pow(recq3[mu],2))>0.99999){
1290     nt=mu;
1291     Int_t sizeqmcmd = qtime.size();
1292     inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
1293     qtime[sizeqmcmd]=recqtime[mu];
1294     q0[sizeqmcmd]=recq0[mu];
1295     q1[sizeqmcmd]=recq1[mu];
1296     q2[sizeqmcmd]=recq2[mu];
1297     q3[sizeqmcmd]=recq3[mu];
1298     qmode[sizeqmcmd]=-10;
1299     orbits.getPosition((double) (qtime[sizeqmcmd] - gltle->GetFromTime())/60., &eCi);
1300     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]);
1301     qRoll[sizeqmcmd]=RYPang_upper->Kren;
1302     qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
1303     qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
1304     }
1305 mocchiut 1.44 }
1306     if(recqtime[mu]>=u_time){
1307 pam-mep 1.69 if(sqrt(pow(L_QQ_Q_l_upper->quat[ui][0],2)+pow(L_QQ_Q_l_upper->quat[ui][1],2)+pow(L_QQ_Q_l_upper->quat[ui][2],2)+pow(L_QQ_Q_l_upper->quat[ui][3],2))>0.99999){
1308     Int_t sizeqmcmd = qtime.size();
1309     inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
1310     qtime[sizeqmcmd]=u_time;
1311     q0[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][0];
1312     q1[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][1];
1313     q2[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][2];
1314     q3[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][3];
1315     qmode[sizeqmcmd]=holeq(lowerqtime,qtime[sizeqmcmd],L_QQ_Q_l_lower,L_QQ_Q_l_upper,ui);
1316     lowerqtime = u_time;
1317     orbits.getPosition((double) (u_time - gltle->GetFromTime())/60., &eCi);
1318     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]);
1319     qRoll[sizeqmcmd]=RYPang_upper->Kren;
1320     qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
1321     qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
1322     break;
1323     }
1324 mocchiut 1.38 }
1325     }
1326     }
1327     }else{
1328 pamela 1.92 // if ( debug ) printf(" here2 %i \n",ui);
1329 mocchiut 1.44 Double_t u_time = dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000));
1330     if(lowerqtime>u_time)nt=0;
1331     Int_t recSize = recqtime.size();
1332 pam-mep 1.54 if(lowerqtime > recqtime[recSize-1]){
1333 pam-mep 1.69 if(sqrt(pow(L_QQ_Q_l_upper->quat[ui][0],2)+pow(L_QQ_Q_l_upper->quat[ui][1],2)+pow(L_QQ_Q_l_upper->quat[ui][2],2)+pow(L_QQ_Q_l_upper->quat[ui][3],2))>0.99999){
1334     Int_t sizeqmcmd = qtime.size();
1335     inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
1336     qtime[sizeqmcmd]=u_time;
1337     q0[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][0];
1338     q1[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][1];
1339     q2[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][2];
1340     q3[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][3];
1341     qmode[sizeqmcmd]=holeq(lowerqtime,qtime[sizeqmcmd],L_QQ_Q_l_lower,L_QQ_Q_l_upper,ui);
1342     lowerqtime = u_time;
1343     orbits.getPosition((double) (u_time - gltle->GetFromTime())/60., &eCi);
1344     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]);
1345     qRoll[sizeqmcmd]=RYPang_upper->Kren;
1346     qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
1347     qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
1348     }
1349 pam-mep 1.54 }
1350 mocchiut 1.44 for(Int_t mu = nt;mu<recSize;mu++){
1351     if(recqtime[mu]>lowerqtime && recqtime[mu]<u_time){
1352 pam-mep 1.69 if(sqrt(pow(recq0[mu],2)+pow(recq1[mu],2)+pow(recq2[mu],2)+pow(recq3[mu],2))>0.99999){
1353 malakhov 1.82 // nt=mu;
1354 pam-mep 1.69 Int_t sizeqmcmd = qtime.size();
1355     inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
1356     qtime[sizeqmcmd]=recqtime[mu];
1357     q0[sizeqmcmd]=recq0[mu];
1358     q1[sizeqmcmd]=recq1[mu];
1359     q2[sizeqmcmd]=recq2[mu];
1360     q3[sizeqmcmd]=recq3[mu];
1361     qmode[sizeqmcmd]=-10;
1362     orbits.getPosition((double) (qtime[sizeqmcmd] - gltle->GetFromTime())/60., &eCi);
1363     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]);
1364     qRoll[sizeqmcmd]=RYPang_upper->Kren;
1365     qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
1366     qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
1367     }
1368 mocchiut 1.44 }
1369     if(recqtime[mu]>=u_time){
1370 pam-mep 1.69 if(sqrt(pow(L_QQ_Q_l_upper->quat[0][0],2)+pow(L_QQ_Q_l_upper->quat[0][1],2)+pow(L_QQ_Q_l_upper->quat[0][2],2)+pow(L_QQ_Q_l_upper->quat[0][3],2))>0.99999){
1371     Int_t sizeqmcmd = qtime.size();
1372     inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
1373     qtime[sizeqmcmd]=u_time;
1374     q0[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][0];
1375     q1[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][1];
1376     q2[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][2];
1377     q3[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][3];
1378     qmode[sizeqmcmd]=holeq(lowerqtime,qtime[sizeqmcmd],L_QQ_Q_l_lower,L_QQ_Q_l_upper,ui);
1379     lowerqtime = u_time;
1380     orbits.getPosition((double) (u_time - gltle->GetFromTime())/60., &eCi);
1381     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]);
1382     qRoll[sizeqmcmd]=RYPang_upper->Kren;
1383     qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
1384     qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
1385     CopyQ(L_QQ_Q_l_lower,L_QQ_Q_l_upper);
1386     break;
1387     }
1388 mocchiut 1.15 }
1389     }
1390 mocchiut 1.44 }
1391 mocchiut 1.38 }
1392 mocchiut 1.15 }
1393 mocchiut 1.44 }
1394 mocchiut 1.15 }
1395 mocchiut 1.44 }
1396 mocchiut 1.47
1397 pam-mep 1.69 if(qtime.size()==0){ // in case if no orientation information in data
1398 mocchiut 1.70 if ( debug ) cout << "qtime.size() = 0" << endl;
1399 mocchiut 1.65 for(UInt_t my=0;my<recqtime.size();my++){
1400 mocchiut 1.70 if(sqrt(pow(recq0[my],2)+pow(recq1[my],2)+pow(recq2[my],2)+pow(recq3[my],2))>0.99999){
1401     Int_t sizeqmcmd = qtime.size();
1402     inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
1403     qtime[sizeqmcmd]=recqtime[my];
1404     q0[sizeqmcmd]=recq0[my];
1405     q1[sizeqmcmd]=recq1[my];
1406     q2[sizeqmcmd]=recq2[my];
1407     q3[sizeqmcmd]=recq3[my];
1408     qmode[sizeqmcmd]=-10;
1409     orbits.getPosition((double) (qtime[sizeqmcmd] - gltle->GetFromTime())/60., &eCi);
1410     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]);
1411     qRoll[sizeqmcmd]=RYPang_upper->Kren;
1412     qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
1413     qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
1414     }
1415 mocchiut 1.65 }
1416 mocchiut 1.47 }
1417    
1418 mocchiut 1.49
1419 pamela 1.92 // if ( debug ) printf(" puffi \n");
1420 mocchiut 1.44 Double_t tmin = 9999999999.;
1421     Double_t tmax = 0.;
1422     for(UInt_t tre = 0;tre<qtime.size();tre++){
1423     if(qtime[tre]>tmax)tmax = qtime[tre];
1424     if(qtime[tre]<tmin)tmin = qtime[tre];
1425     }
1426 pam-mep 1.69 // sorting quaternions by time
1427     Bool_t t = true;
1428     while(t){
1429     t=false;
1430     for(UInt_t i=0;i<qtime.size()-1;i++){
1431     if(qtime[i]>qtime[i+1]){
1432     Double_t tmpr = qtime[i];
1433     qtime[i]=qtime[i+1];
1434     qtime[i+1] = tmpr;
1435     tmpr = q0[i];
1436     q0[i]=q0[i+1];
1437     q0[i+1] = tmpr;
1438     tmpr = q1[i];
1439     q1[i]=q1[i+1];
1440     q1[i+1] = tmpr;
1441     tmpr = q2[i];
1442     q2[i]=q2[i+1];
1443     q2[i+1] = tmpr;
1444     tmpr = q3[i];
1445     q3[i]=q3[i+1];
1446     q3[i+1] = tmpr;
1447     tmpr = qRoll[i];
1448     qRoll[i]=qRoll[i+1];
1449     qRoll[i+1] = tmpr;
1450     tmpr = qYaw[i];
1451     qYaw[i]=qYaw[i+1];
1452     qYaw[i+1] = tmpr;
1453     tmpr = qPitch[i];
1454     qPitch[i]=qPitch[i+1];
1455     qPitch[i+1] = tmpr;
1456     t=true;
1457     }
1458     }
1459     }
1460    
1461     if ( debug ){
1462     cout << "we have loaded quaternions: size of quaternions set is "<< qtime.size() << endl;
1463     for(UInt_t i=0;i<qtime.size();i++) cout << qtime[i] << "\t";
1464     cout << endl << endl;
1465     Int_t lopu;
1466     cin >> lopu;
1467     }
1468 mocchiut 1.46
1469 mocchiut 1.44 } // if we processed first event
1470 pam-mep 1.60
1471 mocchiut 1.44
1472     //Filling Inclination information
1473     Double_t incli = 0;
1474 mocchiut 1.46 if ( qtime.size() > 1 ){
1475 pamela 1.92 if(atime<qtime[0]){
1476     for(UInt_t mu = 1;mu<qtime.size()-1;mu++){
1477     if(qtime[mu]>qtime[0]){
1478     incli = (qPitch[mu]-qPitch[0])/(qtime[mu]-qtime[0]);
1479     orbitalinfo->theta = incli*atime+qPitch[mu]-incli*qtime[mu];
1480     incli = (qRoll[mu]-qRoll[0])/(qtime[mu]-qtime[0]);
1481     orbitalinfo->etha = incli*atime+qRoll[mu]-incli*qtime[mu];
1482     incli = (qYaw[mu]-qYaw[0])/(qtime[mu]-qtime[0]);
1483     orbitalinfo->phi = incli*atime+qYaw[mu]-incli*qtime[mu];
1484    
1485     incli = (q0[mu]-q0[0])/(qtime[mu]-qtime[0]);
1486     orbitalinfo->q0 = incli*atime+q0[mu]-incli*qtime[mu];
1487     incli = (q1[mu]-q1[0])/(qtime[mu]-qtime[0]);
1488     orbitalinfo->q1 = incli*atime+q1[mu]-incli*qtime[mu];
1489     incli = (q2[mu]-q2[0])/(qtime[mu]-qtime[0]);
1490     orbitalinfo->q2 = incli*atime+q2[mu]-incli*qtime[mu];
1491     incli = (q3[mu]-q3[0])/(qtime[mu]-qtime[0]);
1492     orbitalinfo->q3 = incli*atime+q3[mu]-incli*qtime[mu];
1493     orbitalinfo->TimeGap=qtime[0]-atime;
1494     break;
1495     }
1496     }
1497     }
1498     Float_t eend=qtime.size()-1;
1499     if(atime>qtime[eend]){
1500     for(UInt_t mu=eend-1;mu>=0;mu--){
1501     if(qtime[mu]<qtime[eend]){
1502     incli = (qPitch[eend]-qPitch[mu])/(qtime[eend]-qtime[mu]);
1503     orbitalinfo->theta = incli*atime+qPitch[eend]-incli*qtime[eend];
1504     incli = (qRoll[eend]-qRoll[mu])/(qtime[eend]-qtime[mu]);
1505     orbitalinfo->etha = incli*atime+qRoll[eend]-incli*qtime[eend];
1506     incli = (qYaw[eend]-qYaw[mu])/(qtime[eend]-qtime[mu]);
1507     orbitalinfo->phi = incli*atime+qYaw[eend]-incli*qtime[eend];
1508    
1509     incli = (q0[eend]-q0[mu])/(qtime[eend]-qtime[mu]);
1510     orbitalinfo->q0 = incli*atime+q0[eend]-incli*qtime[eend];
1511     incli = (q1[eend]-q1[mu])/(qtime[eend]-qtime[mu]);
1512     orbitalinfo->q1 = incli*atime+q1[eend]-incli*qtime[eend];
1513     incli = (q2[eend]-q2[mu])/(qtime[eend]-qtime[mu]);
1514     orbitalinfo->q2 = incli*atime+q2[eend]-incli*qtime[eend];
1515     incli = (q3[eend]-q3[mu])/(qtime[eend]-qtime[mu]);
1516     orbitalinfo->q3 = incli*atime+q3[eend]-incli*qtime[eend];
1517     break;
1518     }
1519     }
1520     }
1521 mocchiut 1.65 for(UInt_t mu = must;mu<qtime.size()-1;mu++){
1522     if ( debug ) printf(" ??grfuffi %i sixe %i must %i \n",mu,qtime.size()-1,must);
1523     if(qtime[mu+1]>qtime[mu]){
1524 mocchiut 1.70 if ( debug ) cout << "qtime[" << mu << "] = " << qtime[mu] << "\tqtime[" << mu+1 << "] = " << qtime[mu+1] << "\tatime = " << atime << endl;
1525 mocchiut 1.65 if(atime<=qtime[mu+1] && atime>=qtime[mu]){
1526 mocchiut 1.70 if ( debug ) cout << "here we have found proper quaternions for interpolation: mu = "<<mu<<endl;
1527 mocchiut 1.65 must = mu;
1528     incli = (qPitch[mu+1]-qPitch[mu])/(qtime[mu+1]-qtime[mu]);
1529     orbitalinfo->theta = incli*atime+qPitch[mu+1]-incli*qtime[mu+1];
1530     incli = (qRoll[mu+1]-qRoll[mu])/(qtime[mu+1]-qtime[mu]);
1531     orbitalinfo->etha = incli*atime+qRoll[mu+1]-incli*qtime[mu+1];
1532     incli = (qYaw[mu+1]-qYaw[mu])/(qtime[mu+1]-qtime[mu]);
1533     orbitalinfo->phi = incli*atime+qYaw[mu+1]-incli*qtime[mu+1];
1534    
1535     incli = (q0[mu+1]-q0[mu])/(qtime[mu+1]-qtime[mu]);
1536     orbitalinfo->q0 = incli*atime+q0[mu+1]-incli*qtime[mu+1];
1537     incli = (q1[mu+1]-q1[mu])/(qtime[mu+1]-qtime[mu]);
1538     orbitalinfo->q1 = incli*atime+q1[mu+1]-incli*qtime[mu+1];
1539     incli = (q2[mu+1]-q2[mu])/(qtime[mu+1]-qtime[mu]);
1540     orbitalinfo->q2 = incli*atime+q2[mu+1]-incli*qtime[mu+1];
1541     incli = (q3[mu+1]-q3[mu])/(qtime[mu+1]-qtime[mu]);
1542     orbitalinfo->q3 = incli*atime+q3[mu+1]-incli*qtime[mu+1];
1543 malakhov 1.74 Float_t tg = (qtime[mu+1]-qtime[mu])/1000.0;
1544 mocchiut 1.70 if(tg>=1) tg=0.00;
1545 pamela 1.92 orbitalinfo->TimeGap = TMath::Min(TMath::Abs(qtime[mu+1]-atime),TMath::Abs(atime-qtime[mu]))+tg;//qtime[mu+1]-qtime[mu];
1546 mocchiut 1.70 orbitalinfo->mode = qmode[mu+1];
1547     //if(atime==qtime[mu] || atime==qtime[mu+1]) orbitalinfo->qkind = 0; else orbitalinfo->qkind=1;
1548 mocchiut 1.65 //if(qmode[mu+1]==-10) orbitalinfo->R10r = true;else orbitalinfo->R10r = false;
1549     if ( debug ) printf(" grfuffi4 %i \n",mu);
1550     break;
1551     }
1552     }
1553     }
1554 mocchiut 1.46 }
1555 mocchiut 1.65 if ( debug ) printf(" grfuffi5 \n");
1556 mocchiut 1.30 //
1557     // ops no inclination information
1558     //
1559 mocchiut 1.65
1560 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 ){
1561 pamela 1.92 if (debug) cout << "Warning: no iclination information "<< endl;
1562 mocchiut 1.30 orbitalinfo->mode = 10;
1563     orbitalinfo->q0 = -1000.;
1564     orbitalinfo->q1 = -1000.;
1565     orbitalinfo->q2 = -1000.;
1566     orbitalinfo->q3 = -1000.;
1567     orbitalinfo->etha = -1000.;
1568     orbitalinfo->phi = -1000.;
1569     orbitalinfo->theta = -1000.;
1570 pam-mep 1.69 orbitalinfo->TimeGap = -1000.;
1571 pamela 1.92 TMatrixD Iij(3,3);
1572     Iij(0,0)=0; Iij(0,1)=0; Iij(0,2)=0;
1573     Iij(1,0)=0; Iij(1,1)=0; Iij(1,2)=0;
1574     Iij(2,0)=0; Iij(2,1)=0; Iij(2,2)=0;
1575     orbitalinfo->Iij.ResizeTo(Iij);
1576     orbitalinfo->Iij = Iij;
1577    
1578 pam-mep 1.69 //orbitalinfo->qkind = -1000;
1579 mocchiut 1.70
1580     // if ( debug ){
1581     // Int_t lopu;
1582     // cin >> lopu;
1583     // }
1584 mocchiut 1.65 if ( debug ) printf(" grfuffi6 \n");
1585     }
1586 mocchiut 1.30 //
1587 mocchiut 1.65 if ( debug ) printf(" filling \n");
1588 mocchiut 1.30 // #########################################################################################################################
1589 mocchiut 1.15 //
1590     // fill orbital positions
1591     //
1592 mocchiut 1.7 // Build coordinates in the right range. We want to convert,
1593     // longitude from (0, 2*pi) to (-180deg, 180deg). Altitude is
1594     // in meters.
1595     lon = (coo.m_Lon > M_PI) ? rad2deg(coo.m_Lon - 2*M_PI) : rad2deg(coo.m_Lon);
1596     lat = rad2deg(coo.m_Lat);
1597     alt = coo.m_Alt;
1598 pam-mep 1.69
1599     cOrbit orbits2(*gltle->GetTle());
1600     orbits2.getPosition((double) (atime - gltle->GetFromTime())/60., &eCi);
1601 mocchiut 1.70 // Float_t x=eCi.getPos().m_x;
1602     // Float_t y=eCi.getPos().m_y;
1603     // Float_t z=eCi.getPos().m_z;
1604    
1605 pam-mep 1.69 TVector3 V(eCi.getVel().m_x,eCi.getVel().m_y,eCi.getVel().m_z);
1606     TVector3 Pos(eCi.getPos().m_x,eCi.getPos().m_y,eCi.getPos().m_z);
1607 mocchiut 1.70
1608 pam-mep 1.69 Float_t dlon=Pos.Phi()*TMath::RadToDeg()-lon;
1609 mocchiut 1.70
1610 pam-mep 1.69 Pos.RotateZ(-dlon*TMath::DegToRad());
1611     V.RotateZ(-dlon*TMath::DegToRad());
1612     Float_t diro;
1613     if(V.Z()>0) diro=1; else diro=-1;
1614 mocchiut 1.70
1615     // 10REDNEW
1616 pam-mep 1.69 Int_t errq=0;
1617 malakhov 1.74 Int_t azim=0;
1618 malakhov 1.82 Int_t qual=0;
1619 malakhov 1.74 Int_t MU=0;
1620 pamela 1.80 for(UInt_t mu = 0;mu<RTtime2.size()-1;mu++){
1621     if(atime<RTstart[mu+1] && atime>=RTstart[mu]){
1622 mocchiut 1.70 errq=RTerrq[mu];
1623     azim=RTazim[mu];
1624 malakhov 1.82 qual=RTqual[mu];
1625 malakhov 1.74 MU=mu;
1626     break;
1627 mocchiut 1.70 }
1628     }
1629     orbitalinfo->errq = errq;
1630     orbitalinfo->azim = azim;
1631 malakhov 1.82 orbitalinfo->rtqual=qual;
1632 mocchiut 1.70 orbitalinfo->qkind = 0;
1633    
1634 mocchiut 1.65 if ( debug ) printf(" coord done \n");
1635 mocchiut 1.7 if( lon<180 && lon>-180 && lat<90 && lat>-90 && alt>0 ){
1636     orbitalinfo->lon = lon;
1637     orbitalinfo->lat = lat;
1638 pam-mep 1.69 orbitalinfo->alt = alt;
1639     orbitalinfo->V = V;
1640    
1641 mocchiut 1.70 // GMtype_CoordGeodetic location;
1642 pam-mep 1.69 location.lambda = lon;
1643     location.phi = lat;
1644     location.HeightAboveEllipsoid = alt;
1645    
1646     GM_GeodeticToSpherical(Ellip, location, &CoordSpherical);
1647     GM_SphericalToCartesian(CoordSpherical, &CoordCartesian);
1648     GM_EarthCartToDipoleCartCD(Pole, CoordCartesian, &DipoleCartesian);
1649     GM_CartesianToSpherical(DipoleCartesian, &DipoleSpherical);
1650     orbitalinfo->londip = DipoleSpherical.lambda;
1651     orbitalinfo->latdip = DipoleSpherical.phig;
1652    
1653     if(debug)cout<<"geodetic:\t"<<lon<<"\t"<<lat<<"\tgeomagnetic:\t"<<orbitalinfo->londip<<"\t"<<orbitalinfo->latdip<<endl;
1654    
1655 mocchiut 1.15 //
1656 mocchiut 1.7 // compute mag field components and L shell.
1657 mocchiut 1.15 //
1658 mocchiut 1.65 if ( debug ) printf(" call igrf feldg \n");
1659 mocchiut 1.7 feldg_(&lat, &lon, &alt, &bnorth, &beast, &bdown, &babs);
1660 mocchiut 1.65 if ( debug ) printf(" call igrf shellg \n");
1661 mocchiut 1.7 shellg_(&lat, &lon, &alt, &dimo, &xl, &icode, &bab1);
1662 mocchiut 1.65 if ( debug ) printf(" call igrf findb \n");
1663 mocchiut 1.7 findb0_(&stps, &bdel, &value, &bequ, &rr0);
1664 mocchiut 1.15 //
1665 mocchiut 1.65 if ( debug ) printf(" done igrf \n");
1666 mocchiut 1.7 orbitalinfo->Bnorth = bnorth;
1667     orbitalinfo->Beast = beast;
1668     orbitalinfo->Bdown = bdown;
1669     orbitalinfo->Babs = babs;
1670 pam-mep 1.60 orbitalinfo->M = dimo;
1671 mocchiut 1.7 orbitalinfo->BB0 = babs/bequ;
1672 mocchiut 1.15 orbitalinfo->L = xl;
1673 mocchiut 1.7 // Set Stormer vertical cutoff using L shell.
1674 mocchiut 1.57 orbitalinfo->cutoffsvl = 14.295 / (xl*xl); //
1675 pam-mep 1.69 if(debug)cout << "L = " << xl << "\tM = " << dimo << "\tvertical cutoff: "<< orbitalinfo->cutoffsvl << endl;
1676    
1677 mocchiut 1.81
1678     // ---------- Forwarded message ----------
1679     // Date: Wed, 09 May 2012 12:16:47 +0200
1680     // From: Alessandro Bruno <alessandro.bruno@ba.infn.it>
1681     // To: Mirko Boezio <mirko.boezio@ts.infn.it>
1682     // Cc: Francesco S. Cafagna <Francesco.Cafagna@ba.infn.it>
1683     // Subject: Störmer vertical cutoff
1684    
1685     // Ciao Mirko,
1686     // volevo segnalarti che il valore dello Störmer vertical cutoff nel Level2 è
1687     // sovrastimato di circa il 4%.
1688     // Dopo un'approfondita analisi con l'IGRF-05 abbiamo ricavano un valore pari
1689     // a: 14.295 / L^2 anzichè 14.9 / L^2, valore obsoleto in quanto riferito agli
1690     // anni '50.
1691     //
1692 mocchiut 1.57 //14.9/(xl*xl);
1693 mocchiut 1.85 orbitalinfo->igrf_icode = (Float_t)icode;
1694 mocchiut 1.15 //
1695 pamela 1.92 } //check lon lat alt
1696 pamelaprod 1.11 //
1697 mocchiut 1.40 if ( debug ) printf(" pitch angle \n");
1698     //
1699 mocchiut 1.30 // pitch angles
1700     //
1701 pamela 1.92 if( orbitalinfo->TimeGap>=0){
1702 mocchiut 1.30 //
1703 mocchiut 1.65 if ( debug ) printf(" timegap %f \n",orbitalinfo->TimeGap);
1704 mocchiut 1.49 Float_t Bx = -orbitalinfo->Bdown;
1705     Float_t By = orbitalinfo->Beast;
1706     Float_t Bz = orbitalinfo->Bnorth;
1707 pam-mep 1.69
1708 malakhov 1.82 // TMatrixD Qiji(3,3);
1709 pam-mep 1.69 TMatrixD Qij = PO->QuatoECI(orbitalinfo->q0,orbitalinfo->q1,orbitalinfo->q2,orbitalinfo->q3);
1710     TMatrixD Dij = PO->ECItoGEO(Qij,orbitalinfo->absTime,orbitalinfo->lat,orbitalinfo->lon);
1711    
1712     //10REDNEW
1713 mocchiut 1.81 // If initial orientation data have reason to be inaccurate
1714     Float_t tg = 0.00;
1715 malakhov 1.74 Float_t tmptg;
1716 pam-ts 1.86 Bool_t tgpar=false;
1717     Bool_t tgpar0=false;
1718     if (orbitalinfo->TimeGap>10.0 || ((modf(orbitalinfo->TimeGap,&tmptg)*1000>10 || modf(orbitalinfo->TimeGap,&tmptg)*1000==0.0) && orbitalinfo->TimeGap>2.0)) tgpar=true;
1719     if (orbitalinfo->TimeGap>180.0) tgpar0=true;
1720 malakhov 1.74 if(MU!=0){
1721 pam-mep 1.69 // if(orbitalinfo->TimeGap>0 && errq==0 && azim==0){ // 10RED CHECK (comparison between three metod of recovering orientation)
1722 pamela 1.92
1723 pam-ts 1.87 if((atime>=RTstart[MU] && atime<RTstart[MU+1] && RTbank1[MU]==0 && RTbank2[MU]==0 && (TMath::Abs(orbitalinfo->etha)>0.1 || tgpar0)) || ((RTbank1[MU]!=0 || RTbank2[MU]!=0) && atime>=RTstart[MU] && atime<RTstart[MU+1] && azim==0 && (errq!=0 || tgpar))){
1724 pamela 1.92
1725 mocchiut 1.81 //found in Rotation Table this data for this time interval
1726 pam-mep 1.69 if(atime<RTtime1[0])
1727     orbitalinfo->azim = 5; //means that RotationTable no started yet
1728     else{
1729     // search for angle betwean velosity and direction to north in tangential to Earth surfase plane in satellite position
1730 pamela 1.92
1731 malakhov 1.74 Double_t bank=RTstart[MU];
1732 pam-mep 1.69 Double_t tlat=orbitalinfo->lat;
1733 malakhov 1.74
1734     tg=modf(orbitalinfo->TimeGap,&tg)*1000;
1735    
1736     if(atime>=RTpluto1[MU] && atime<=RTpluto2[MU]){
1737     Double_t kar=(RTbank2[MU]-RTbank1[MU])/(RTtime2[MU]-RTtime1[MU]);
1738     Double_t bak=RTbank1[MU]-kar*RTtime1[MU];
1739     bank=kar*atime+bak;
1740     }
1741     if(atime>=RTstart[MU] && atime<RTpluto1[MU]){
1742 pamela 1.80 Double_t s_dBdt2=(RTbpluto1[MU]-RTbank1[MU])/(Int_t)(RTpluto1[MU]-RTstart[MU]);
1743 malakhov 1.74 Double_t s_t2=((Double_t)RTpluto1[MU]+(Double_t)RTstart[MU])/2. - RTstart[MU];
1744     Double_t s_t1=RTstart[MU]-RTstart[MU];
1745     Double_t s_k=s_dBdt2/(s_t2-s_t1);
1746     Double_t s_b=-s_k*s_t1;
1747     Double_t s_t3=RTpluto1[MU]-RTstart[MU];
1748     Double_t s_b3=RTbpluto1[MU];
1749     Double_t s_c=s_b3-0.5*s_k*s_t3*s_t3 -s_b*s_t3;
1750     bank=0.5*s_k*(atime-RTstart[MU])*(atime-RTstart[MU]) + s_b*(atime-RTstart[MU]) + s_c;
1751     }
1752     if(atime>RTpluto2[MU] && atime<=RTstart[MU+1]){
1753 pamela 1.80 Double_t s_dBdt2=(RTbpluto2[MU] - RTbank2[MU])/(Int_t)(RTpluto2[MU]-RTstart[MU+1]);
1754 malakhov 1.74 Double_t s_t2=((Double_t)RTpluto2[MU]+(Double_t)RTstart[MU+1])/2. - RTstart[MU];
1755     Double_t s_t1=RTstart[MU+1]-RTstart[MU];
1756     Double_t s_k=s_dBdt2/(s_t2-s_t1);
1757     Double_t s_b=-s_k*s_t1;
1758     Double_t s_t3=RTpluto2[MU]-RTstart[MU];
1759     Double_t s_b3=RTbpluto2[MU];
1760     Double_t s_c=s_b3-0.5*s_k*s_t3*s_t3 -s_b*s_t3;
1761     bank=0.5*s_k*(atime-RTstart[MU])*(atime-RTstart[MU]) + s_b*(atime-RTstart[MU]) + s_c;
1762     }
1763 malakhov 1.82 if(TMath::Abs(orbitalinfo->etha-bank)>0.1){
1764     orbitalinfo->etha=bank;
1765     Double_t spitch = 0.00001; // temprary not zero to avoid problem with tranzition from Euler angles to orientation matrix
1766 pam-mep 1.69
1767     //Estimations of pitch angle of satellite
1768 malakhov 1.82 if(TMath::Abs(bank)>0.7){
1769     Float_t spitch1=TMath::DegToRad()*0.7*diro;//RTdir1[MU];
1770     Float_t spitch2=TMath::DegToRad()*0.7*diro;//RTdir2[MU];
1771     Float_t kva=(spitch2-spitch1)/(RTtime2[MU]-RTtime1[MU]);
1772     Float_t bva=spitch1-kva*RTtime1[MU];
1773     spitch=kva*atime+bva;
1774     }
1775    
1776     //Calculate Yaw angle accordingly with fit, see picture FitYaw.jpg
1777     Double_t yaw=0.00001; // temprary not zero to avoid problem with tranzition from Euler angles to orientation matrix
1778     if(TMath::Abs(tlat)<70)
1779     yaw = -3.7e-8*tlat*tlat*tlat*tlat + 1.4e-7*tlat*tlat*tlat - 0.0005*tlat*tlat - 0.00025*tlat + 3.6;
1780     yaw = diro*yaw; //because should be different sign for ascending and descending orbits!
1781     orbitalinfo->phi=yaw;
1782    
1783     if(TMath::Abs(bank)>0.5 && TMath::Abs(yaw-orbitalinfo->phi)<3.0) yaw=orbitalinfo->phi;
1784    
1785     // Qiji = PO->EulertoEci(eCi.getPos().m_x,eCi.getPos().m_y,eCi.getPos().m_z,eCi.getVel().m_x,eCi.getVel().m_y,eCi.getVel().m_z,bank,yaw,spitch); // 10RED CHECK
1786     Qij = PO->EulertoEci(eCi.getPos().m_x,eCi.getPos().m_y,eCi.getPos().m_z,eCi.getVel().m_x,eCi.getVel().m_y,eCi.getVel().m_z,bank,yaw,spitch); // STANDARD
1787     orbitalinfo->qkind = 1;
1788     }
1789 pam-mep 1.69
1790     //Qij = PO->GEOtoECI(Dij,orbitalinfo->absTime,orbitalinfo->lat,orbitalinfo->lon); // to convert from Dij to Qij
1791    
1792     } // end of if(atime<RTtime1[0]
1793 malakhov 1.82 } // end of (((orbitalinfo->TimeGap>60.0 && TMath...
1794 malakhov 1.74 } // end of MU~=0
1795 pam-mep 1.69
1796     TMatrixD qij = PO->ColPermutation(Qij);
1797     TMatrixD Fij = PO->ECItoGreenwich(Qij,orbitalinfo->absTime);
1798 mocchiut 1.49 TMatrixD Gij = PO->ColPermutation(Fij);
1799 pam-mep 1.69 Dij = PO->ECItoGEO(Qij,orbitalinfo->absTime,orbitalinfo->lat,orbitalinfo->lon);
1800 mocchiut 1.30 TMatrixD Iij = PO->ColPermutation(Dij);
1801 pamela 1.92
1802 pam-mep 1.69 TVector3 SP = PO->GetSunPosition(orbitalinfo->absTime);
1803     // go to Pamela reference frame from Resurs reference frame
1804     Float_t tmpy = SP.Y();
1805     SP.SetY(SP.Z());
1806     SP.SetZ(-tmpy);
1807     TVector3 SunZenith;
1808     SunZenith.SetMagThetaPhi(1,23.439281*TMath::DegToRad(),TMath::Pi()/2.);
1809     TVector3 SunMag = -SP;
1810     SunMag.Rotate(-45*TMath::DegToRad(),SunZenith);
1811     tmpy=SunMag.Y();
1812     SunMag.SetY(SunMag.Z());
1813     SunMag.SetZ(-tmpy);
1814    
1815 mocchiut 1.33 orbitalinfo->Iij.ResizeTo(Iij);
1816     orbitalinfo->Iij = Iij;
1817 pam-ts 1.86
1818     Bool_t saso=true;
1819     if (orbitalinfo->qkind==1) saso=true;
1820 pam-ts 1.91 if (orbitalinfo->qkind==0 && orbitalinfo->azim>0 && orbitalinfo->azim!=5 && tgpar) saso=false;
1821 pam-ts 1.90 if (orbitalinfo->qkind==0 && orbitalinfo->azim==5 && TMath::Abs(orbitalinfo->etha)>0.1 && tgpar) saso=false;
1822 pam-ts 1.89 if (orbitalinfo->qkind==0 && orbitalinfo->azim==5 && TMath::Abs(orbitalinfo->etha)<=0.1 && tgpar0) saso=false;
1823 pam-ts 1.86 if (saso) orbitalinfo->mode=orbitalinfo->rtqual; else orbitalinfo->mode=2;
1824    
1825 mocchiut 1.33 //
1826 mocchiut 1.64 // A1 = Iij(0,2);
1827     // A2 = Iij(1,2);
1828     // A3 = Iij(2,2);
1829 mocchiut 1.49 //
1830 mocchiut 1.33 // orbitalinfo->pamzenitangle = (Float_t)PO->GetPitchAngle(1,0,0,A1,A2,A3); // Angle between zenit and Pamela's main axiz
1831     // orbitalinfo->pamBangle = (Float_t)PO->GetPitchAngle(A1,A2,A3,Bx,By,Bz); // Angle between Pamela's main axiz and B
1832 mocchiut 1.30 //
1833 mocchiut 1.65 if ( debug ) printf(" matrixes done \n");
1834 mocchiut 1.73 if ( !standalone ){
1835 mocchiut 1.65 if ( debug ) printf(" !standalone \n");
1836 mocchiut 1.30 //
1837 mocchiut 1.73 // Standard tracking algorithm
1838     //
1839 mocchiut 1.30 Int_t nn = 0;
1840 mocchiut 1.73 if ( verbose ) printf(" standard tracking \n");
1841 mocchiut 1.30 for(Int_t nt=0; nt < tof->ntrk(); nt++){
1842     //
1843     ToFTrkVar *ptt = tof->GetToFTrkVar(nt);
1844 pam-mep 1.69 if (debug) cout<<"tof->ntrk() = "<<tof->ntrk()<<"\tptt->trkseqno = "<<ptt->trkseqno<<"\ttrke->ntrk() = "<<trke->ntrk()<<endl;
1845 mocchiut 1.30 Double_t E11x = ptt->xtr_tof[0]; // tr->x[0];
1846     Double_t E11y = ptt->ytr_tof[0]; //tr->y[0];
1847     Double_t E11z = zin[0];
1848     Double_t E22x = ptt->xtr_tof[3];//tr->x[3];
1849     Double_t E22y = ptt->ytr_tof[3];//tr->y[3];
1850     Double_t E22z = zin[3];
1851 mocchiut 1.32 if ( (E11x < 100. && E11y < 100. && E22x < 100. && E22y < 100.) || ptt->trkseqno != -1 ){
1852 pam-mep 1.69 TrkTrack *mytrack = trke->GetStoredTrack(ptt->trkseqno);
1853     Float_t rig=1/mytrack->GetDeflection();
1854 mocchiut 1.30 Double_t norm = sqrt(pow(E22x-E11x,2)+pow(E22y-E11y,2)+pow(E22z-E11z,2));
1855 mocchiut 1.73 //
1856 mocchiut 1.30 Px = (E22x-E11x)/norm;
1857     Py = (E22y-E11y)/norm;
1858     Pz = (E22z-E11z)/norm;
1859     //
1860 mocchiut 1.33 t_orb->trkseqno = ptt->trkseqno;
1861     //
1862     TMatrixD Eij = PO->PamelatoGEO(Iij,Px,Py,Pz);
1863     t_orb->Eij.ResizeTo(Eij);
1864     t_orb->Eij = Eij;
1865     //
1866 pam-mep 1.51 TMatrixD Sij = PO->PamelatoGEO(Gij,Px,Py,Pz);
1867 mocchiut 1.33 t_orb->Sij.ResizeTo(Sij);
1868     t_orb->Sij = Sij;
1869 mocchiut 1.30 //
1870     t_orb->pitch = (Float_t)PO->GetPitchAngle(Eij(0,0),Eij(1,0),Eij(2,0),Bx,By,Bz);
1871 mocchiut 1.33 //
1872 mocchiut 1.73 // SunPosition in instrumental reference frame
1873 pam-mep 1.69 TMatrixD Kij = PO->PamelatoGEO(qij,Px,Py,Pz);
1874     TMatrixD Lij = PO->PamelatoGEO(qij,0,0,1);
1875     t_orb->sunangle=(Float_t)PO->GetPitchAngle(Kij(0,0),Kij(1,0),Kij(2,0),-SP.X(),-SP.Y(),-SP.Z());
1876     t_orb->sunmagangle=(Float_t)PO->GetPitchAngle(Kij(0,0),Kij(1,0),Kij(2,0),SunMag.X(),SunMag.Y(),SunMag.Z());
1877 mocchiut 1.33 //
1878     //
1879 pam-mep 1.69 Double_t omega = PO->GetPitchAngle(-Eij(0,0),-Eij(1,0),-Eij(2,0),1,0,0) * TMath::DegToRad();
1880     TVector3 Bxy(0,By,Bz);
1881     TVector3 Exy(0,-Eij(1,0),-Eij(2,0));
1882     Double_t dzeta=Bxy.Angle(Exy);
1883     if (-Eij(1,0) < 0) dzeta=2.0*TMath::Pi() - dzeta;
1884 mocchiut 1.73
1885     if(debug) cout << "omega = "<<omega*TMath::RadToDeg()<<"\tdzeta = "<<dzeta*TMath::RadToDeg()<<endl;
1886 pam-mep 1.69
1887 mocchiut 1.73 // Formula from D.F. Smart *, M.A. Shea [2005]; A review of geomagnetic cutoff rigidities for earth-orbiting spacecraft
1888 pam-mep 1.69 if(rig>=0) t_orb->cutoff = 59.3/(pow(orbitalinfo->L,2)*pow(1+sqrt(1-sin(omega)*sin(dzeta)*pow(cos(orbitalinfo->lat*TMath::DegToRad()),3)),2));
1889 mocchiut 1.73 else t_orb->cutoff = 59.3/(pow(orbitalinfo->L,2)*pow(1+sqrt(1-sin(omega)*sin(TMath::Pi()+dzeta)*pow(cos(orbitalinfo->lat*TMath::DegToRad()),3)),2));
1890 pam-mep 1.69 if (debug) cout << "R = " << rig << "\tcutoff = " << t_orb->cutoff << endl;
1891    
1892 mocchiut 1.33 //
1893 mocchiut 1.36 if ( t_orb->pitch != t_orb->pitch ) t_orb->pitch = -1000.;
1894     if ( t_orb->cutoff != t_orb->cutoff ) t_orb->cutoff = -1000.;
1895 pam-mep 1.69 if ( t_orb->sunangle != t_orb->sunangle ) t_orb->sunangle = -1000.;
1896     if ( t_orb->sunmagangle != t_orb->sunmagangle ) t_orb->sunmagangle = -1000.;
1897 mocchiut 1.36 //
1898 mocchiut 1.33 if ( debug ) printf(" orbitalinfo->cutoffsvl %f vitaly %f \n",orbitalinfo->cutoffsvl,t_orb->cutoff);
1899 mocchiut 1.30 //
1900     new(tor[nn]) OrbitalInfoTrkVar(*t_orb);
1901     nn++;
1902     //
1903     t_orb->Clear();
1904     //
1905 mocchiut 1.73 }
1906 mocchiut 1.30 //
1907 mocchiut 1.73 } // end standard tracking algorithm
1908    
1909     //
1910     // Code for extended tracking algorithm:
1911     //
1912     if ( hasNucleiTrk ){
1913     Int_t ttentry = 0;
1914     if ( verbose ) printf(" hasNucleiTrk \n");
1915     for(Int_t nt=0; nt < tcNucleiTof->GetEntries() ; nt++){
1916     //
1917     if ( verbose ) printf(" got1\n");
1918     ToFTrkVar *ptt = (ToFTrkVar*)(tcNucleiTof->At(nt));
1919     if (verbose) cout<<" tcNucleiTof->GetEntries() = "<<tcNucleiTof->GetEntries()<<"\tptt->trkseqno = "<<ptt->trkseqno<<endl;
1920     Double_t E11x = ptt->xtr_tof[0]; // tr->x[0];
1921     Double_t E11y = ptt->ytr_tof[0]; //tr->y[0];
1922     Double_t E11z = zin[0];
1923     Double_t E22x = ptt->xtr_tof[3];//tr->x[3];
1924     Double_t E22y = ptt->ytr_tof[3];//tr->y[3];
1925     Double_t E22z = zin[3];
1926     if ( (E11x < 100. && E11y < 100. && E22x < 100. && E22y < 100.) || ptt->trkseqno != -1 ){
1927     TrkTrack *mytrack = (TrkTrack*)(tcNucleiTrk->At(ptt->trkseqno));
1928     if ( verbose ) printf(" got tcNucleiTrk \n");
1929     Float_t rig=1/mytrack->GetDeflection();
1930     Double_t norm = sqrt(pow(E22x-E11x,2)+pow(E22y-E11y,2)+pow(E22z-E11z,2));
1931     //
1932     Px = (E22x-E11x)/norm;
1933     Py = (E22y-E11y)/norm;
1934     Pz = (E22z-E11z)/norm;
1935     //
1936     t_orb->trkseqno = ptt->trkseqno;
1937     //
1938     TMatrixD Eij = PO->PamelatoGEO(Iij,Px,Py,Pz);
1939     t_orb->Eij.ResizeTo(Eij);
1940     t_orb->Eij = Eij;
1941     //
1942     TMatrixD Sij = PO->PamelatoGEO(Gij,Px,Py,Pz);
1943     t_orb->Sij.ResizeTo(Sij);
1944     t_orb->Sij = Sij;
1945     //
1946     t_orb->pitch = (Float_t)PO->GetPitchAngle(Eij(0,0),Eij(1,0),Eij(2,0),Bx,By,Bz);
1947     //
1948     // SunPosition in instrumental reference frame
1949     TMatrixD Kij = PO->PamelatoGEO(qij,Px,Py,Pz);
1950     TMatrixD Lij = PO->PamelatoGEO(qij,0,0,1);
1951     t_orb->sunangle=(Float_t)PO->GetPitchAngle(Kij(0,0),Kij(1,0),Kij(2,0),-SP.X(),-SP.Y(),-SP.Z());
1952     t_orb->sunmagangle=(Float_t)PO->GetPitchAngle(Kij(0,0),Kij(1,0),Kij(2,0),SunMag.X(),SunMag.Y(),SunMag.Z());
1953     //
1954     //
1955     Double_t omega = PO->GetPitchAngle(-Eij(0,0),-Eij(1,0),-Eij(2,0),1,0,0) * TMath::DegToRad();
1956     TVector3 Bxy(0,By,Bz);
1957     TVector3 Exy(0,-Eij(1,0),-Eij(2,0));
1958     Double_t dzeta=Bxy.Angle(Exy);
1959     if (-Eij(1,0) < 0) dzeta=2.0*TMath::Pi() - dzeta;
1960    
1961     if(debug) cout << "omega = "<<omega*TMath::RadToDeg()<<"\tdzeta = "<<dzeta*TMath::RadToDeg()<<endl;
1962    
1963     // Formula from D.F. Smart *, M.A. Shea [2005]; A review of geomagnetic cutoff rigidities for earth-orbiting spacecraft
1964     if(rig>=0) t_orb->cutoff = 59.3/(pow(orbitalinfo->L,2)*pow(1+sqrt(1-sin(omega)*sin(dzeta)*pow(cos(orbitalinfo->lat*TMath::DegToRad()),3)),2));
1965     else t_orb->cutoff = 59.3/(pow(orbitalinfo->L,2)*pow(1+sqrt(1-sin(omega)*sin(TMath::Pi()+dzeta)*pow(cos(orbitalinfo->lat*TMath::DegToRad()),3)),2));
1966     if (debug) cout << "R = " << rig << "\tcutoff = " << t_orb->cutoff << endl;
1967    
1968     //
1969     if ( t_orb->pitch != t_orb->pitch ) t_orb->pitch = -1000.;
1970     if ( t_orb->cutoff != t_orb->cutoff ) t_orb->cutoff = -1000.;
1971     if ( t_orb->sunangle != t_orb->sunangle ) t_orb->sunangle = -1000.;
1972     if ( t_orb->sunmagangle != t_orb->sunmagangle ) t_orb->sunmagangle = -1000.;
1973     //
1974     if ( debug ) printf(" orbitalinfo->cutoffsvl %f vitaly %f \n",orbitalinfo->cutoffsvl,t_orb->cutoff);
1975     //
1976     TClonesArray &tt1 = *torbNucleiTrk;
1977     new(tt1[ttentry]) OrbitalInfoTrkVar(*t_orb);
1978     ttentry++;
1979     //
1980     t_orb->Clear();
1981     //
1982     }
1983     //
1984     }
1985     } // end standard tracking algorithm: nuclei
1986     if ( hasExtNucleiTrk ){
1987     Int_t ttentry = 0;
1988     if ( verbose ) printf(" hasExtNucleiTrk \n");
1989     for(Int_t nt=0; nt < tcExtNucleiTof->GetEntries() ; nt++){
1990     //
1991     if ( verbose ) printf(" got2\n");
1992     ToFTrkVar *ptt = (ToFTrkVar*)(tcExtNucleiTof->At(nt));
1993     if (verbose) cout<<" tcExtNucleiTof->GetEntries() = "<<tcExtNucleiTof->GetEntries()<<"\tptt->trkseqno = "<<ptt->trkseqno<<endl;
1994     Double_t E11x = ptt->xtr_tof[0]; // tr->x[0];
1995     Double_t E11y = ptt->ytr_tof[0]; //tr->y[0];
1996     Double_t E11z = zin[0];
1997     Double_t E22x = ptt->xtr_tof[3];//tr->x[3];
1998     Double_t E22y = ptt->ytr_tof[3];//tr->y[3];
1999     Double_t E22z = zin[3];
2000     if ( (E11x < 100. && E11y < 100. && E22x < 100. && E22y < 100.) || ptt->trkseqno != -1 ){
2001     ExtTrack *mytrack = (ExtTrack*)(tcExtNucleiTrk->At(ptt->trkseqno));
2002     if ( verbose ) printf(" got tcExtNucleiTrk \n");
2003     Float_t rig=1/mytrack->GetDeflection();
2004     Double_t norm = sqrt(pow(E22x-E11x,2)+pow(E22y-E11y,2)+pow(E22z-E11z,2));
2005     //
2006     Px = (E22x-E11x)/norm;
2007     Py = (E22y-E11y)/norm;
2008     Pz = (E22z-E11z)/norm;
2009     //
2010     t_orb->trkseqno = ptt->trkseqno;
2011     //
2012     TMatrixD Eij = PO->PamelatoGEO(Iij,Px,Py,Pz);
2013     t_orb->Eij.ResizeTo(Eij);
2014     t_orb->Eij = Eij;
2015     //
2016     TMatrixD Sij = PO->PamelatoGEO(Gij,Px,Py,Pz);
2017     t_orb->Sij.ResizeTo(Sij);
2018     t_orb->Sij = Sij;
2019     //
2020     t_orb->pitch = (Float_t)PO->GetPitchAngle(Eij(0,0),Eij(1,0),Eij(2,0),Bx,By,Bz);
2021     //
2022     // SunPosition in instrumental reference frame
2023     TMatrixD Kij = PO->PamelatoGEO(qij,Px,Py,Pz);
2024     TMatrixD Lij = PO->PamelatoGEO(qij,0,0,1);
2025     t_orb->sunangle=(Float_t)PO->GetPitchAngle(Kij(0,0),Kij(1,0),Kij(2,0),-SP.X(),-SP.Y(),-SP.Z());
2026     t_orb->sunmagangle=(Float_t)PO->GetPitchAngle(Kij(0,0),Kij(1,0),Kij(2,0),SunMag.X(),SunMag.Y(),SunMag.Z());
2027     //
2028     //
2029     Double_t omega = PO->GetPitchAngle(-Eij(0,0),-Eij(1,0),-Eij(2,0),1,0,0) * TMath::DegToRad();
2030     TVector3 Bxy(0,By,Bz);
2031     TVector3 Exy(0,-Eij(1,0),-Eij(2,0));
2032     Double_t dzeta=Bxy.Angle(Exy);
2033     if (-Eij(1,0) < 0) dzeta=2.0*TMath::Pi() - dzeta;
2034    
2035     if(debug) cout << "omega = "<<omega*TMath::RadToDeg()<<"\tdzeta = "<<dzeta*TMath::RadToDeg()<<endl;
2036    
2037     // Formula from D.F. Smart *, M.A. Shea [2005]; A review of geomagnetic cutoff rigidities for earth-orbiting spacecraft
2038     if(rig>=0) t_orb->cutoff = 59.3/(pow(orbitalinfo->L,2)*pow(1+sqrt(1-sin(omega)*sin(dzeta)*pow(cos(orbitalinfo->lat*TMath::DegToRad()),3)),2));
2039     else t_orb->cutoff = 59.3/(pow(orbitalinfo->L,2)*pow(1+sqrt(1-sin(omega)*sin(TMath::Pi()+dzeta)*pow(cos(orbitalinfo->lat*TMath::DegToRad()),3)),2));
2040     if (debug) cout << "R = " << rig << "\tcutoff = " << t_orb->cutoff << endl;
2041    
2042     //
2043     if ( t_orb->pitch != t_orb->pitch ) t_orb->pitch = -1000.;
2044     if ( t_orb->cutoff != t_orb->cutoff ) t_orb->cutoff = -1000.;
2045     if ( t_orb->sunangle != t_orb->sunangle ) t_orb->sunangle = -1000.;
2046     if ( t_orb->sunmagangle != t_orb->sunmagangle ) t_orb->sunmagangle = -1000.;
2047     //
2048     if ( debug ) printf(" orbitalinfo->cutoffsvl %f vitaly %f \n",orbitalinfo->cutoffsvl,t_orb->cutoff);
2049     //
2050     TClonesArray &tt2 = *torbExtNucleiTrk;
2051     new(tt2[ttentry]) OrbitalInfoTrkVar(*t_orb);
2052     ttentry++;
2053     //
2054     t_orb->Clear();
2055     //
2056     }
2057     //
2058     }
2059     } // end standard tracking algorithm: nuclei extended
2060     if ( hasExtTrk ){
2061     Int_t ttentry = 0;
2062     if ( verbose ) printf(" hasExtTrk \n");
2063     for(Int_t nt=0; nt < tcExtTof->GetEntries() ; nt++){
2064     //
2065     if ( verbose ) printf(" got3\n");
2066     ToFTrkVar *ptt = (ToFTrkVar*)(tcExtTof->At(nt));
2067     if (verbose) cout<<" tcExtTof->GetEntries() = "<<tcExtTof->GetEntries()<<"\tptt->trkseqno = "<<ptt->trkseqno<<endl;
2068     Double_t E11x = ptt->xtr_tof[0]; // tr->x[0];
2069     Double_t E11y = ptt->ytr_tof[0]; //tr->y[0];
2070     Double_t E11z = zin[0];
2071     Double_t E22x = ptt->xtr_tof[3];//tr->x[3];
2072     Double_t E22y = ptt->ytr_tof[3];//tr->y[3];
2073     Double_t E22z = zin[3];
2074     if ( (E11x < 100. && E11y < 100. && E22x < 100. && E22y < 100.) || ptt->trkseqno != -1 ){
2075     ExtTrack *mytrack = (ExtTrack*)(tcExtTrk->At(ptt->trkseqno));
2076     if ( verbose ) printf(" got tcExtTrk \n");
2077     Float_t rig=1/mytrack->GetDeflection();
2078     Double_t norm = sqrt(pow(E22x-E11x,2)+pow(E22y-E11y,2)+pow(E22z-E11z,2));
2079     //
2080     Px = (E22x-E11x)/norm;
2081     Py = (E22y-E11y)/norm;
2082     Pz = (E22z-E11z)/norm;
2083     //
2084     t_orb->trkseqno = ptt->trkseqno;
2085     //
2086     TMatrixD Eij = PO->PamelatoGEO(Iij,Px,Py,Pz);
2087     t_orb->Eij.ResizeTo(Eij);
2088     t_orb->Eij = Eij;
2089     //
2090     TMatrixD Sij = PO->PamelatoGEO(Gij,Px,Py,Pz);
2091     t_orb->Sij.ResizeTo(Sij);
2092     t_orb->Sij = Sij;
2093     //
2094     t_orb->pitch = (Float_t)PO->GetPitchAngle(Eij(0,0),Eij(1,0),Eij(2,0),Bx,By,Bz);
2095     //
2096     // SunPosition in instrumental reference frame
2097     TMatrixD Kij = PO->PamelatoGEO(qij,Px,Py,Pz);
2098     TMatrixD Lij = PO->PamelatoGEO(qij,0,0,1);
2099     t_orb->sunangle=(Float_t)PO->GetPitchAngle(Kij(0,0),Kij(1,0),Kij(2,0),-SP.X(),-SP.Y(),-SP.Z());
2100     t_orb->sunmagangle=(Float_t)PO->GetPitchAngle(Kij(0,0),Kij(1,0),Kij(2,0),SunMag.X(),SunMag.Y(),SunMag.Z());
2101     //
2102     //
2103     Double_t omega = PO->GetPitchAngle(-Eij(0,0),-Eij(1,0),-Eij(2,0),1,0,0) * TMath::DegToRad();
2104     TVector3 Bxy(0,By,Bz);
2105     TVector3 Exy(0,-Eij(1,0),-Eij(2,0));
2106     Double_t dzeta=Bxy.Angle(Exy);
2107     if (-Eij(1,0) < 0) dzeta=2.0*TMath::Pi() - dzeta;
2108    
2109     if(debug) cout << "omega = "<<omega*TMath::RadToDeg()<<"\tdzeta = "<<dzeta*TMath::RadToDeg()<<endl;
2110    
2111     // Formula from D.F. Smart *, M.A. Shea [2005]; A review of geomagnetic cutoff rigidities for earth-orbiting spacecraft
2112     if(rig>=0) t_orb->cutoff = 59.3/(pow(orbitalinfo->L,2)*pow(1+sqrt(1-sin(omega)*sin(dzeta)*pow(cos(orbitalinfo->lat*TMath::DegToRad()),3)),2));
2113     else t_orb->cutoff = 59.3/(pow(orbitalinfo->L,2)*pow(1+sqrt(1-sin(omega)*sin(TMath::Pi()+dzeta)*pow(cos(orbitalinfo->lat*TMath::DegToRad()),3)),2));
2114     if (debug) cout << "R = " << rig << "\tcutoff = " << t_orb->cutoff << endl;
2115    
2116     //
2117     if ( t_orb->pitch != t_orb->pitch ) t_orb->pitch = -1000.;
2118     if ( t_orb->cutoff != t_orb->cutoff ) t_orb->cutoff = -1000.;
2119     if ( t_orb->sunangle != t_orb->sunangle ) t_orb->sunangle = -1000.;
2120     if ( t_orb->sunmagangle != t_orb->sunmagangle ) t_orb->sunmagangle = -1000.;
2121     //
2122     if ( debug ) printf(" orbitalinfo->cutoffsvl %f vitaly %f \n",orbitalinfo->cutoffsvl,t_orb->cutoff);
2123     //
2124     TClonesArray &tt3 = *torbExtTrk;
2125     new(tt3[ttentry]) OrbitalInfoTrkVar(*t_orb);
2126     ttentry++;
2127     //
2128     t_orb->Clear();
2129     //
2130     }
2131     //
2132     }
2133     } // end standard tracking algorithm: extended
2134    
2135 mocchiut 1.30 } else {
2136 mocchiut 1.65 if ( debug ) printf(" mmm... mode %u standalone \n",orbitalinfo->mode);
2137     }
2138 mocchiut 1.30 //
2139 mocchiut 1.73 } else { // HERE IT MISS ALL CODE FOR EXTENDED TRACKING!
2140     if ( !standalone ){
2141 mocchiut 1.35 //
2142 mocchiut 1.73 if ( verbose ) printf(" no orb info for tracks \n");
2143 mocchiut 1.35 Int_t nn = 0;
2144     for(Int_t nt=0; nt < tof->ntrk(); nt++){
2145     //
2146     ToFTrkVar *ptt = tof->GetToFTrkVar(nt);
2147     if ( ptt->trkseqno != -1 ){
2148     //
2149     t_orb->trkseqno = ptt->trkseqno;
2150     //
2151     t_orb->Eij = 0;
2152     //
2153     t_orb->Sij = 0;
2154     //
2155     t_orb->pitch = -1000.;
2156     //
2157 pam-mep 1.69 t_orb->sunangle = -1000.;
2158     //
2159     t_orb->sunmagangle = -1000;
2160     //
2161 mocchiut 1.35 t_orb->cutoff = -1000.;
2162     //
2163     new(tor[nn]) OrbitalInfoTrkVar(*t_orb);
2164     nn++;
2165     //
2166     t_orb->Clear();
2167     //
2168 mocchiut 1.73 }
2169 mocchiut 1.35 //
2170 mocchiut 1.73 }
2171     //
2172     // Code for extended tracking algorithm:
2173     //
2174     if ( hasNucleiTrk ){
2175     Int_t ttentry = 0;
2176     if ( verbose ) printf(" hasNucleiTrk \n");
2177     for(Int_t nt=0; nt < tcNucleiTof->GetEntries() ; nt++){
2178     //
2179     ToFTrkVar *ptt = (ToFTrkVar*)(tcNucleiTof->At(nt));
2180     if ( ptt->trkseqno != -1 ){
2181     //
2182     t_orb->trkseqno = ptt->trkseqno;
2183     //
2184     t_orb->Eij = 0;
2185     //
2186     t_orb->Sij = 0;
2187     //
2188     t_orb->pitch = -1000.;
2189     //
2190     t_orb->sunangle = -1000.;
2191     //
2192     t_orb->sunmagangle = -1000;
2193     //
2194     t_orb->cutoff = -1000.;
2195     //
2196     TClonesArray &tz1 = *torbNucleiTrk;
2197     new(tz1[ttentry]) OrbitalInfoTrkVar(*t_orb);
2198     ttentry++;
2199     //
2200     t_orb->Clear();
2201     //
2202     }
2203     //
2204     }
2205     }
2206     if ( hasExtNucleiTrk ){
2207     Int_t ttentry = 0;
2208     if ( verbose ) printf(" hasExtNucleiTrk \n");
2209     for(Int_t nt=0; nt < tcExtNucleiTof->GetEntries() ; nt++){
2210     //
2211     if ( verbose ) printf(" got2\n");
2212     ToFTrkVar *ptt = (ToFTrkVar*)(tcExtNucleiTof->At(nt));
2213     if ( ptt->trkseqno != -1 ){
2214     //
2215     t_orb->trkseqno = ptt->trkseqno;
2216     //
2217     t_orb->Eij = 0;
2218     //
2219     t_orb->Sij = 0;
2220     //
2221     t_orb->pitch = -1000.;
2222     //
2223     t_orb->sunangle = -1000.;
2224     //
2225     t_orb->sunmagangle = -1000;
2226     //
2227     t_orb->cutoff = -1000.;
2228     //
2229     TClonesArray &tz2 = *torbExtNucleiTrk;
2230     new(tz2[ttentry]) OrbitalInfoTrkVar(*t_orb);
2231     ttentry++;
2232     //
2233     t_orb->Clear();
2234     //
2235     }
2236     //
2237     }
2238     }
2239     if ( hasExtTrk ){
2240     Int_t ttentry = 0;
2241     if ( verbose ) printf(" hasExtTrk \n");
2242     for(Int_t nt=0; nt < tcExtTof->GetEntries() ; nt++){
2243     //
2244     if ( verbose ) printf(" got3\n");
2245     ToFTrkVar *ptt = (ToFTrkVar*)(tcExtTof->At(nt));
2246     if ( ptt->trkseqno != -1 ){
2247     //
2248     t_orb->trkseqno = ptt->trkseqno;
2249     //
2250     t_orb->Eij = 0;
2251     //
2252     t_orb->Sij = 0;
2253     //
2254     t_orb->pitch = -1000.;
2255     //
2256     t_orb->sunangle = -1000.;
2257     //
2258     t_orb->sunmagangle = -1000;
2259     //
2260     t_orb->cutoff = -1000.;
2261     //
2262     TClonesArray &tz3 = *torbExtNucleiTrk;
2263     new(tz3[ttentry]) OrbitalInfoTrkVar(*t_orb);
2264     ttentry++;
2265     //
2266     t_orb->Clear();
2267     //
2268     }
2269     //
2270     }
2271     }
2272     }
2273     } // if( orbitalinfo->TimeGap>0){
2274 mocchiut 1.30 //
2275 mocchiut 1.15 // Fill the class
2276 pamelaprod 1.11 //
2277 mocchiut 1.1 OrbitalInfotr->Fill();
2278 mocchiut 1.15 //
2279 mocchiut 1.81 // tor.Clear("C"); // memory leak?
2280     tor.Delete(); // memory leak?
2281 mocchiut 1.30 delete t_orb;
2282     //
2283 mocchiut 1.81 // printf(" q0 size %i q0 capacity %i \n",(int)q0.size(),(int)q0.capacity());
2284     } // loop over the events in the run
2285    
2286    
2287 mocchiut 1.1 //
2288     // Here you may want to clear some variables before processing another run
2289     //
2290 mocchiut 1.44
2291 mocchiut 1.81 // OrbitalInfotr->FlushBaskets();
2292    
2293 mocchiut 1.57 if ( verbose ) printf(" Clear before new run \n");
2294 mocchiut 1.5 delete dbtime;
2295 mocchiut 1.57
2296 mocchiut 1.62 if ( mcmdrc ) mcmdrc->Clear();
2297 mocchiut 1.57 mcmdrc = 0;
2298    
2299     if ( verbose ) printf(" Clear before new run1 \n");
2300     if ( L_QQ_Q_l_lower ) delete L_QQ_Q_l_lower;
2301     if ( verbose ) printf(" Clear before new run2 \n");
2302 mocchiut 1.18 if ( L_QQ_Q_l_upper ) delete L_QQ_Q_l_upper;
2303 mocchiut 1.57 if ( verbose ) printf(" Clear before new run3 \n");
2304 mocchiut 1.18 if ( RYPang_upper ) delete RYPang_upper;
2305 mocchiut 1.57 if ( verbose ) printf(" Clear before new run4 \n");
2306 mocchiut 1.18 if ( RYPang_lower ) delete RYPang_lower;
2307 mocchiut 1.57
2308 mocchiut 1.81
2309     if ( l0tr ){
2310     if ( verbose ) printf(" delete l0tr\n");
2311     l0tr->Delete();
2312     l0tr = 0;
2313     }
2314     // if ( l0head ){
2315     // printf(" delete l0head\n");
2316     // l0head->Reset();
2317     // delete l0head;
2318     // printf(" delete l0head done\n");
2319     // l0head = 0;
2320     // }
2321     if (eh) {
2322     if ( verbose ) printf(" delete eh\n");
2323     delete eh;
2324     eh = 0;
2325     }
2326    
2327     if ( verbose ) printf(" close file \n");
2328     if ( l0File ) l0File->Close("R");
2329 mocchiut 1.57 if ( verbose ) printf(" End run \n");
2330    
2331 mocchiut 1.81 q0.clear();
2332     q1.clear();
2333     q2.clear();
2334     q3.clear();
2335     qtime.clear();
2336     qPitch.clear();
2337     qRoll.clear();
2338     qYaw.clear();
2339     qmode.clear();
2340    
2341     if (ch){
2342     if ( verbose ) printf(" delete ch\n");
2343     ch->Delete();
2344     ch = 0;
2345     }
2346     } // process all the runs <===
2347     if ( debug ){
2348     printf("AFTER LOOP ON RUNs\n");
2349     gObjectTable->Print();
2350     }
2351     //
2352 mocchiut 1.1 if (verbose) printf("\n Finished processing data \n");
2353     //
2354     closeandexit:
2355     //
2356     // 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.
2357     //
2358     if ( !reprocall && reproc && code >= 0 ){
2359     if ( totfileentries > noaftrun ){
2360     if (verbose){
2361     printf("\n Post-processing: copying events from the old tree after the processed run\n");
2362     printf(" Copying %i events in the file which are after the end of the run %i \n",(int)(totfileentries-noaftrun),(int)run);
2363     printf(" Start copying at event number %i end copying at event number %i \n",(int)noaftrun,(int)totfileentries);
2364     }
2365     for (UInt_t j = noaftrun; j < totfileentries; j++ ){
2366     //
2367     // Get entry from old tree
2368     //
2369 mocchiut 1.43 if ( OrbitalInfotrclone->GetEntry(j) <= 0 ) throw -36;
2370 mocchiut 1.1 //
2371     // copy orbitalinfoclone to OrbitalInfo
2372     //
2373 mocchiut 1.76 // orbitalinfo->Clear();
2374 mocchiut 1.5 //
2375 mocchiut 1.1 memcpy(&orbitalinfo,&orbitalinfoclone,sizeof(orbitalinfoclone));
2376     //
2377     // Fill entry in the new tree
2378     //
2379     OrbitalInfotr->Fill();
2380     };
2381     if (verbose) printf(" Finished successful copying!\n");
2382     };
2383 mocchiut 1.57 //if ( OrbitalInfotrclone ) OrbitalInfotrclone->Clear();
2384     //if ( OrbitalInfotrclone ) OrbitalInfotrclone->Delete();
2385 mocchiut 1.1 };
2386     //
2387     // Close files, delete old tree(s), write and close level2 file
2388     //
2389 pam-mep 1.69
2390 mocchiut 1.1 if ( l0File ) l0File->Close();
2391 mocchiut 1.23 if ( myfold ) gSystem->Unlink(tempname.str().c_str());
2392 mocchiut 1.5 //
2393 mocchiut 1.1 if ( OrbitalInfotr ) OrbitalInfotr->SetName("OrbitalInfo");
2394 mocchiut 1.30 //
2395 mocchiut 1.1 if ( file ){
2396     file->cd();
2397 mocchiut 1.63 if ( OrbitalInfotr ) OrbitalInfotr->Write("OrbitalInfo", TObject::kOverwrite); // 10 RED bug fixed
2398 mocchiut 1.1 };
2399     //
2400 mocchiut 1.57 if (verbose) printf("\n Exiting...\n");
2401    
2402 mocchiut 1.23 if ( myfold ) gSystem->Unlink(OrbitalInfofolder.str().c_str());
2403 mocchiut 1.1 //
2404     // the end
2405     //
2406 mocchiut 1.25 if ( dbc ){
2407     dbc->Close();
2408     delete dbc;
2409     };
2410 mocchiut 1.57 //
2411 mocchiut 1.1 if (verbose) printf("\n Exiting...\n");
2412 mocchiut 1.57 if ( tempfile ) tempfile->Close();
2413    
2414 mocchiut 1.30 if ( PO ) delete PO;
2415 mocchiut 1.57 if ( gltle ) delete gltle;
2416     if ( glparam ) delete glparam;
2417     if ( glparam2 ) delete glparam2;
2418     if (verbose) printf("\n Exiting3...\n");
2419 mocchiut 1.4 if ( glroot ) delete glroot;
2420 mocchiut 1.57 if (verbose) printf("\n Exiting4...\n");
2421     if ( runinfo ) runinfo->Close();
2422 mocchiut 1.4 if ( runinfo ) delete runinfo;
2423 mocchiut 1.58
2424 mocchiut 1.73 if ( tcNucleiTrk ){
2425     tcNucleiTrk->Delete();
2426     delete tcNucleiTrk;
2427     tcNucleiTrk = NULL;
2428     }
2429     if ( tcExtNucleiTrk ){
2430     tcExtNucleiTrk->Delete();
2431     delete tcExtNucleiTrk;
2432     tcExtNucleiTrk = NULL;
2433     }
2434     if ( tcExtTrk ){
2435     tcExtTrk->Delete();
2436     delete tcExtTrk;
2437     tcExtTrk = NULL;
2438     }
2439    
2440     if ( tcNucleiTof ){
2441     tcNucleiTof->Delete();
2442     delete tcNucleiTof;
2443     tcNucleiTof = NULL;
2444     }
2445     if ( tcExtNucleiTof ){
2446     tcExtNucleiTof->Delete();
2447     delete tcExtNucleiTof;
2448     tcExtNucleiTof = NULL;
2449     }
2450     if ( tcExtTof ){
2451     tcExtTof->Delete();
2452     delete tcExtTof;
2453     tcExtTrk = NULL;
2454     }
2455    
2456    
2457 pam-mep 1.69 if ( tof ) delete tof;
2458     if ( trke ) delete trke;
2459    
2460 mocchiut 1.58 if ( debug ){
2461 mocchiut 1.57 cout << "1 0x" << OrbitalInfotr << endl;
2462     cout << "2 0x" << OrbitalInfotrclone << endl;
2463     cout << "3 0x" << l0tr << endl;
2464     cout << "4 0x" << tempOrbitalInfo << endl;
2465     cout << "5 0x" << ttof << endl;
2466 mocchiut 1.58 }
2467 mocchiut 1.57 //
2468 mocchiut 1.58 if ( debug ) file->ls();
2469 mocchiut 1.4 //
2470 mocchiut 1.81 if ( debug ){
2471     printf("BEFORE EXITING\n");
2472     gObjectTable->Print();
2473     }
2474 mocchiut 1.1 if(code < 0) throw code;
2475     return(code);
2476     }
2477    
2478 mocchiut 1.7
2479     //
2480     // Returns the cCoordGeo structure holding the geographical
2481     // coordinates for the event (see sgp4.h).
2482     //
2483     // atime is the abstime of the event in UTC unix time.
2484     // tletime is the time of the tle in UTC unix time.
2485     // tle is the previous and nearest tle (compared to atime).
2486     cCoordGeo getCoo(UInt_t atime, UInt_t tletime, cTle *tle)
2487     {
2488     cEci eci;
2489     cOrbit orbit(*tle);
2490     orbit.getPosition((double) (atime - tletime)/60., &eci);
2491    
2492     return eci.toGeo();
2493     }
2494 mocchiut 1.15
2495     // function of copyng of quatrnions classes
2496    
2497     void CopyQ(Quaternions *Q1, Quaternions *Q2){
2498     for(UInt_t i = 0; i < 6; i++){
2499     Q1->time[i]=Q2->time[i];
2500     for (UInt_t j = 0; j < 4; j++)Q1->quat[i][j]=Q2->quat[i][j];
2501     }
2502     return;
2503     }
2504    
2505     // functions of copyng InclinationInfo classes
2506    
2507     void CopyAng(InclinationInfo *A1, InclinationInfo *A2){
2508     A1->Tangazh = A2->Tangazh;
2509     A1->Ryskanie = A2->Ryskanie;
2510     A1->Kren = A2->Kren;
2511     return;
2512     }
2513    
2514     UInt_t holeq(Double_t lower,Double_t upper,Quaternions *Qlower, Quaternions *Qupper, UInt_t f){
2515    
2516     UInt_t hole = 10;
2517 mocchiut 1.40 Bool_t R10l = false; // Sign of R10 mode in lower quaternions array
2518     Bool_t R10u = false; // Sign of R10 mode in upper quaternions array
2519     Bool_t insm = false; // Sign that we inside quaternions array
2520 mocchiut 1.64 // Bool_t mxtml = false; // Sign of mixt mode in lower quaternions array
2521     // Bool_t mxtmu = false; // Sign of mixt mode in upper quaternions array
2522 mocchiut 1.40 Bool_t npasm = false; // Sign of normall pass between R10 and non R10 or between non R10 and R10
2523 mocchiut 1.15 UInt_t NCQl = 6; // Number of correct quaternions in lower array
2524 mocchiut 1.64 // UInt_t NCQu = 6; // Number of correct quaternions in upper array
2525 mocchiut 1.15 if (f>0){
2526     insm = true;
2527     if(Qupper->time[f]-Qupper->time[f-1]==30) R10u = false;
2528     if(Qupper->time[f]-Qupper->time[f-1]<1) R10u = true;
2529     }else{
2530     insm = false;
2531     if((Qlower->time[5]-Qlower->time[0]<2)&&(Qlower->time[1]-Qlower->time[0]<2)) R10l = true;
2532     if((Qupper->time[5]-Qupper->time[0]<2)&&(Qupper->time[1]-Qupper->time[0]<2)) R10u = true;
2533     if((Qlower->time[5]-Qlower->time[0]==150)&&(Qlower->time[1]-Qlower->time[0]==30)) R10l = false;
2534     if((Qupper->time[5]-Qupper->time[0]==150)&&(Qupper->time[1]-Qupper->time[0]==30)) R10u = false;
2535     if((Qlower->time[5]-Qlower->time[0]<2)&&(Qlower->time[1]-Qlower->time[0]==30)){
2536 mocchiut 1.64 // mxtml = true;
2537 mocchiut 1.15 for(UInt_t i = 1; i < 6; i++){
2538     if(Qlower->time[i]-Qlower->time[0]==30*i) NCQl=i;
2539     }
2540     }
2541 mocchiut 1.64 // if((Qupper->time[5]-Qupper->time[0]<2)&&(Qupper->time[1]-Qupper->time[0]==30)){
2542     // mxtmu = true;
2543     // for(UInt_t i = 1; i < 6; i++){
2544     // if(Qupper->time[i]-Qupper->time[0]==30*i) NCQu=i;
2545     // }
2546     // }
2547 mocchiut 1.15 }
2548    
2549     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;
2550    
2551    
2552     if (R10u&&insm) hole=0; // best event R10
2553     if ((upper-lower<=5)&&(!insm)&&R10l&&R10u) hole = 1; // when first of 6 quaternions in array is correct
2554     if (((!R10u)&&insm)||((!insm)&&(!R10u)&&(!R10l)&&((upper-lower==210+(6-NCQl)*30)||(upper-lower==30)))) hole = 2; //non R10
2555     if (npasm&&(!insm)&&((R10l&&!R10u)||(R10u&&!R10l))) hole = 3; //normall pass from R10 to non R10 or from non R10 to R10
2556     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
2557     if ((upper-lower>=300)&&(!insm)&&((R10l&&!R10u)||(R10u&&!R10l))) hole = 5; //uneliminable hole between R10 and non R10 or between non R10 and R10
2558     if ((upper-lower>5)&&(upper-lower<=300)&&R10u&&R10l) hole = 6; // eliminable hole inside R10
2559     if ((upper-lower>300)&&R10u&&R10l) hole = 7; //uneliminable hole inside R10
2560     if ((upper-lower>210)&&(upper-lower<=1200)&&(!R10u)&&(!R10l)) hole = 8; //eliminable hole inside non R10
2561     if ((upper-lower>1200)&&!R10u&&!R10l) hole = 9; // uneliminable hole inside non R10
2562     return hole;
2563     }
2564    
2565 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){
2566     Int_t sizee = t.size()+1;
2567     t.resize(sizee);
2568     q0.resize(sizee);
2569     q1.resize(sizee);
2570     q2.resize(sizee);
2571     q3.resize(sizee);
2572     mode.resize(sizee);
2573     Roll.resize(sizee);
2574     Pitch.resize(sizee);
2575     Yaw.resize(sizee);
2576     }
2577    
2578 pam-mep 1.69 // geomagnetic calculation staff
2579    
2580     void GM_ScanIGRF(TSQLServer *dbc, GMtype_Data *G0, GMtype_Data *G1, GMtype_Data *H1)
2581     {
2582     GL_PARAM *glp = new GL_PARAM();
2583     Int_t parerror=glp->Query_GL_PARAM(1,304,dbc); // parameters stored in DB in GL_PRAM table
2584     if ( parerror<0 ) {
2585     throw -902;
2586     }
2587     /*This function scans inputs G0, G1, and H1 of the IGRF table into 3 data arrays*/
2588     // TString SATH="/data03/Malakhov/pam9Malakhov/installed10/calib/orb-param/";
2589     int i;
2590     double temp;
2591     char buffer[200];
2592     FILE *IGRF;
2593     IGRF = fopen((glp->PATH+glp->NAME).Data(), "r");
2594     // IGRF = fopen(PATH+"IGRF.tab", "r");
2595     G0->size = 25;
2596     G1->size = 25;
2597     H1->size = 25;
2598     for( i = 0; i < 4; i++)
2599     {
2600     fgets(buffer, 200, IGRF);
2601 mocchiut 1.44 }
2602 pam-mep 1.69 fscanf(IGRF, "g 1 0 %lf ", &G0->element[0]);
2603     for(i = 1; i <= 22; i++)
2604     {
2605     fscanf(IGRF ,"%lf ", &G0->element[i]);
2606 mocchiut 1.44 }
2607 pam-mep 1.69 fscanf(IGRF ,"%lf\n", &temp);
2608     G0->element[23] = temp * 5 + G0->element[22];
2609     G0->element[24] = G0->element[23] + 5 * temp;
2610     fscanf(IGRF, "g 1 1 %lf ", &G1->element[0]);
2611     for(i = 1; i <= 22; i++)
2612     {
2613     fscanf( IGRF, "%lf ", &G1->element[i]);
2614 mocchiut 1.44 }
2615 pam-mep 1.69 fscanf(IGRF, "%lf\n", &temp);
2616     G1->element[23] = temp * 5 + G1->element[22];
2617     G1->element[24] = temp * 5 + G1->element[23];
2618     fscanf(IGRF, "h 1 1 %lf ", &H1->element[0]);
2619     for(i = 1; i <= 22; i++)
2620     {
2621     fscanf( IGRF, "%lf ", &H1->element[i]);
2622 mocchiut 1.44 }
2623 pam-mep 1.69 fscanf(IGRF, "%lf\n", &temp);
2624     H1->element[23] = temp * 5 + H1->element[22];
2625     H1->element[24] = temp * 5 + H1->element[23];
2626     if ( glp ) delete glp;
2627 mocchiut 1.81 /*
2628     printf("############################## SCAN IGRF ######################################\n");
2629     printf(" G0 G1 H1\n");
2630     printf(" size %10i %10i %10i \n",G0->size,G1->size,H1->size);
2631     for ( i = 0; i < 30; i++){
2632     printf("%5i %10.2f %10.2f %10.2f \n",i,G0->element[i],G1->element[i],H1->element[i]);
2633     }
2634     printf("###############################################################################\n");
2635     */
2636     } /*GM_ScanIGRF*/
2637    
2638    
2639    
2640    
2641     void GM_SetIGRF(Int_t isSecular, TString ifile1, TString ifile2, GMtype_Data *G0, GMtype_Data *G1, GMtype_Data *H1)
2642     {
2643     /*This function scans inputs G0, G1, and H1 of the IGRF table into 3 data arrays*/
2644     int i;
2645     double temp,temp2;
2646     int it1,it2;
2647     char buffer[200];
2648     FILE *IGRF;
2649     G0->size = 2;
2650     G1->size = 2;
2651     H1->size = 2;
2652    
2653     for( i = 0; i < 30; i++){
2654     G0->element[i] = 0.;
2655     G1->element[i] = 0.;
2656     H1->element[i] = 0.;
2657     }
2658    
2659     IGRF = fopen(ifile1.Data(), "r");
2660     for( i = 0; i < 2; i++){
2661     fgets(buffer, 200, IGRF);
2662     }
2663     fscanf(IGRF, "%3i%3i%12lf%11lf",&it1,&it2, &G0->element[0],&temp);
2664     fscanf(IGRF, "%3i%3i%12lf%11lf",&it1,&it2, &G1->element[0],&H1->element[0]);
2665     fclose(IGRF);
2666    
2667     IGRF = fopen(ifile2.Data(), "r");
2668     for( i = 0; i < 2; i++){
2669     fgets(buffer, 200, IGRF);
2670     }
2671     if ( isSecular ){
2672     fscanf(IGRF, "%3i%3i%12lf%11lf",&it1,&it2,&temp,&temp2);
2673     G0->element[1] = temp * 5. + G0->element[0];
2674     fscanf(IGRF, "%3i%3i%12lf%11lf",&it1,&it2,&temp,&temp2);
2675     G1->element[1] = temp * 5. + G1->element[0];
2676     H1->element[1] = temp2 * 5. + H1->element[0];
2677     } else {
2678     fscanf(IGRF, "%3i%3i%12lf%11lf",&it1,&it2, &G0->element[1],&temp);
2679     fscanf(IGRF, "%3i%3i%12lf%11lf",&it1,&it2, &G1->element[1],&H1->element[1]);
2680     }
2681     fclose(IGRF);
2682     /*
2683     printf("############################## SCAN IGRF ######################################\n");
2684     printf(" G0 G1 H1\n");
2685     printf(" size %10i %10i %10i \n",G0->size,G1->size,H1->size);
2686     for ( i = 0; i < 30; i++){
2687     printf("%5i %10.2f %10.2f %10.2f \n",i,G0->element[i],G1->element[i],H1->element[i]);
2688     }
2689     printf("###############################################################################\n");
2690     */
2691 pam-mep 1.69 } /*GM_ScanIGRF*/
2692    
2693     void GM_SetEllipsoid(GMtype_Ellipsoid *Ellip)
2694     {
2695     /*This function sets the WGS84 reference ellipsoid to its default values*/
2696     Ellip->a = 6378.137; /*semi-major axis of the ellipsoid in */
2697     Ellip->b = 6356.7523142;/*semi-minor axis of the ellipsoid in */
2698     Ellip->fla = 1/298.257223563;/* flattening */
2699     Ellip->eps = sqrt(1- ( Ellip->b * Ellip->b) / (Ellip->a * Ellip->a )); /*first eccentricity */
2700     Ellip->epssq = (Ellip->eps * Ellip->eps); /*first eccentricity squared */
2701     Ellip->re = 6371.2;/* Earth's radius */
2702     } /*GM_SetEllipsoid*/
2703    
2704    
2705     void GM_EarthCartToDipoleCartCD(GMtype_Pole Pole, GMtype_CoordCartesian EarthCoord, GMtype_CoordCartesian *DipoleCoords)
2706     {
2707     /*This function converts from Earth centered cartesian coordinates to dipole centered cartesian coordinates*/
2708     double X, Y, Z, CosPhi, SinPhi, CosLambda, SinLambda;
2709     CosPhi = cos(TMath::DegToRad()*Pole.phi);
2710     SinPhi = sin(TMath::DegToRad()*Pole.phi);
2711     CosLambda = cos(TMath::DegToRad()*Pole.lambda);
2712     SinLambda = sin(TMath::DegToRad()*Pole.lambda);
2713     X = EarthCoord.x;
2714     Y = EarthCoord.y;
2715     Z = EarthCoord.z;
2716    
2717     /*These equations are taken from a document by Wallace H. Campbell*/
2718     DipoleCoords->x = X * CosPhi * CosLambda + Y * CosPhi * SinLambda - Z * SinPhi;
2719     DipoleCoords->y = -X * SinLambda + Y * CosLambda;
2720     DipoleCoords->z = X * SinPhi * CosLambda + Y * SinPhi * SinLambda + Z * CosPhi;
2721     } /*GM_EarthCartToDipoleCartCD*/
2722    
2723     void GM_GeodeticToSpherical(GMtype_Ellipsoid Ellip, GMtype_CoordGeodetic CoordGeodetic, GMtype_CoordSpherical *CoordSpherical)
2724     {
2725     double CosLat, SinLat, rc, xp, zp; /*all local variables */
2726     /*
2727     ** Convert geodetic coordinates, (defined by the WGS-84
2728     ** reference ellipsoid), to Earth Centered Earth Fixed Cartesian
2729     ** coordinates, and then to spherical coordinates.
2730     */
2731    
2732     CosLat = cos(TMath::DegToRad()*CoordGeodetic.phi);
2733     SinLat = sin(TMath::DegToRad()*CoordGeodetic.phi);
2734    
2735     /* compute the local radius of curvature on the WGS-84 reference ellipsoid */
2736    
2737     rc = Ellip.a / sqrt(1.0 - Ellip.epssq * SinLat * SinLat);
2738    
2739     /* compute ECEF Cartesian coordinates of specified point (for longitude=0) */
2740    
2741     xp = (rc + CoordGeodetic.HeightAboveEllipsoid) * CosLat;
2742     zp = (rc*(1.0 - Ellip.epssq) + CoordGeodetic.HeightAboveEllipsoid) * SinLat;
2743    
2744     /* compute spherical radius and angle lambda and phi of specified point */
2745    
2746     CoordSpherical->r = sqrt(xp * xp + zp * zp);
2747     CoordSpherical->phig = TMath::RadToDeg()*asin(zp / CoordSpherical->r); /* geocentric latitude */
2748     CoordSpherical->lambda = CoordGeodetic.lambda; /* longitude */
2749     } /*GM_GeodeticToSpherical*/
2750    
2751     void GM_PoleLocation(GMtype_Model Model, GMtype_Pole *Pole)
2752     {
2753     /*This function finds the location of the north magnetic pole in spherical coordinates. The equations are
2754     **from Wallace H. Campbell's Introduction to Geomagnetic Fields*/
2755    
2756     Pole->phi = TMath::RadToDeg()*-atan(sqrt(Model.h1 * Model.h1 + Model.g1 * Model.g1)/Model.g0);
2757     Pole->lambda = TMath::RadToDeg()*atan(Model.h1/Model.g1);
2758     } /*GM_PoleLocation*/
2759    
2760     void GM_SphericalToCartesian(GMtype_CoordSpherical CoordSpherical, GMtype_CoordCartesian *CoordCartesian)
2761     {
2762     /*This function converts spherical coordinates into Cartesian coordinates*/
2763     double CosPhi = cos(TMath::DegToRad()*CoordSpherical.phig);
2764     double SinPhi = sin(TMath::DegToRad()*CoordSpherical.phig);
2765     double CosLambda = cos(TMath::DegToRad()*CoordSpherical.lambda);
2766     double SinLambda = sin(TMath::DegToRad()*CoordSpherical.lambda);
2767    
2768     CoordCartesian->x = CoordSpherical.r * CosPhi * CosLambda;
2769     CoordCartesian->y = CoordSpherical.r * CosPhi * SinLambda;
2770     CoordCartesian->z = CoordSpherical.r * SinPhi;
2771     } /*GM_SphericalToCartesian*/
2772    
2773     void GM_TimeAdjustCoefs(Float_t year, Float_t jyear, GMtype_Data g0d, GMtype_Data g1d, GMtype_Data h1d, GMtype_Model *Model)
2774     {
2775     /*This function calls GM_LinearInterpolation for the coefficients to estimate the value of the
2776     **coefficient for the given date*/
2777     int index;
2778     double x;
2779     index = (year - GM_STARTYEAR) / 5;
2780 mocchiut 1.81 x = (jyear - GM_STARTYEAR) / 5.;
2781 pam-mep 1.69 Model->g0 = GM_LinearInterpolation(index, index+1, g0d.element[index], g0d.element[index+1], x);
2782     Model->g1 = GM_LinearInterpolation(index, index+1, g1d.element[index], g1d.element[index+1], x);
2783     Model->h1 = GM_LinearInterpolation(index, index+1, h1d.element[index], h1d.element[index+1], x);
2784     } /*GM_TimeAdjustCoefs*/
2785    
2786     double GM_LinearInterpolation(double x1, double x2, double y1, double y2, double x)
2787     {
2788     /*This function takes a linear interpolation between two given points for x*/
2789     double weight, y;
2790     weight = (x - x1) / (x2 - x1);
2791 mocchiut 1.81 y = y1 * (1. - weight) + y2 * weight;
2792     // printf(" x1 %f x2 %f y1 %f y2 %f x %f ==> y %f \n",x1,x2,y1,y2,x,y);
2793 pam-mep 1.69 return y;
2794     }/*GM_LinearInterpolation*/
2795 mocchiut 1.44
2796 pam-mep 1.69 void GM_CartesianToSpherical(GMtype_CoordCartesian CoordCartesian, GMtype_CoordSpherical *CoordSpherical)
2797     {
2798     /*This function converts a point from Cartesian coordinates into spherical coordinates*/
2799     double X, Y, Z;
2800    
2801     X = CoordCartesian.x;
2802     Y = CoordCartesian.y;
2803     Z = CoordCartesian.z;
2804    
2805     CoordSpherical->r = sqrt(X * X + Y * Y + Z * Z);
2806     CoordSpherical->phig = TMath::RadToDeg()*asin(Z / (CoordSpherical->r));
2807     CoordSpherical->lambda = TMath::RadToDeg()*atan2(Y, X);
2808     } /*GM_CartesianToSpherical*/

  ViewVC Help
Powered by ViewVC 1.1.23