<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Create a truth database
This notebook shows how to cull needed information from the opsim and cached star databases for the relevant truth information for the stars in the Twinkles field. Once the data is gathered we also calculate the expected flux for the objects in each visit in the Twinkles run. This is used to gather values to compare against the DM processed data in our pserv databases.
"True" in this case refers to the values that come from our LSST CATSIM database. This CATSIM database stores the LSST simulated universe model that we use to provide inputs to LSST simulations. It is important to note that this means that "true" does not refer to actual stars in sky, but to the known inputs to our simulations. More information on the LSST Simulations can be found here. 
 Note
Step1:  Get cached CatSim stars
We have cached all the simulated stars in the Twinkles field in the data folder of the repository. We have written methods to interact with the database.
Step2:  Use Opsim to calculate expected flux in visits.
We also have methods to interact with the LSST Opsim databases. This can be used to get the observing conditions simulated for each visit. We then use this information to create a pandas dataframe with the expected flux based upon the Opsim information and CatSim flux of the objects in the field.
Step3:  Save to a sqlite database
Finally, we save this to a sqlite database to use in our comparisons to processed data from the simulated Twinkles images.
<ASSISTANT_TASK:>
Python Code:
import desc.monitor
import pandas as pd
%load_ext autoreload
%autoreload 2
Explanation: Create a truth database
This notebook shows how to cull needed information from the opsim and cached star databases for the relevant truth information for the stars in the Twinkles field. Once the data is gathered we also calculate the expected flux for the objects in each visit in the Twinkles run. This is used to gather values to compare against the DM processed data in our pserv databases.
"True" in this case refers to the values that come from our LSST CATSIM database. This CATSIM database stores the LSST simulated universe model that we use to provide inputs to LSST simulations. It is important to note that this means that "true" does not refer to actual stars in sky, but to the known inputs to our simulations. More information on the LSST Simulations can be found here. 
 Note: Requires an Opsim database. Twinkles 1.1 uses kraken_1042 while Twinkles 1.3 uses minion_1016.
End of explanation
truth_db_conn = desc.monitor.StarCacheDBObj(database='../data/star_cache.db')
truth_db_conn.columns
Explanation: Get cached CatSim stars
We have cached all the simulated stars in the Twinkles field in the data folder of the repository. We have written methods to interact with the database.
End of explanation
worker = desc.monitor.TrueStars(truth_db_conn, '../../kraken_1042_sqlite.db')
# Just use one visit here for the demo. Otherwise it will use the list in data/selectedVisits.csv
star_df = worker.get_true_stars(for_obsHistIds=200, catalog_constraints='gmag > 11')
star_df
Explanation: Use Opsim to calculate expected flux in visits.
We also have methods to interact with the LSST Opsim databases. This can be used to get the observing conditions simulated for each visit. We then use this information to create a pandas dataframe with the expected flux based upon the Opsim information and CatSim flux of the objects in the field.
End of explanation
worker.write_to_db(star_df, 'twinkles_truth_demo.db', if_exists='replace')
Explanation: Save to a sqlite database
Finally, we save this to a sqlite database to use in our comparisons to processed data from the simulated Twinkles images.
End of explanation
<END_TASK>