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

  ViewVC Help
Powered by ViewVC 1.1.23