/[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.44 - (hide annotations) (download)
Fri Jan 29 17:27:57 2010 UTC (15 years, 1 month ago) by mocchiut
Branch: MAIN
Changes since 1.43: +462 -238 lines
New inclination code, added new ToF calibration files

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

  ViewVC Help
Powered by ViewVC 1.1.23