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

  ViewVC Help
Powered by ViewVC 1.1.23