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

  ViewVC Help
Powered by ViewVC 1.1.23